News for hackers. Stuff that matters.
Re:Ask HN: Why use semicolons to terminate statements in mod... by sh1mmer on Monday November 24, 2008 @03:02AM (#374576)
Minification has already been mentioned, but I wanted to re-iterate as it's such an important point. You can reduce size over the wire by as much as 30-80% by minifying and obfuscating (something like YUI or Dojo compressors do this).

Not having semi-colons risks a lot of code breakage. Doug Crockford's JSMin has an aggressive mode which always strips line breaks and another which doesn't to avoid breakage when semicolons don't exist.

Personally I would just say that if you don't use semi-colons you should think hard about why you code at all. I'm not the anal-retentive type but sloppy code isn't awesome. I would put this in the same category as naming variables $whatever or $x. If you've never picked up someones second hand project you may not appreciate why a pleasant coding style matters.

Re:Ask HN: Why use semicolons to terminate statements in mod... by timknauf on Monday November 24, 2008 @03:34AM (#374586)
I wasn't intending to come across as being anti-code-conventions! I love coding and I'm a bit shocked that my questions suggest to you that I might have a sloppy coding style in general. :^) I agree that conventions and shared idioms are extremely important. My question is, "why is this a convention?", not "why should we use conventions at all?".

I'm all for adopting the semicolon convention myself, even in sole-programmer projects, if there are reasons for using it; that's basically why I asked the question in the first place. (And of course I adopt it if contributing to a project which already uses it - no need to rock the boat.) Things like minification are exactly what I'm interested in, although I was under the impression that HTTP compression does a better job than Javascript minifiers at solving the size problem. Obfuscation is, of course, another matter entirely.

Re:Ask HN: Why use semicolons to terminate statements in mod... by adelle on Monday November 24, 2008 @05:13AM (#374647)
Welcome to the real world, where "the next programmer to work on this might be dumber than you" is considered a valid justification for cargo-cult programming.

A good minifier should replace line-breaks with semicolons as appropriate using all the same inferencing rules as JavaScript itself. Until one of those comes along it seems like using semicolons is necessary to make the best use of the tools that do exist.

Re:Ask HN: Why use semicolons to terminate statements in mod... by timknauf on Monday November 24, 2008 @07:47AM (#374738)
Welcome to the real world, where "the next programmer to work on this might be dumber than you" is considered a valid justification for cargo-cult programming.

Yes, I am a little surprised that some of the responses have assumed that because I'm questioning the usefulness of a syntactic element, I'm not interested in code quality or consistency. Quite the reverse: I'm very interested in producing code that is as clean as possible. What I've been trying to discover is whether the semicolon is a vestigial element that can now be left behind.

But the concensus seems to be that there is still a present need for semicolons, at least when it comes to interpreted JavaScript (though I'm still not convinced that the same holds for always-compiled variants like ActionScript). And like you say, current tools expect - quite reasonably - a certain coding style, even if they should use the full set of inferencing rules.

Re:Ask HN: Why use semicolons to terminate statements in mod... by inimino on Monday November 24, 2008 @09:39AM (#374850)
I've written JavaScript sans semicolons for years and have not encountered incorrect parsing by any browser. I find this style cleaner and easier on the eyes.

I haven't done any testing regarding parsing speed, but I would be shocked if anyone can demonstrate a measurable speed difference in a realistic testing scenario.

Re:Ask HN: Why use semicolons to terminate statements in mod... by timknauf on Monday November 24, 2008 @09:48AM (#374861)
I've written JavaScript sans semicolons for years and have not encountered incorrect parsing by any browser.

Yes, neither have I - but then I'm doing fairly simple stuff. I do test across a fairly broad range of browsers, though.

I find this style cleaner and easier on the eyes.

I'm glad I've found at least one kindred spirit in this regard! I was beginning to think I was the only one.

I haven't done any testing regarding parsing speed, but I would be shocked if anyone can demonstrate a measurable speed difference in a realistic testing scenario.

Perhaps with very, very large and complex code if the JavaScript engine uses an old-style interpreter? But surely the problem will continue to diminish away to nothing as more JavaScript engines implement efficient compilation.

Re:Ask HN: Why use semicolons to terminate statements in mod... by inimino on Monday November 24, 2008 @06:04PM (#375594)
I'm glad I've found at least one kindred spirit in this regard! I was beginning to think I was the only one.

People with a background in non-brace-and-semicolon languages are more likely to feel the same way. Others (e.g. recent graduates learning only Java) may be less comfortable with the lack of semicolons, and may regard it as "sloppy," perhaps from a lack of familiarity, or because they've never thought about it or haven't read the specs and are simply regurgitating what they've heard.

On a related note, in CSS the semicolon is a separator, not a terminator, so to pick on some CSS from an arbitrary Web page:

    body  { font-family:Verdana; font-size:10pt; color:#828282; } /* unnecessary semicolon */
    body  { font-family:Verdana; font-size:10pt; color:#828282 } /* equivalent but shorter */
Also on the topic of cargo-cult coding cruft, you'll often see pseudo-XHTML with self-closing tags that include a space, e.g. "<br />", invariably served as text/html, where it is equivalent to "<br>" with the addition of two extraneous characters and one syntax error. People still actively recommend this practice, despite the fact that even when served as HTML, the extra space has not been necessary since the days of Netscape Navigator 3 or 4.

Perhaps with very, very large and complex code if the JavaScript engine uses an old-style interpreter?

JavaScript is only parsed once, even in ancient browsers, and no matter how much code there is, the time required to download it, not to mention executing it, will typically be orders of magnitude greater than the time spent parsing it. I had never heard the performance reason before as a justification for this practice, but I consider it a total red herring until someone provides hard numbers that suggest otherwise.

Ultimately this is a question of style and personal preference, but if you happen to prefer the cleaner style as I do, I see no technical reasons why you should not use it.

Finally, never forget Wadler's Law:

http://www.haskell.org/haskellwiki/Wadlers_Law

Re:Ask HN: Why use semicolons to terminate statements in mod... by timknauf on Monday November 24, 2008 @06:54PM (#375688)
Thanks for the detailed reply.

People with a background in non-brace-and-semicolon languages are more likely to feel the same way.

Yes, and most of them are off happily working with their Pythons, Rubys, Haskells and the like, staying well away from this sort of discussion. :^) I've been writing a lot of ActionScript lately (making games which target Flash), so I've been thinking about ECMAScript-style syntax a lot more than I have in the past.

Also on the topic of cargo-cult coding cruft...

I see "cargo-cult coding cruft" most often in languages which are parsed by a client (e.g. CSS, XHTML, JavaScript). I suspect that this is due to fear that some client, somewhere will choke on the actually-correct code. And whaddaya know - sometimes that client actually exists. It's called Internet Explorer 6. ;^)

Finally, never forget Wadler's Law

Yeah, I'm reminded of an old Python joke:

  Question: Why is Python such a productive language?
  Answer: You won't have to spend any time arguing about brace style.
Re:Ask HN: Why use semicolons to terminate statements in mod... by adelle on Monday November 24, 2008 @05:17AM (#374648)
A domain-specific compression algorithm will often outperform a general purpose compression algorithm, and sometimes a combination of both is even better.

Besides, downloading a zip file is still faster than downloading the uncompressed file, so either everybody's doing HTTP compression wrong or it's not so hot to start with.