by jashkenas · 14y ago · 0 comments
Between semicolon-free folks and JavaScript traditionalists, who gets to play the role of the expert? If Node.js is popular, does that mean that Isaac's comma-first style is "correct"? (https://gist.github.com/357981)
In the absence of a quantitative engineering method with which to evaluate either approach, isn't it a purely personal and political choice?
You're framing this as a disagreement between equals. It is not. There are degrees of expertise. (Total lines of production code * amount of usage) is a reasonable approximation, and those who rank highest on this scale are overwhelmingly in favor of requiring semicolons.
If Carmack and Torvalds were in agreement on a C best practice while Odesker^W^W^W^W^W^W^W Topcoder user #12571 said to do something else, I wouldn't go with the latter.
Google, responsible for a JS engine, multiple compilers that target JS, and what is likely to be the single largest JS codebase of any company: http://google-styleguide.googlecode.com/svn/trunk/javascript...
Brendan Eich: "The moral of this story: ASI is (formally speaking) a syntactic error correction procedure."
Or you can go with the Node.js guys.
If you are doing a Topcoder round, then the random Topcoder user's advice is probably better than Carmack's. Carmack or Torvalds would probably tell you to use a variable name that gives a lot of information about its purpose, so that other people will find it easier to read your code. The Topcoder user might tell you to use a cryptic abbreviation that helps you remember a variable's purpose but clues little to the outside reader, since Topcoder users are penalized for allowing others to find their bugs. When implementing an algorithm, Carmack or Torvalds would code it as part of an extensible system with an API so that it can be used for many purposes. The Topcoder user might code it with no potential for reuse, but would do it in the way easiest to code quickly without making a mistake.
Right, because what could those experts possibly have to teach you? Fuck that intellectual crap, brogrammers are here to crank out some code yo! Just because those dinosaurs have spent "thousands of hours" studying and trying out different programming styles and concepts means I should listen to them? This is the internet age man. Some guy in a github comment told me different and github is cool.
It's like those functional programming professors who try and talk about how continuation passing style was a bad road to go down because of maintenance problems and how difficult refactoring and just reasoning about code becomes in large systems.
Fuck that egghead noise, node.js has callbacks and node is how you get webscale.
I'm right with you bro!
The point of your parent was "don't let the experts think for you", not "screw the experts" - that's a very important distinction. You should form your own opinions, not just uncritically adopt the opinion of some expert. It's unquestionable that expert knowledge and experience is important - but nothing beats forming your own (hopefully informed) opinion.
And google is well known for terrible quality javascript. http://www.sitepoint.com/google-closure-how-not-to-write-jav...
And finally, Crockfords reasoning about things like this seems based on the presumption that js developers are drooling idiots. For example, he recommends against using the new keyword for object creation in javascript, even though object factories can often be a huge waste of memory, because dumb javascript developers may forget to use the new keyword at times when they want to instantiate objects.
I am not saying you shouldn't listen to smart people, but I am saying that you should actually learn the reasons they say the things they do, and use your own brain when making choices.
Error correction simply means "add a semicolon if it's necessary."
He says it should be thought of as an error correction feature.
That is: do not in any case rely on ASI. Add your semicolons and only let ASI do it works when you do an actual ERROR and forget to add one.
If you look at the language Tom objects to the most, it's language Isaac uses that promotes this tribal attitude that the other side doesn't have your best interests at heart, and questions their motives.
We're just terrible at this, and I'm not sure any human society has ever really cracked it.
The tribal attitude is the enemy of this process for sure, and humans are very prone to that attitude but that doesn't mean you can't still find the answer more likely to be right.
Omitting semi-colons, however, can cause novice JavaScript programmers serious grief. Nevermind novice JavaScript programmers – they cause me grief, and I put semi-colons everywhere! That's because I'm often called in to debug our customers websites, and on several such occasions I've had to fix bugs that never would have occurred if that customer used semi-colons properly.
Sure, if our customers understood JavaScript's ASI and how to use semi-colons "properly", I wouldn't have this problem either. Except, as Tom points out, the reality is that almost none of these people will invest in doing so.
Really?
The comma first style makes it less likely that your list will end with a trailing comma, which can cause IE-specific bugs. If you're trying to track down the IE breakage a couple of weeks after you wrote down the code, it can be fun to track down the extra comma. If you don't know that this bug is possible (as I'm guessing you don't), multiply the hair-pulling by 10.
See the cautionary story in http://www.enterprisedojo.com/2010/12/19/beware-the-trailing... for a real life example.
With the comma first style, that bug would never have been introduced. Neat, huh?
If you run any syntax checking or minification tools, this will be immediately obvious. If you test your site in IE at all, this will be immediately obvious. If you record JS errors on your site, this will soon be obvious. If you record any kind of usage statistic, this will eventually be obvious.
Take it from someone who has experimented with cutesy formatting in the past[1]: you don't need to use cutesy formatting to solve this problem.
[1 | You can end class declarations with _:0} instead of a bare } to avoid the trailing comma issue. ]
Having mechanisms in place to catch programmer errors is no excuse for adopting practices that make programmer errors more likely.
Lists usually grow on the end, not the beginning.
(I use this kind of formatting for SQL, for a similar reason.)
It is an IE bug that causes it to break. One of the many ways they do not follow the spec.
Stylistically, I side with the no semicolon people. Grammatically they're redundant.
Pragmatically, if you ever find yourself saying "read this enormous, hard to read spec just so you can identify the three or four edge cases that will burn (yet remain statistically likely to commit)" you are on the losing side of history.
Basically, it's preposterous to have reasonably smart people seriously argue that the option that is least user friendly and most likely to cause pain down the road is the best one out of a sense of misguided spec puritanism.
Edit: to use an analogy, imagine if all vehicle development was treated with the same terminology. Whether you're building helicopters, spacecraft, mass produced commuter cars, RC toys, nuclear powered submarines, formula 1 race cars, or tunnel boring machines: all of it is just vehicle manufacturing right? Just as anyone would find it ridiculous to put forward advice that could possibly apply across all of those distinctly different disciplines we should view advice about software with the same skepticism.
>"Best practices" for development of an iphone game may not be the same as for flight software for an orbital rocket or for an international banking backbone.
I disagree. There are a few things that should be common across all of these, like not storing passwords in plain text.
No.
Here's one idea: perhaps everyone's opinion is not equal on every subject. Perhaps by looking at the opinions of respected accomplished people who have garnered the respect of other respected accomplished people you can being to see who's opinion should get more weight. This is the process that came up with the very ideas of things like "engineering" and "quantitative".
I know we love the young upstarts disrupt the old way of thinking meme. That's why you framed it this in that way with the folks vs the traditionalists. You might want to remember that historically the young upstarts have been wrong 99% of the time. That's why it's news when they are right.
"One man's best practice is another man's anti-pattern"
And in the vast majority of cases, when you ask that question about a specific problem: One of those men is wrong. Even if they're both smart. But how to tell? If a huge number of other smart and experienced people have been convinced by one of them ... well I'd go with that guy. You won't be right all the time but it's the best system we've come up with so far for things that don't conform to double blind placebo controlled studies or mathematical proofs. It literally is the best way to be right most often.
This reminds me a lot of abstraction in software systems. For 95% of cases, the abstractions make things quicker in the end. For those 5% of edge cases which the abstractions don't cover properly, you spend inordinate amounts of time trying to get things to fit. The bigger point here is that trying to white-wash anything with a particular methodology is both the right and wrong thing to do. It's right because most of the time it will work, it's wrong because when it doesn't work, you will have a hell of a time dealing with it.
The best system we have come up with so far is actually to take things on a case-by-case basis instead of trying to solve everything in one fell swoop. The problem is that one must be mindful as consistently as possible to achieve this. Most people are not capable of keeping that line of focus for an hour, let alone a day or their whole lives. Funnily enough, the solution to this is meditation, not a set of best practices or expert opinions.
This will answer the question, "who is best at arguing their point". It does not reliably answer, "whose point is correct?"
That's why this system relies on convincing smart, educated people and not just people in general. The need for intellectual discourse to avoid this exact problem and what that looks like is taught as part of being educated.
To me, consistency is important. All of my codebase is traditional comma postfix, so I'll keep using that; however, maybe with some new projects I'll try on the comma prefix pants and see if it helps.
Thanks for the link!
To me
, consistency is important
. All of my codebase is traditional comma postfix
, so I'll keep using that
; however
, maybe with some new projects I'll try on the comma prefix [...]
... using typography in a typographically sensible way is far more important to readability than an arbitrary "consistency" of punctation. We're humans, not computers, after all.They're always correct, they copy without any editing, and the "var"s line up to show that it's a block of assignments the same as commas or whitespace.
var foo = 1,
bar = 2
baz = 3
In 90% of the cases the code will run the same whether or not there is a comma on line 2, but in the other 10% you will get burned in odd ways now that `baz` is a global variable.It's so ugly to me that the whole idea seems like it could be a troll.
To me, consistency is important
. All of my codebase is traditional comma postfix
, so I'll keep using that; however
, maybe [...]
Which is more confusing than either of our consistent examples. Unless I misunderstood your point. And let me play the advocatus Dei
, because that actually looks quite readable
.
Though, more seriously, using English (a natural language) to try to make an
appeal to absurdity about the format style of a formal language, is plain
absurd.What convinced you? I read it and saw something wildly different, which is best reserved for a good reason. The reason appears to be visual recognition of delimiter mistakes that the parser will catch anyway.
The gist contains examples of various mistakes in both styles. Most of the errors are syntax error which will be immediately rejected by a parser. While the comma first errors are generally more obvious, they are silently bad by unexpectedly returning undefined. The standard "var" example is silently bad by leaking vars into the global scope when chaining initializers on a single "var". My conclusion from these samples is that comma prefix formatting only practically helps to prevent hidden mistakes in chained "var"s. Putting each variable declaration on a line of its own is far less distracting than reformatting every list and object.
Sample comma first style error. This seems like a strange thing to do, but I'm unfamiliar with this style.
return
{ a : "ape"
, b : "bat"
} // returns undefined,
// then creates a block with two named statements.
Sample standard style error: var a = "ape eat banana",
b = "bat, allowed to fly",
c = "cat toy",
d = "dog chasing the mailman,"
e = "elf lord",
....
// leaks "e" into global scope return
{ a : "ape",
b : "bat"
}
The error here has no relation to the commas.In the absence of a true quantitative engineering method, there is a middle ground other than personal choice: common anecdotes. Presumably, those shameful leaders have heard enough stories about the issue to make a reasonable recommendation, sort of like advising against shipping server software with an open default security strategy even though I don't have any peer-reviewed studies saying such is unwise.
No. Isaac doesn't define what style node.js is written in. And in an interview when he took on the lead developer role on he affirmed that he would abide by their current coding preference, which wasn't comma-first. That's Isaac in a nutshell, he works with the developers around him.
You may be mistaking Node.js with npm. npm is Isaac's code, and that is comma-first I believe. It's his project, and his rules, and his call as to what coding style he wants. People who participate in his projects either accept that, or walk away. Node.js is an existing project, so the existing coding preferences are in place -- unless, I guess, there's a general consensus that the coding preferences in place are causing an issue.
The only problem with this approach is the diffs between the repo and your working copy.
My advice? Don't work with people whose best practices are your anti-patterns. Or at least strive to work with people who agree with you on a core set of principles. Or hell, work with people who agree with you on nothing. Just don't complain when you can't work with any of them. :-)
And besides that, what's wrong with "purely personal and political choice[s]"? Being that I'm the person who's writing my code, I'm allowed to make a certain number of purely personal choices. That is, as long as I take into account the political consequences of doing so. Put another way, it's ok to have personal preferences, but you have to take other peoples' feelings into account too.