r/javascript • • Feb 05 '11

Are Semicolons Necessary in JavaScript?

http://aresemicolonsnecessaryinjavascript.com/
0 Upvotes

66 comments sorted by

View all comments

18

u/drobilla Feb 06 '11

Nobody argues it isn't valid to omit semicolons. People recommend against it because it is a mind-blowingly braindead language "feature" that should not exist in the first place, and can cause confusing errors and other problems.

Fuzzy ambiguous syntax may have a place in content (e.g. markup), but it sure as hell has no place in programming language syntax. The tragedy of Javascript is that underneath the awful veneer is a pretty elegant language...

-2

u/sjs Feb 06 '11 edited Feb 06 '11

If you prefix ( and [ with semicolons you're good to go. Nowhere else are they needed, and the problems with ASI aren't solved by using semicolons everywhere.

e.g.

function foo() {
    return
    {
        something: 'here'
    }
}

Using semis everywhere doesn't save you from ASI, you just have to be careful about line breaks. Whether you use semis or not. The problem isn't omitting semicolons, it's inserting spurious newlines. Keep braces on the same line as returns or wrap the returned expression in parens, return (....

http://inimino.org/~inimino/blog/javascript_semicolons

http://blog.izs.me/post/2353458699/an-open-letter-to-javascript-leaders-regarding

You have to know the rules either way. It doesn't matter whether or not you use them, it's purely a style thing. Either way we shouldn't try to make anyone else conform to our preferred style unless they're contribution to a project.

edit: Downvotes but no rebuttals. If you ask me that's pretty telling.

4

u/plantian Feb 06 '11

I think you're missing the point here. ASI/Semicolons are not the problem. Here is the problem: "Either way we shouldn't try to make anyone else conform to our preferred style unless they're contributing[contribution] to a project."

This kind of shit just wastes everyone's time because every project then spends hours debating the same shit when each member did something different in the last project. Personally I don't find the location of semicolons in my code a way of expressing myself. The debate on the node list, irc and twitter that you mention is case in point. This is the kind of shit that REAL leadership would eliminate. Crockford almost was able to get everyone together but apparently he failed because it says all over his site to just use semicolons and a lot of other things that people don't follow because they want to express themselves with fancy semicolon placement, dropping braces and pushing around whitespace. If you like JavaScript then you're only hurting its progress with this shit. Right now we could be talking about how to improve modularization of JavaScript, improve sharing of JS between frameworks or even handle future integration of new features but instead we are all talking about total bullshit. I think the phrase is "can't see the forest for the trees".

Take one for the team and always use semicolons. The problem has already been solved: http://javascript.crockford.com/code.html

Now, let's go write some libraries, plugins and apps!

1

u/sjs Feb 06 '11

This kind of shit just wastes everyone's time because every project then spends hours debating the same shit when each member did something different in the last project.

There's no debate. Founders of a project choose the style and contributors conform. It's just polite.

Anyway we're in total agreement.

1

u/plantian Feb 08 '11

I can't tell if you are baiting me or not here but sadly I go for both synthetic and live bait.

I don't think we are quite in agreement. I'm saying that ALL JavaScript developers should strive to all follow the EXACT same conventions not just whatever that project founder feels like. Founders can have styles for whatever higher level patterns they want but letting project founders all make up their own low level coding conventions just creates fragmentation. I hate this stupid website because all it does is open this crap up for debate for every founder rather than just letting the JS community globally finally agree on one damn thing. The tone of this discussion is a good sign though that people are finally coming together behind something. It gives me hope. Its small but everything has to start somewhere. The JavaScript community, as represented in this discussion, is waiting for you to get on board.

1

u/sjs Feb 08 '11

I'm not trolling. And we're not quite in agreement. I appreciate your point of view but I don't think it's realistic.

I'm saying that ALL JavaScript developers should strive to all follow the EXACT same conventions not just whatever that project founder feels like.

That would be great, but without a BFDL to mandate 4 spaces or whatever like Guido can do for Python that is just a pipe dream.

Some people indent with 2 spaces, some with 4, some with tabs. Some like hanging braces and some don't, some like spaces around operators and some don't. Some like spaces between keywords and conditions, some don't. The semicolon thing is a non-issue in comparison! Generally people use them and that's that.

I personally don't use them in my JS unless it's an open-source library that I want others to use. I make a pragmatic decision to include them because some will turn up their nose if they don't see any semis. It's ridiculous, but I do it anyway because people don't understand the issue and when people don't see semis they get uncomfortable because they don't understand the rules about when they're necessary.

It's easy to tell them the rules, but their minds have been tainted by all the misinformation out there. I'm trying to put a stop to the misinformation but everyone here is putting their fingers in their ears and going "LA LA LA!" ... which is why I've given up trying to reason with them.

1

u/vectorjohn Feb 07 '11

var f = function(){} var x = 9

syntax error. Downvotes explained.

1

u/sjs Feb 08 '11 edited Feb 08 '11

That's a straw man, nobody writes code like that. You should be writing it one of these ways:

var f = function(){},
    x = 9

or

var f = function(){}
var x = 9

If you want to write it on one line you obviously need a semi colon. But if anyone wrote code like that in a project I worked on I would fix it, that's just stupid unless it's something trivial like var a = 1, b = 2 which would still be only one statement.

1

u/vectorjohn Feb 08 '11

I may have misunderstood. I was thinking you were implying this code would work minified, when it wouldn't. But I see somewhere else you said that the minifier should be a proper javascript parser (the more popular ones aren't, hence the argument).

So n/m. But semicolon insertion is a mis-feature anyway. White space should either be significant or not, not sometimes significant.

1

u/drobilla Feb 06 '11

The problem/example you describe is a symptom of the flawed delimiters in Javascript, not an argument in support of it.

1

u/sjs Feb 06 '11

ASI isn't going anywhere, we are stuck with it. I don't care if people use semis or not, but rallying for one side or the other is silly. This subject has been discussed to death on the Node mailing list, in blogs, on IRC and twitter many times over. Unless there are new arguments on either side there's no real point discussing this.

People who don't really understand ASI need to stop spreading FUD on the issue. Make no mistake, claiming that always using semis is going to "save you from ASI" is pointless. It does nothing to educate people on the real pitfalls of ASI and essentially is nothing but FUD.

0

u/drobilla Feb 08 '11

Examples where lacking semicolons can cause problems are well-known and used by those who disagree with you. You are beating on a straw man anyway, nobody claims that using semicolons is going to solve all the related problems - but it solves some.

Can you provide an example where using explicit semicolons causes problems but omitting them does not?

2

u/sjs Feb 08 '11 edited Feb 08 '11

Examples where lacking semicolons can cause problems are well-known and used by those who disagree with you.

Besides lines beginning with ( and [ I bet you cannot name one. Because that's the only one.

I don't have to provide examples where using semis causes problems because I never made that claim. That would be a ridiculous claim.

edit: I can't believe I'm getting sucked back into this. Go ahead and spread your misinformation then, I can't reason someone out of an idea that they weren't reasoned into.

0

u/drobilla Feb 08 '11

In other words, no, you can not. QED.

2

u/sjs Feb 08 '11

Your lack of logical reasoning is remarkable. Yes, you win. Uncontested. Good job, 10 internets to you sir.

0

u/[deleted] Feb 10 '11

[deleted]

2

u/sjs Feb 10 '11

Fear mongering about ASI is bullshit. It's not a "weird personal obsession" about semicolons, it's a "weird personal obsession" with the truth. If you tried to tell me that dinosaurs lived on Earth 6000 years ago I'd react the same way.

I'd also like to point out that I've mentioned the single case where you need semicolons, and nobody has been able to identify any others (because there aren't any). I'm not the one hand-waving here.

Forget it, I can't believe I'm still biting.

-5

u/neoform3 Feb 06 '11

The tragedy of Javascript is that underneath the awful veneer is a pretty elegant language

Where everything is globally scoped. Ugh.

1

u/drobilla Feb 08 '11

True, the scope thing is pretty... wow. I suppose it'd be more accurate to say underneath there is a language that is very close to being a pretty elegant language...