function DoSomething: boolean;
begin
if FDoSomethingElse then
begin
DoSomethingElse;
end
end;
I believe the reason why the compiler does not complain about this, is because the semi-colon in Delphi is a statement separator and not a terminator. If I modify this function for example:
function DoSomething: boolean;
begin
if FDoSomethingElse then
begin
DoSomethingElse;
end;
DoOtherStuff
end;
I obviously need to now add in the semi-colon, but I do not need to add in the semi-colon after 'DoOtherStuff'. I currently cannot see any benefit of leaving the semi-colon out. I would not normally code this way and always add in the semi-colon for 2 reasons:
- Adding the semi-colon makes the code more consistent.
- It means later on when the code is modified the semi-colon does not have to be added.
5 seconds with Google could have answered the question authoritatively for you.
ReplyDeletehttp://lmgtfy.com/?q=pascal+semicolon
No 5 seconds on Google does not answer the question. Maybe you do not understand what I am asking, my question was 'why was the language designed so semi-colons are not required at the and of methods?' The important word in the question is 'why'. From answering 'why' I might be able to understand the benefits of not adding the semi-colon, please look at the example code. Most developers I know would add a semi-colon, what do you do, would you add a semi-colon at the end of 'DoOtherStuff'?
DeleteJust looking at an article by Marco Cantu http://www.marcocantu.com/epascal/English/ch05stat.htm and if you look at this you will see him sometimes adding the semi-colon (procedure TForm1.Button2Click) and sometimes not adding (procedure TForm1.Button3Click), but most of the time he seems to add the semi-colon that is not required.
DeletePersonnally I extend the always put in semicolons to always putting in begin/ends. Saves you time later, and occasionally a heck of a lot of debugging (hey, look, indenting stuff isn't enough to make it optional! Not that I am endorsing white space/ident aware code. That is just a pain.)
ReplyDeleteI always found repeat/until's diversion from the single line of code and begin/end annoying too.
At least we can't write C's bugs like: if (a=c)
Still, I have used that dirty little cheat - but added comments to make it clear it was on purpose.
The semicolon was meant to be a statement separator (separating two adjacent statements). In that sense, the final separator is superfluous as there is no statement after the last one. Consider e.g. a CSV format: the comma is there to separate adjacent fields and there is no need for the final comma at the end of each line. In fact, putting it there would create a superfluous empty value. The semicolon after the last statement creates a superfluous empty statement, too. But I agree with your points and I think it's just better style to consistently insert a semicolon after each statement.
ReplyDeletePascal adopted the ALGOL way of using the semicolon as a statement separator, including the option to leave it out at the end.
ReplyDelete