Brand new to Swift, can I end statements with a semi-colon?

Discussion in 'Mac Programming' started by The Clark, Jan 15, 2016.

  1. The Clark macrumors regular

    The Clark

    Dec 11, 2013
    Victoria, BC
    I've done a little bit of research and different people have different opinions. I come from a programming background and it feels very unnatural to not include a semi-colon at the end of a statement. I would prefer to write a semi-colon but is that seen as bad practice? Does it have any effect on the performance or efficiency?

    Do you guys (and gals) include semi-colons?

  2. ArtOfWarfare macrumors G3


    Nov 26, 2007
    I'm not familiar with Swift, but in other languages, you should include a semicolon if a lack of one sometime means something different, as in JavaScript.

    In JavaScript, it'll attempt to automatically join consecutive lines if you don't have a semicolon. It'll only add in a semicolon if implicitly joining the lines together would result in invalid syntax. This can result in really weird and hard to track bugs, so in JavaScript, you should always end lines in semicolons, even though they're supposedly optional in the language.

    In Python, if you don't end a line in a semicolon, one will be added automatically. So in Python, semicolons are completely pointless and the best practice is to never use them. They're just meaningless clutter in Python.

    So with Swift, you need to find out whether the presence of a semicolon ever changes how things are interpreted or not. If it never impacts it, then the best practice is to leave them off. If it sometimes impacts it, then the best practice is to always use them.

    There is no language where mixing semicolons and non-semicolons is a good practice.
  3. 960design macrumors 68030

    Apr 17, 2012
    Destin, FL
    Do not include a semi colon. This helps my brain separate the languages. Including a semi colon has absolutely no effect on performance or efficiency. You could ask the same about coding using K&R. I absolutely hate looking over code from someone that chooses NOT to include brackets for single line functions, just because they can, but that is me.

    Swift convention is no semicolons.
  4. The Clark thread starter macrumors regular

    The Clark

    Dec 11, 2013
    Victoria, BC
    Okay thank you. If it's convention I'll follow it.
  5. theluggage, Jan 19, 2016
    Last edited by a moderator: Jan 19, 2016

    theluggage macrumors 68040

    Jul 29, 2011
    Unless you're working as part of a team with well-defined code layout conventions, the person with the greatest need to read and understand your code is you. If you find semicolons helpful or comforting, include them. They won't have the slightest effect on the operation or efficiency of the program, and stripping them out, should the need arise, is trivial.

    If the worst thing a "language lawyer" can say about your code is that you've used unnecessary semicolons or deviated from the One True Brace Style then you're comfortably ahead of the game.

    Remember the words of Sir Terry Pratchett: rules are there to make you think before you break them.

    Only danger is that the Swift designers seem determined to expunge all trace of C nostalgia from Swift, so once they've dumped the '++ & --' operators and the 'for(;;)' loop (planned for Swift 3) it wouldn't be surprising if they come for the semicolons next (since the 'for' loop was the thing that needed them).

Share This Page