Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Core JS files mixup many declarations way:
var i, j;
var i, j = 'xyz';
var i;
j = 'xyz';
Suggestion from crockford:
(http://javascript.crockford.com/code.html)
It is preferred that each variable be given its own line and comment. They should be listed in alphabetical order.
var currentEntry; // currently selected table entry
var level; // indentation level
var size; // size of table
example from jQuery source:
var options, name, src, copy, copyIsArray, clone,
target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false;
My suggestion:
var i, j; // keeps all no assigned vars in single line
var k = 'xyz' // assigned vars in single line
var foo = 'xyz' //
Comments
Comment #1
nod_I brought up the subject in #1089300: Clean up drupal.js. Might want to cross-post to get some more people here.
I agree with this.
Comment #2
sunComment #3
sunRegardless of what we decide on:
Inline comments have to be located above the remarked line, not behind/next to it on the same line.
Comment #4
jhodgdonComment #5
droplet CreditAttribution: droplet commentedSo far, there's 2 options.
Option 1:
Cons:
- waste more spaces, eg:
Option 2:
Cons:
- 2 steps to assign a value, eg:
var i, j;
to
Comment #6
RobLoachjQuery has already figured this out for us. We use jQuery, so lets go with what they recommend, but with two-spaced whitespace of course:
Comment #7
nod_The issue i'm seeing there is people not putting "," or ";" at the right place and create leaking vars like crazy. Misplacing a ";" won't crash the thing like in php, it'll just be silent. Unless we force
"use strict";
It would be ideal but too error prone.
Comment #8
sunIt's way too pedantic to enforce a rule for how to write/define variables. All of the options are perfectly legit.
Enforcing alphabetic sorting, separate
var
s on separate lines, and whatnot is way too specific. Code should always be written in a way that makes sense within the context and for the particular goal.Coding standards should define how to write code, if you write it in a certain way. Only in very special edge-cases, in which it may technically, organizationally, structurally, or politically matters, coding standards may ask to write code in a certain way. But that's clearly not the case here.
For example, coding standards should say that, if you declare multiple variables on one line, then the line should not exceed 80 chars in total (with commonly known exceptions to the rule). They may also recommend that you start a new line of variable declarations instead of continuing indented on the next line. But they should also clarify how to write the code when continuing to the next line. They may also say that declarations that need a comment should be written as separate variable declarations, so it is clear to which declaration the comment refers to.
Comment #9
nod_At the end of the day, I agree. Let's just make the other issues about js doc move forward.
Comment #10
droplet CreditAttribution: droplet commentedumm..in Drupal World, I'm not sure.
CSS enforcing alphabetic sorting, even enforcing color hex to lower case. we have another issue enforing else if to elseif #1509838: Enforce "elseif" standard.
In my views,
It's closed to CSS.
we suggested this
But not
#forum td.posts, #forum td.topics, #forum td.replies
It's more consistency and reducing the changes during each patches.
for example, I setup my IDE format rule to Option 1. .. used in a option 2 file. it will be a lot of changes in the patch.
Comment #11
sunClosing might have been a bit premature, yes.
At the very least, the docs could point out the high-level coding style parameters, similar to those I gave as examples in #8.
Comment #12
pounardAgree with #8 and #9 here. My real opinion is that we should probably follow JSLint, but not everyone seems to want it, so I'd prefer recommendations over strictness as #8 says.
Comment #13
seutje CreditAttribution: seutje commentedfeels silly to put var in front of everything, the first "var" is like "here be var statements till ye hit a semicolon"
but it also feels weird to put all no-assignment vars on a single line, isn't that what we have minification for? I usually place my no-assignment ones last, but all on a separate line
I would, however, prefer to set the indentation to variable name, like so:
But this is probably not the most readable format to everyone.
Comment #14
nod_See #9, this is not a useful issue.
Comment #15
seutje CreditAttribution: seutje commentedSo can we at least agree on a recommendation to put in the documentation, because right now it's all random and confusing a lot of people.
Comment #16
seutje CreditAttribution: seutje commentedDoes anyone have any objection to putting a recommendation in the doc like the following:
Variable declaration
It is not recommended not to mix assignment and no-assignment variables under a single
var
statement.Additionally, it is preferred to use multiple var statements in combination with new-lines to avoid having variables accidentally leak into the global scope.
Comment #17
nod_All good for me, maybe add a sentence about comments that should be above the variable, not after, on the same line.
Comment #18
seutje CreditAttribution: seutje commentedI'm fine with adding that, but the docs already state
Comment #19
nod_oh ok then, don't mind me :)
Comment #20
nod_Added #16 to #1778828-7: [policy, no patch] Update JS coding standards
Thanks :)