JavaScript precedence for dummies
February 20, 2012 7:23 AM   Subscribe

I have a hard time remembering operator precedence rules in JavaScript. Is there anything wrong with using parentheses to force the precedence I want so I don't have to remember the rules?
posted by markcmyers to Computers & Internet (12 answers total) 2 users marked this as a favorite
Best answer: I don't write JavaScript - mostly Java, some Python - but I'm a huge fan of writing code to be as explicit and readable as possible, even at the expense of (gasp!) a few wasted characters here or there. Now, there's a reasonable case to be made that my users aren't downloading my code every time they run it, which gives me a different perspective on such things, but when in doubt, I'll always carry the banner of Readability Now, Readability Forever.
posted by Tomorrowful at 7:32 AM on February 20, 2012 [1 favorite]

Best answer: Nothing wrong with that at all. Some people prefer it so that it is obvious to everyone reading the code what order of operations was intended.
posted by toomuchpete at 7:32 AM on February 20, 2012

Best answer: I actually prefer people using parens - it's a much clearer, more explicit method of programming.

There's also a few precedent mnemonics:
NATO - NOT, AND, then OR
posted by unixrat at 7:37 AM on February 20, 2012

Best answer: This is a recommended practice in >Practical C Programming. In fact, the advice is featured in the blurb for the book.
posted by kindall at 7:38 AM on February 20, 2012

Best answer: Personally, I think that depending on operator precedence rather than explicit use of parentheses makes it more difficult for other programmers reading your code and is setting yourself up for mistakes... it's sort of like those perl scripts that do amazingly complex operations in a single line but are impossible to read-- yes, you can do it if your programming-fu is strong enough, but it doesn't really help anyone.
posted by deanc at 7:44 AM on February 20, 2012

Best answer: There is nothing wrong with writing working code.
posted by Mad_Carew at 7:53 AM on February 20, 2012

The only problem is when you have to read other peoples code who were not fans of parenthesis.
posted by Land Ho at 8:39 AM on February 20, 2012

If you're using closure compiler or something similar it'll strip out the extra parens anyway so go for it.
posted by klanawa at 9:44 AM on February 20, 2012

I completely disagree with the advice given in this thread. You can do whatever you want as long as everyone working on the project agrees to do the same thing. You want to adopt a style guide such that any two developers working on a project would end up writing the same statement, more or less, for any given bit of logic. To that end you have two choices: only use parens when you need to override operator precendence, or always use parens everywhere. If your style guide is "use parens when you think it makes things easier" then you have a big grey area. Operator precedence really isn't that hard, and if you go minimal for two weeks I promise it won't be hard to look at anymore. If you're writing such complex logic that you think you need parens to make it more readable, then break it up into more than one line with well-named variables.
posted by jeffamaphone at 11:09 AM on February 20, 2012

There is nothing wrong with writing working code.

That is a dangerous attitude. while (foo == null) { sleep(100); } works, but is completely the wrong way to do synchronization and if you check it in to my code base I will roll back your change.
posted by jeffamaphone at 11:11 AM on February 20, 2012

The choice of whether or not to use parentheses to avoid ambiguity is completely orthogonal to whether those rules need to be learned -- using parentheses does not get you off the hook for not knowing them, or at least knowing where to look them up. For example, you might encounter some code that you didn't write that doesn't use parentheses and say "hey, I should add parens there so it's more clear", but to do so properly requires knowing the operator precedence rules or else you'll introduce a bug. Also, it's common to have to debug JS that's been minified or otherwise machine-generated, and such code is likely to lack everything extraneous.
posted by Rhomboid at 1:30 PM on February 20, 2012

Write yourself a cheatsheet, pin it up where you can glance at it, and you'll learn it soon enough.
posted by theora55 at 6:36 PM on February 21, 2012 [1 favorite]

« Older Sci-fi written by women in the 2000s?   |   I miss my old friends Newer »
This thread is closed to new comments.