Last active
December 14, 2015 22:49
-
-
Save ratbeard/5161422 to your computer and use it in GitHub Desktop.
{} syntax is more dangerous than no-;
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I thought of a good analogy to 'no ;' are bad argument: | |
~~~ | |
I can recall getting bitten by 'no ;' once, but LOTS of times I've | |
gotten the dangling ',' in object literal bug: | |
this.options = { a:1, b:2, } | |
Modern browsers allow it so you typically don't spot it late in the game when ie7 | |
throws a cryptic error message at you. This leads to the conclusion that object | |
literals are unsafe and lead to bugs that waste developer time. Instead, this | |
form is safer and more robust, and should be taught to new js developers: | |
this.options = new Object(); | |
this.options.a = 1; | |
this.options.b = 2; | |
This rule is just intrinsically easier to learn than the "no dangling ," rule. | |
~~~ | |
Also, on rare occasions you need property names with spaces in them, e.g.: | |
this.options["Cambrian Blue"] = "smack shack"; | |
To not mentally tax devs with different syntax for different circumstances, | |
lets just always use this one consistent form: | |
this.options = new Object(); | |
this.options["a"] = 1; | |
this.options["b"] = 2; | |
Even though some may argue this code is not as readable, its safer, more | |
consistent, and therefore always better than: | |
this.options = {a:1, b:2} | |
~~~ | |
Object literal actually has a further danger - old engines will error if a | |
reserved word, e.g. "new" or "default" is used as an unquoted key name. | |
Crockford made the call that JSON keys must all be quoted based on this JS | |
grammar mistake. | |
So even with these two dangers -- which are ESPECIALLY bad since they vary | |
across browsers -- people use {} all the time because its a convention. | |
But that in itself doesn't mean its a safer or better approach. | |
Early JS devs came from languages like c++ and java where ';' were required. I | |
imagine that, and the somewhat subtle ';' insertion semantics (though in my | |
experience ONLY matter in the self-executing-function case) lead to ending | |
with ';' being the convention. When in doubt its great to follow a convention, | |
but it doesn't mean its a safer solution. | |
I find ';' unnecessary and make the code slightly harder to read, so I don't | |
use them. If I'm on a project that does use them, I try to follow the | |
conventions of the project. Though one reasons I stopped using them was I | |
noticed unless you automate checking for missing ';'s (e.g. upon saving a file | |
in your editor or code checkin), you're probably missing lots of ';'s anyways. | |
Sidenote: finding dangling ',' is the only really useful part of js-lint I've | |
found. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment