10

I'm used to delimit statements by a semicolon from Java, so naturally I do it in Scala code too. I also feel that the code is easier to read, because it's evident where one statement ends and another begins. But many times when I post a piece of Scala code on SO, the code gets edited just to get semicolons removed.

  1. Should I use semicolons or not? Are there any "official" guidelines or coding style?
  2. Are there cases where semicolons are required, otherwise the code is ambiguous?
3
  • 5
    Has Haskell taught you nothing? ;P remove unnecessary syntax, semicolons should only be used to break up multiple clauses/statements on the same line Jul 10, 2013 at 12:58
  • @JimmyHoffa Haskell taught me that syntax must be beautiful and consistent :).
    – Petr
    Jul 10, 2013 at 13:02
  • 2
    The first part of that is why I get annoyed whenever I see Scala snippets... Jul 10, 2013 at 14:28

2 Answers 2

9

There is no officially "right" way of doing it, but most practitioners omit semicolons wherever they can (which is why so many people remove semicolons reflexively).

The language tries very hard to make you forget about the need to terminate statements at all. Unfortunately, it doesn't quite succeed, so there are various intricate situations in which semicolons, or an extra set of parenthesis, or similar "hints" are necessary to avoid unwanted semantics. Such situations are almost rare enough that normally you don't have to think about them. Opinions differ on whether this means you should punctuate defensively (that's what I think) or never punctuate until you run into trouble (that's what the majority of users think). Ultimately, it comes own to whether you self-identify as a renegade or a model citizen.

3
  • Thanks, could you perhaps give an example (or a few :)) of such intricate situations?
    – Petr
    Jul 10, 2013 at 10:17
  • 3
    I was afraid I'd get asked this... Programming in Scala lists several of them that surprised me, but I don't have the book with me at the moment, and I forget the details. That's the point I was trying to make: if I could remember these exceptional situations it wouldn't be so bad, but as it is, I always feel uncertain whether I can really omit the semicolon - so I don't. Jul 10, 2013 at 10:47
  • Huh. I've never once run into a situation where missing a semicolon in Scala has hurt me. I never include them. I've also only programmed Scala for hobby-like things though, so maybe it will bite me sooner or later.
    – KChaloux
    Jul 10, 2013 at 14:11
4

How much do you want to understand the languages you use? Do you want to play to its strengths or not? Scala's syntax encourages clear and concise code; the few edge cases where taking advantage of this causes problems for the Scala parser are instructive (that is, you learn more about Scala by encountering and learning how to avoid them) and gradually being fixed (e.g. suffix notation). Defensively using the semi-colon may mean you won't ever have to learn about these issues but do you really see that as a good thing? The fixes for these issues usually have other implications but you will miss out on these possibilities.

Another consideration is that, as Kilian concedes, most Scala developers omit semi-colons by default. How do you expect to work with other people's code if you avoid using idiomatic Scala yourself? You're going to find it much harder than it need be.

I can't emphasis enough that these features of the Scala parser were chosen to encourage clean, re-usable and functional (in the fp sense) code. Take infix operator syntax as an example; it encourages developers to furnish classes with simple, single-purpose methods which compose well together. Scala's collection library shows just how beatifully that can work. Classically-trained Java devs who embrace this will tend to develop better habits and new ways of thinking about their code. Those who stick to all those familiar dots and brackets will miss out. I contend that this is also true for those too timid to abandon the semi-colon.

3
  • 8
    -1 yes, being "forced" to learn the language due to compiler gotchas went out of style with C++. Also, there's little evidence behind the assertions, which I would want for an answer with such biased tone.
    – Telastyn
    Aug 27, 2013 at 16:42
  • (1) What @Telastyn said -- if you really want to learn a language this way, get a cat, they love sitting on keyboards because they care about your education. (2) Omitting semicolons have nothing to do with clean style or encouraging it -- if anything, dropping semicolons is hiding the fact that these are separate statements that are executed sequentially, and possibly have side-effects. Leaving semicolons in makes you aware of that and therefore prefer a more functional style. The decision is therefore purely syntactic (eg, style, parsing, etc). Aug 16, 2016 at 7:29
  • While the "forced to learn language" doesn't fly, "using the language as its authors intend" and "as most other developers use it" are important reference points. Maintaining consistent style between multiple developers is useful. From both this answer and from @Killan Foth's, the maintainers and most developers apparently view the semicolon as extraneous. May 23, 2018 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.