0

In studying Node.js, I've found that most developers tend to not follow the single-var pattern for declaring variables. This also seems to be the case when declaring dependencies in CommonJS/Browserify.

Is there a technical advantage to having multiple var statements in Node and CommonJS compared to "regular" client-side JavaScript, or is it a matter of personal preference? If not, is there a reason why multiple var declarations are more prevalent?

Besides the well-known literature advocating the single-var pattern, the only data I've found on the subject is this jsperf showing better performance using a single declaration.

2
  • There's no reason why the practice should differ between server-side and client side code. It's just a matter of personal preference. Commented Jun 12, 2014 at 1:18
  • Watch this before you refer to the microbenchmarks next time: youtube.com/watch?v=65-RbBwZQdU :-) Commented Jun 12, 2014 at 1:20

2 Answers 2

1

Crockford says that you should use the 'single var pattern', while Felix says otherwise.

I prefer to not use it, as it is more readable and easier to read. There are no technical advantages in any cases, just use the one you prefer.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks - wasn't aware of any prominent Node coding styles. Would be nice if Felix explains why it's okay to deviate from Crockford (especially for declaring "deeper inside a function"), but that guide does seem pretty authoritative.
"deeper inside a function"?
That quote is from the updated version: github.com/felixge/…
Oh."deeper inside a function" is a reference to Crockford saying that you should only declare vars on the top of the scope, because of hoisting,while Felix says that you should put where it makes sense.
0

I think that in a case as small as this, Readability > Performance, especially when the performance increase is minimal.

Go with your preference. Personally, I like using multiple vars.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.