Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Style question: bracket notation vs dot notation #9

Closed
samreid opened this issue Nov 1, 2013 · 5 comments
Closed

Style question: bracket notation vs dot notation #9

samreid opened this issue Nov 1, 2013 · 5 comments

Comments

@samreid
Copy link
Member

samreid commented Nov 1, 2013

In gravity and orbits code, I am seeing several instances that use the bracket notation for property access:

model[currentObj + 'Position'] = model[currentObj + 'Position'].timesScalar( 1.0 / scale ).plus( model[currentObj + 'Velocity'].timesScalar( dt ).plus( model[currentObj + 'Acceleration'].timesScalar( dt * dt / 2.0 ) ) ).timesScalar( scale );

instead of the dot notation, here is the above rewritten with dot notation:

body.position = body.position.timesScalar( 1.0 / scale ).plus( body.velocity ).timesScalar( dt ).plus( body.acceleration ).timesScalar( dt * dt / 2 ).timesScalar( scale );

The latter (dot notation) is more consistent with the style we use throughout the rest of our code base, and it seems to me a bit easier to read. Is there a good reason to use the bracket notation? Should the sim be converted to use dot notation?

@samreid
Copy link
Member Author

samreid commented Nov 1, 2013

It looks performance characteristics are about the same on many platforms, with a notable speed improvement on Safari: http://jsperf.com/dot-notation-vs-bracket-notation/2

This amount of speed increase for safari may be in the noise compared to more expensive steps like drawing.

@samreid
Copy link
Member Author

samreid commented Nov 1, 2013

The most upvoted answer in Stack Overflow's "JavaScript property access: dot notation vs. brackets?" question concludes:

Dot notation is faster to write and clearer to read.
Square bracket notation allows access to properties containing special characters and selection of properties using variables

http://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets

@jbphet
Copy link
Contributor

jbphet commented Nov 4, 2013

I agree that dot notation is more readable, so I would like to see us standardize on it and only use bracket notation when it is necessary to do so.

@jonathanolson
Copy link
Contributor

Your jsperf didn't include the string concatenations (and thus JIT dynamic lookups that in the jsperf can be compiled to static references) that are going in in the above code. I think the dot notation will be faster in that specific case, and I prefer the dot notation style.

@pixelzoom
Copy link
Contributor

+1 for dot notation. I especially don't like the style of bracket notation that uses string concatenation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants