Semicolons give me comfort

The other day at work we had a discussion about how code should look, or rather, what characters to use.

It all started when I made the comment:

I like semicolons, they give me comfort

Immediately I had three Ruby on Rails developer laughing so hard tears would come out of their eyes. “Semicolons, such bullshit. Look how clean OUR code is” (Clean where? However, preprocessing code with no idea of the output is a topic for another day…). So, we talked about it in a more sensible manner, once everyone had calmed down, and it got me thinking.

Why do I feel comfort in semicolons? Should just line endings just be good for most purposes? Cleaner, less noise etc. I think it all stems from me doing JavaScript development for about 12 years now – semicolons is like a safety line, a way to control (or rather, not rely on) semicolon insertion in JavaScript. Also, with objects, multiline statements etc, it just feels good to have.

But from another point of view, I think for me it’s also about punctuation. No matter if some statement is one or several lines, no matter what characters it contain; when I see the semicolon I know that that specific statement is over. That gives me comfort.

Or do I just want Douglas Crockford to like me?

What are your thoughts on semicolons and how code should look like?

41 Comments

  • rajakvk says:

    I \\feel\\ to have semicolons

  • tb says:

    Hehe Douglas Crockford…I like them too and I don’t feel like the code looks cleaner without them, I think it gives the code more structure and makes it easier/faster to read.

  • I like them very much indeed:

    Semicolons Give Me Closure

  • Hi Robert,

    I do agree on you! I’m a Java programmer for profession and Ruby first adopter. While learning Ruby I had too many difficulties with the indentation issues and punctuation. Even Java is so typed and verbose, it gives me a security point that I can’t see while Rubying 😀
    It may a be a matter of habit, but I’m not used to yet. On the other hand, you can do things lot of easier with a few lines and this makes things to look easier.
    Interesting discussion…
    What do you think?

  • Pete says:

    I love the look of Python, which is similar to Ruby in the way it uses line breaks as the delimiter.

    So, I prefer not to have them, but they make sense in JavaScript — how could you reliably minify a script without a semi-colon delimiter? Since Javascript is always downloaded on the fly, I’d consider this a welcome practical feature more than an aesthetic one.

  • Douglas already likes you, even if you don’t agree with him on everything … or perhaps BECAUSE you don’t agree with him on everything.

    I am myself VERY opinionated on what good javascript should look like, and whether or not there are optional semicolons and optional curly braces.

    In my view of the (JavaScript) world there is no “loose” syntax, semicolons and curly braces MUST ALWAYS be used.

    Am currently building slides for a talk, and one of the talking points will be about writing beautiful code, vs. sloppy code.

  • Remy Sharp says:

    The reason I put semi-colons in my code (the code being JavaScript), is because I don’t like something else inserting them for me. I’d rather know that my code is executing what I’ve written.

  • Anders Ringqvist says:

    Wise men says that explicit trumps implicit in most cases. But the old syntactically significant whitespace jihad will probably rage on forever.

  • Dan Eastwell says:

    I think it’s something as simple as javascript can and should be compressed and minified in a way that almost all other languages don’t have to be, therefore it’s dangerous to rely implicitly on line endings in javascript.

  • Semicolons are for girls and douchebags.

  • fvsch says:

    I mostly do CSS, JavaScript, and a bit of Python.
    – In CSS, you need the semicolons save for the last one in a declaration block. I try to add this last semicolon as well since I code my CSS with one declaration per line, and sometimes I need to add or reorder declarations and it’s really easy to end up with a missing semicolon in the middle of the block. And even though CSS could have been written differently with optional semicolons or significant line breaks, I find the semicolons quite useful to signal the end of a very long value (see the background property in CSS 2.1, or multiple backgrounds and gradients and other verbose/complex stuff in CSS 3).
    – In JavaScript, I don’t want to rely on semicolon insertion, and it’s a good practice to write explicit code that can be minified efficiently.
    – In Python, I like it the way it is and I don’t miss curly brackets and semicolons. The significant whitespace thing works quite well (especially since it’s more tolerant than many people think) and I’ve never had issues with it.

    Different languages, with different syntaxes and constraints. Saying that Python or Ruby are better than other languages because they don’t need semicolons is ridiculous and childish. Saying that you personally prefer the “style” of a specific language over an other is fine.

  • Vernon says:

    I definitely support the use of semicolons (paid for by the ad council for semicolon’s of America). Not only do you not rely on automatic insertion, but if you are debugging a file that you’ve never touched the semicolons give you a quick reference to where statements and the such are ending.

  • […] This post was mentioned on Twitter by Robert Nyman, Martin Andersson, Aditya, Web RSS News, Gustavo Freitas and others. Gustavo Freitas said: RT @robertnyman: Semicolons give me comfort: http://t.co/bFAKJ6a […]

  • Goulven says:

    I do mostly PHP, JS and CSS and I always use semicolons. However, I’d appreciate whitespace to be significant in PHP because:
    * Only properly indented code would work, making it easier to share with other devs (sigh…)
    * Lines often end with parenthesis, making it clear that statement is over
    * Semi-colons are both typed with the same hand, which slows typing down a bit
    * Hitting Enter seems to me proof of intent enough that I’m done with this particular line. Plus, don’t tell me you’ve never slammed the Enter key hard after squashing a nasty bug?

    But I believe PHP won’t change, and I’d be fine doing without semi-colons when switching over to Python or Ruby.

  • Semicolons gives us control.

  • Aldrik says:

    I always use semicolons in JavaScript as you can’t use backslash-newlines after operators as I would in other languages.

  • Dan says:

    Am I the only one who noticed the statement should be:

    I like semicolons; they give me comfort.

    Missing a semicolon in a statement about semicolons. Oh, the irony! 🙂

  • Adam Trimble says:

    Ironic that your sentence should actually use a semicolon to avoid a sentence fragment.

    “I like semicolons; they give me comfort.”

  • I agree with Remy Sharp.

    Writing JavaScript, inserting my own semicolons give me comfort.

    Semicolons will keep my JavaScript code predictable when it is mashed by concatenation and minification.

    I prefer to not have minified and concatenated code which stops working and requires debugging.

    When it comes to other programming languages, I really do not have an opinion since I jump between Java, PHP, Ruby, Python, Obj-C and Bash on hour basis.

    I do believe one thing, though: being inconsistent with white-space AND not using semi-colons is a really bad idea.

  • Laidlaw says:

    I’d like to round up every semicolon in existence, throw them into a deep pit, and bury them alive.

    Love,
    Ruby & I

  • Andy says:

    I have one word for you: CoffeeScript.
    😉

  • Dan @ 17: No, you’re definitely not the only one. We started laughing for an entirely different reason than the Ruby developers — I’m just sad that you beat me to the comment 😉

    For anyone unfamiliar with the grammatical error: When you separate two independent clauses (which could each be a whole sentence) with a comma, it is called a comma splice. It can be solved, to list a few options, by turning it into two sentences; by replacing the comma with a semicolon; or by adding a conjunction, such as “and” or “or”, between the two clauses. (Note that I just demonstrated another use of the semicolon — separating a list of terms with internal punctuation.)

  • George Lee says:

    Personally, I prefer indentation to any other delimiters. I will spend a lot of my time adding them in before I’ll consider editing the code. My comfort comes from being able to look at code and instantly see This depends on that.

  • trevnorris says:

    The use of a line delimiter in code that can actually be seen is important to me. Python or Ruby syntax does have a certain simplistic beauty to it, but to control code evaluation by whitespace is strange (imho). The purpose of code syntax is to help us humans understand what’s going on, and semicolon/curly give a lot more flexibility to change code around when desired.

    Guess the arguments is that by forcing proper whitespace the code will always be easy to understand, but I’ll have to disagree with that. Really, this feels like another Emacs vs. Vim. Why do I have to be convinced that one is better than the other. Just use what you like, and accept other people like something else.

  • George Lee says:

    I think what could be most acceptable is the ability to use both in all languages. If that did happen we would have to enforce both in the language, of course.

  • SamuraiJack says:

    I wonder if those who states, that the code w/o semi-colons can’t be minified ever tried to that. If so, when (three years ago?). Nowadays all A-grade minifiers can deal w/o semi-colons just fine.

    Using semi-colons for me, is like writing the code like this:

    > if (a < (b + c)) { … }

    The inner parenthesis gives me comfort and free me from the requirement to look at the operator precedence table.

  • Robert Nyman says:

    Thanks for the comments everyone!

    Gustavo,

    I’m hooked on semicolons, but the majority of my coding is HTML, CSS or JavaScript, so I think that explains it too.

    Jeena,

    Where you are the latter then? 🙂

    fvsch,

    Completely agree!

    Goulven,

    Hitting Enter is definitely intent while you’re typing, but not when you’re skimming through the code, I think.

    Dan, Adam, Chris,

    Yeah, yeah…But I was talking about semicolons in code and not English language. 🙂
    I’ll leave it as it is, for reference for your comments.

    Laidlaw,

    🙂

    Andy,

    Ah, no, I’m allergic to not writing what actually comes out.

    trevnorris,

    No convincing needed at all, just interesting to hear peoples’ views on it. I agree on using what you like, as long as you agree with the other people that will work with the same code.

    George,

    Interesting thought!

    SamuraiJack,

    Haven’t really tested that in a while, but for me, that’s not one of the reasons.

  • Steve Williams says:

    I certainly prefer languages that use punctuation delimiters, but that’s what I’m used to; I think it’s conditional. Spending years scripting/coding with or without punctuation will affect your preference.

    Annotation, white space and indentation are far more important for readability though.

    Minification is a good argument for why non-compiled languages should use punctuation to delimit clauses.

  • Tim Heighes says:

    As one of the three Ruby developers with tears in their eyes, I remember Robert actually saying, “I like semi-colons; they help readability.”

    I code Javascript too and use JSLint to insist on semi-colons when I do, so I’m not against the use of semi-colons at all. In fact, I’m a bit anal about Javascript syntax because of the need to minimise production code.

    When I switch from coding Ruby to PHP, I always forget to add semi-colons but never add semi-colons to Ruby when switching back. I guess I just appreciate the “simplicity” or “clarity” of Ruby syntax. It just feels natural.

  • Robert Nyman says:

    Steve,

    Absolutely, it’s a lot about what you get accustomed to.

    Tim,

    So you decided to out yourself? 🙂
    The quote could have been that, but for me readability is comfort.

    I’m glad that you like the syntax in the languages where you work.

  • Robert Nyman says:

    Aldrik,

    Interesting, thanks for the tip!

  • Micke says:

    Looking at many of the newer languages like ruby, python and scala (which don’t require semicolons), semicolons in programming seems to be a thing of the past. I see why you feel good using them, It can give you a sense of greater control, but they’re really just there too keep you comfortable 🙂

    In 10 years time you will probably laugh at this blog post – “I can’t believe we used semicolons at the end of EVERY row! And we thought it was good to have them! How ridiculous!” 🙂

  • Robert Nyman says:

    Micke,

    While there’s no doubt my like comes from many years getting used to it, and that many newer languages don’t have, I still wouldn’t count it out.

    When browsing through a large document I to think they help readability, but who knows – I might change my mind over time too. 🙂

  • Chris says:

    I don’t trust using whitespace as a delimiter because you can’t see what the delimiter actually is, whether it’s a tab, space, newline or carriage return (or both – \r\n – on Windows) – at least I can see the semi-colon and KNOW that what I’ve terminated the line with.

    If you jump between *nix, Windows and Unix and rely on line terminators as code terminators, you’re actually relying on 3 different character sets – \n, \r and \r\n. JavaScript is technically worse as it can use all of them AND semi-colons. So to simplify matters, I tend to ALWAYS use semi-colons to know exactly where I’ve terminated the line and what with 🙂

    While I’ve never had whitespace delimiters cause an actual problem in Python it still feels “alien”; I miss my curly braces and semi-colons!

  • Robert Nyman says:

    Chris,

    That’s exactly the same way I feel!

  • Clare says:

    Over the past 3 years, I’ve noticed a an attachment to semi-colons which only gets stronger; I can always see when one is urgently needed! 😉
    You’ve made me realise that it could be linked to my career in design – coding and semi-colons go hand-in-hand, and that’s probably contributed to my obsession!

  • Robert Nyman says:

    Clare,

    Glad you feel that way! 🙂

  • baash05 says:

    I am a semicolon lover!

    I personally have never understood the “makes code more readable”.
    After 15 years in c++ I read it almost as fast as I read English.
    Heck I’ve spent more time studying it’s grammar.

    Clean code in most any language is easy to read (if you know the lang, and often even if you don’t)

    I miss the ; and to be perfectly honest.. I put it in my ruby, because if I don’t then when I go back to c/c++, I almost always forget it.

  • Robert Nyman says:

    baash05,

    Good to hear! 🙂

  • ThorX89 says:

    Re: Pete:
    You don’t need semicolons to “reliably minify” a JS file.
    Newlines make text files more spacious visually, but on the bytelevel, it doesn’t matter (on unix-like systems) whether you use semicolons or newlines.

Leave a Reply to Chris Sullins Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.