Wednesday, 15 March 2017

Why are semi-colons sometimes not required?

Over the 20 years I've been developing commercial software one question I still have with the Delphi language is why was the language designed so semi-colons are not required at the end of methods? For example:

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:

  1. Adding the semi-colon makes the code more consistent.
  2. It means later on when the code is modified the semi-colon does not have to be added.
I have come across Delphi developers who will not put in the semi-colon if it is not required, but cannot see any real benefit from leaving it out. Maybe I have answered my own question, and that is the semi-colon is a separator and when the language was developed they did not see any pros or cons of just allowing the semi-colon not to be there.


6 comments:

  1. 5 seconds with Google could have answered the question authoritatively for you.
    http://lmgtfy.com/?q=pascal+semicolon

    ReplyDelete
    Replies
    1. 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'?

      Delete
    2. Just 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.

      Delete
  2. Personnally 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.)

    I 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.

    ReplyDelete
  3. 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.

    ReplyDelete
  4. Pascal adopted the ALGOL way of using the semicolon as a statement separator, including the option to leave it out at the end.

    ReplyDelete