Comments on: Ordering variable declarations https://j11y.io/javascript/ordering-variable-declarations/ Sun, 22 Mar 2015 15:39:22 +0000 hourly 1 https://wordpress.org/?v=5.0.13 By: Martin Varesio https://j11y.io/javascript/ordering-variable-declarations/#comment-2373 Sun, 22 Apr 2012 20:10:44 +0000 https://j11y.io/?p=2020#comment-2373 Myself, I like to define in largely alphabetical order, usually with simple values first, then calculated values (2 alphabetized lists).

]]>
By: Ross Hadden https://j11y.io/javascript/ordering-variable-declarations/#comment-2372 Sun, 11 Mar 2012 18:15:33 +0000 https://j11y.io/?p=2020#comment-2372 I also recite the alphabet aloud up to the letter I am trying to figure out.

]]>
By: Joe Zim https://j11y.io/javascript/ordering-variable-declarations/#comment-2371 Wed, 07 Mar 2012 04:44:11 +0000 https://j11y.io/?p=2020#comment-2371 I’ve never even heard of anyone saying that you should order your variables alphabetically. My guess is that it was someone who was a bit OCD and was well known enough that when he said “order them by alpha” that everyone thought it was convention.

]]>
By: Vasili https://j11y.io/javascript/ordering-variable-declarations/#comment-2370 Sat, 03 Mar 2012 18:25:16 +0000 https://j11y.io/?p=2020#comment-2370 I run into the same problem you do when I try to alphabetize either my JavaScript or PHP variables: some variables rely on previously declared ones.

I’m a little anal about alphabetizing stuff so I still do it for CSS properties because their order doesn’t matter. But CSS is a completely different story because I have my own weird pattern for that.

An interesting post nevertheless, especially because, like you mentioned, it’s hard to find that balance between readability and functionality. Oh the problems us programmers face!

]]>
By: WebManWalking https://j11y.io/javascript/ordering-variable-declarations/#comment-2369 Fri, 02 Mar 2012 23:21:05 +0000 https://j11y.io/?p=2020#comment-2369 If you were the last person to work on a particular code file, you will almost certainly be the person your boss assigns to work on it again when there’s a problem to be fixed.

Therefore, you should code for your own readability first and foremost. Myself, I like to define in largely alphabetical order, usually with simple values first, then calculated values (2 alphabetized lists).

But that’s me. You should suit yourself.

]]>
By: Jamie https://j11y.io/javascript/ordering-variable-declarations/#comment-2368 Fri, 02 Mar 2012 19:33:04 +0000 https://j11y.io/?p=2020#comment-2368 Me four. Not seeing the point. With so many *real* style issues to be concerned with, I think this is best left to the same people who feel the need to alphabetize their bookshelves and square their stapler and tape on their desk. The effort required to maintain is more than the benefit.

Anyway I can’t help but feel that if you had so many variables that you needed to sort them to find something easily, you probably have bigger problems with the code than the order of your declarations. And of course what the previous commenter said – search.

I usually try to order things in some way that makes them readable, e.g. undeclared ones first, then simple assignments, then longer lines. This is mostly just about not have lines of dramatically different length together, which (imho) hinders readability.

var i,j,x,y,
    me=this,
    tryLimit=10,
    success=false,
    options=core.getCurrentOptions();
]]>
By: Dan G. Switzer, II https://j11y.io/javascript/ordering-variable-declarations/#comment-2367 Fri, 02 Mar 2012 14:19:16 +0000 https://j11y.io/?p=2020#comment-2367 Alphabetically ordering doesn’t fly with me either–mostly for the reasons you’ve already outlined.

Also, maybe it’s just me, but I rarely visually search for variables. I use my IDE/text editor to find them. A good editor provides keyboard shortcuts to navigate between the currently selected word/variable and can highlight all instances of the variable. So if the key argument is it makes them easier to find, I’d disagree. Your editor should make them easy to find (and most do.)

]]>
By: Sarfraz Ahmed https://j11y.io/javascript/ordering-variable-declarations/#comment-2366 Fri, 02 Mar 2012 13:47:16 +0000 https://j11y.io/?p=2020#comment-2366 I agree with you that variables should be put together section/module-wise. I don’t see any benefit of ordered declaration. What I see though is extra bit of time wasted to do so.

]]>
By: olvado https://j11y.io/javascript/ordering-variable-declarations/#comment-2365 Fri, 02 Mar 2012 12:09:09 +0000 https://j11y.io/?p=2020#comment-2365 I don’t see the benefit in alphabetical ordering either – as with CSS properties (I’ve read these should be declared alphabetically too) why would I want to declare padding alongside position?!

Surely the important thing to remember is when working with a team (or on code that will be edited by others at some point) then having a convention between yourselves will alleviate the pain of working with someone else’s code. All singing from the same hymn sheet!

A lot of the rest of his article does make sense (though I prefer 2 space indentations as opposed to 4!). However, as you conclude, it’s surely about programming ability rather than conventions.

]]>