Register | Sign In


Understanding through Discussion


EvC Forum active members: 65 (9162 total)
5 online now:
Newest Member: popoi
Post Volume: Total: 915,821 Year: 3,078/9,624 Month: 923/1,588 Week: 106/223 Day: 4/13 Hour: 0/2


Thread  Details

Email This Thread
Newer Topic | Older Topic
  
Author Topic:   Software Development
Percy
Member
Posts: 22394
From: New Hampshire
Joined: 12-23-2000
Member Rating: 5.2


(1)
Message 1 of 4 (909415)
04-03-2023 1:28 PM


A Thought About the Semicolon
I need a break so I thought I'd post a thought about the semicolon.
Throughout much of its history the languages of computer science have required semicolons at the end of statements. This was a great help syntactically, making it easy for the compiler/interpreter to recognize the end of statements.
Not all languages required semicolons, but most. Assembly languages didn't use the semicolon because it was one line per instruction, no statement separator needed. Fortran was also only one statement per line (per IBM card in the earlier days), so no semicolon. But almost all the higher level languages that came after required semicolons between statements, COBOL being a notable exception. Languages I've used that required semicolons were Bliss Bliss32, C, C++, Perl and PHP.
Language requiring no semicolons that are increasing in popularity are Python and Swift. Semicolons are only necessary if you're including multiple statements on the same line.
And then there's Javascript. In Javascript semicolons are optional. Mostly. These statements need no semicolon:
let arr = []
arr[0] = 5
arr[2] = 10
arr[3] = 25
arr.join(', ')   // Value is "5, 10, 25" due to implicit conversion of the integers to strings
But this will not work because the last line is illegal syntax:
let arr = []
arr[0] = 5
arr[2] = 10
arr[3] = 25
(arr[0] + arr[1] + arr[2]).toString()  // Illegal syntax
The reason that it's illegal syntax is that the "25" is followed immediately by an open paren on the next line, which causes the interpreter to interpret the 25 as a function and the open paren as the beginning of its argument list. So we have to add a semicolon after the "25":
let arr = []
arr[0] = 5
arr[2] = 10
arr[3] = 25;
(arr[0] + arr[1] + arr[2]).toString()  // Legal syntax, value of the string is "40"
I've been writing Javascript since the early 2000's and have always used semicolons after every statement. Normally I would include a semicolon after every line, like this:
let arr = [];
arr[0] = 5;
arr[2] = 10;
arr[3] = 25;
(arr[0] + arr[1] + arr[2]).toString();  // Legal syntax, value of the string is "40"
That was my habit. Not using semicolons seemed blasphemous, plus I was writing copious amounts of code in Perl and later in PHP, both of which require semicolons. Switching back and forth between using and not using semicolons would have caused numerous errors of omission.
But recently I've been working on a website that is Javascript only, and I've been leaving out the semicolons. The cases where a semicolon is required, like the example above, almost never arise is real code, especially in the NodeJS/ReactJS context I'm currently working in. Problems do arise when I make changes to the PHP code of this website where semicolons are required and which now I keep forgetting.
But I plan to write increasing amounts in code in Javascript, so I'll continue leaving out the semicolons.
--Percy

Replies to this message:
 Message 2 by Stile, posted 04-04-2023 8:50 AM Percy has replied

  
Percy
Member
Posts: 22394
From: New Hampshire
Joined: 12-23-2000
Member Rating: 5.2


(1)
Message 3 of 4 (909457)
04-04-2023 9:26 AM
Reply to: Message 2 by Stile
04-04-2023 8:50 AM


Re: A Thought About the Semicolon
I agree with much of what you say. I never cared much about particular programming languages - they were all just a means to an end.
Something's sadly lacking in my education, because I don't know what ladder logic, AAA programming, Function Block Diagram or Statement Logic are. I always used (and still use) the code outline approach where you distribute bare bones functions and data object definitions among code modules then fill in the details.
Swift was the one language where I couldn't use that approach because the XCode IDE uses a graphical interface where you interconnect graphical boxes of a variety of types with lines representing control and dataflow. XCode then creates templates for each graphical box and you fill in the functional code. You can write bare Swift, but given that the target is an iPhone or iPad it doesn't come out well. For example, if you implement in bare Swift you have to manually handle device rotations, while the graphical approach takes care of it for you automatically.
I might have miscommunicated my feelings about semicolons. I could care less about them. They're just a habit. But I am worried about a higher error rate if I have to move back and forth between semicolons-required and semicolons-optional environments.
The smart thing to do would be to maintain my habit of using semicolons, because that way I would avoid simple errors, but I've opted to go semicolon-free in Javascript and am hoping I don't have to move back to PHP too often. But I fixed a PHP bug on 3/20, and I fixed another last night, and I kept forgetting the semicolons.
--Percy

This message is a reply to:
 Message 2 by Stile, posted 04-04-2023 8:50 AM Stile has replied

Replies to this message:
 Message 4 by Stile, posted 04-04-2023 12:18 PM Percy has not replied

  
Newer Topic | Older Topic
Jump to:


Copyright 2001-2023 by EvC Forum, All Rights Reserved

™ Version 4.2
Innovative software from Qwixotic © 2024