phpBB

Development Discussion Board

phpBB's testing ground of bleeding edge code
Advanced search

[RFC] Coding guidelines: var keyword in js per line

Publish your own request for comments or patches for the next version of phpBB. Discuss the contributions and proposals of others. Upcoming releases are 3.1/Ascraeus and 3.2/Arsia.

[RFC] Coding guidelines: var keyword in js per line

Postby Oleg » Mon Jun 25, 2012 6:42 pm

Each line in JS that declares variables should have its own var keyword, in order for diffs to not change irrelevant lines.

See also: http://benalman.com/news/2012/05/multip ... avascript/
Oleg
3.1 Release Manager
3.1 Release Manager
 
Posts: 1150
Joined: Tue Feb 23, 2010 2:38 am

Re: [RFC] Coding guidelines: var keyword in js per line

Postby Senky » Mon Jun 25, 2012 6:52 pm

+1
Senky
Registered User
 
Posts: 222
Joined: Thu Jul 16, 2009 4:41 pm

Re: [RFC] Coding guidelines: var keyword in js per line

Postby Erik Frèrejean » Mon Jun 25, 2012 6:54 pm

Kinda surprised that this isn't already the case.
Available on .com
Support Toolkit developer
User avatar
Erik Frèrejean
Registered User
 
Posts: 207
Joined: Thu Oct 25, 2007 2:25 pm
Location: surfnet

Re: [RFC] Coding guidelines: var keyword in js per line

Postby callumacrae » Mon Jun 25, 2012 7:21 pm

I also suggest that in the name of a nicer diff, we use the following syntax for objects:

Code: Select all
var obj = {};
obj.foo = 'a';
obj.bar = 'b';


In fact, pretty much all of the relevant arguments can be applied to objects.

Let's take it a step further. I reckon that we should use the following for arrays:

Code: Select all
var ary = [];
ary.push('a');
ary.push('b');


It's only a few extra characters, and it makes it way easier to reorder array entries, no?


Seriously though, I am against this change. It isn't exactly DRY, is it?

What I wouldn't be against is the change suggested at the end of the article:

Code: Select all
var foo, bar;
foo = 1;
bar = 2;
"In JavaScript, there is a beautiful, elegant, highly expressive language that is buried under a steaming pile of good intentions and blunders"
—Douglas Crockford

View my MOD, phpBB Mobile
User avatar
callumacrae
Website Team
Website Team
 
Posts: 881
Joined: Tue Apr 27, 2010 9:37 am
Location: England

Re: [RFC] Coding guidelines: var keyword in js per line

Postby Erik Frèrejean » Mon Jun 25, 2012 7:49 pm

callumacrae wrote:What I wouldn't be against is the change suggested at the end of the article:

Code: Select all
var foo, bar;
foo = 1;
bar = 2;

That really doesn't help a lot, the first line still changes for no reason. That last example shows completely different behavior, that speaks of the difference whether a variable gets an assignment or not.
Available on .com
Support Toolkit developer
User avatar
Erik Frèrejean
Registered User
 
Posts: 207
Joined: Thu Oct 25, 2007 2:25 pm
Location: surfnet

Re: [RFC] Coding guidelines: var keyword in js per line

Postby wGEric » Mon Jun 25, 2012 7:54 pm

You could always use JS Lint or JS Hint to catch when a variable would become global.

callumacrae wrote:I also suggest that in the name of a nicer diff, we use the following syntax for objects:

Code: Select all
var obj = {};
obj.foo = 'a';
obj.bar = 'b';


In fact, pretty much all of the relevant arguments can be applied to objects.

Let's take it a step further. I reckon that we should use the following for arrays:

Code: Select all
var ary = [];
ary.push('a');
ary.push('b');


It's only a few extra characters, and it makes it way easier to reorder array entries, no?

This would make Javascript files larger.

Code: Select all
(function() {
var obj = {};
obj.foo = 'a';
obj.bar = 'b';

var ary = [];
ary.push('a');
ary.push('b');
})()


Old version: 115 characters
New version: 77 characters
Saved: 38 (result is 66.9% of original)
Code: Select all
(function(){var a={};a.foo="a",a.bar="b";var b=[];b.push("a"),b.push("b")})()


Code: Select all
(function() {
var obj = {
'foo' : 'a',
'bar' : 'b'
};
var ary = [
'a',
'b'
];
})()


Old version: 91 characters
New version: 51 characters
Saved: 40 (result is 56.0% of original)
Code: Select all
(function(){var a={foo:"a",bar:"b"},b=["a","b"]})()


callumacrae wrote:What I wouldn't be against is the change suggested at the end of the article:

Code: Select all
var foo, bar;
foo = 1;
bar = 2;

The diff would still show two lines being changed and IMO would make it easier to accidentally make global variables.
Eric
wGEric
Registered User
 
Posts: 506
Joined: Wed Jun 11, 2003 2:07 am

Re: [RFC] Coding guidelines: var keyword in js per line

Postby callumacrae » Mon Jun 25, 2012 8:27 pm

No need to worry about global variables, strict case is enabled and so global variable assignments should throw errors.
"In JavaScript, there is a beautiful, elegant, highly expressive language that is buried under a steaming pile of good intentions and blunders"
—Douglas Crockford

View my MOD, phpBB Mobile
User avatar
callumacrae
Website Team
Website Team
 
Posts: 881
Joined: Tue Apr 27, 2010 9:37 am
Location: England

Re: [RFC] Coding guidelines: var keyword in js per line

Postby Oleg » Mon Jun 25, 2012 9:50 pm

Array and hash initialization is an entirely separate topic and not relevant here.
Oleg
3.1 Release Manager
3.1 Release Manager
 
Posts: 1150
Joined: Tue Feb 23, 2010 2:38 am

Re: [RFC] Coding guidelines: var keyword in js per line

Postby callumacrae » Tue Jun 26, 2012 4:02 am

It was also an entirely sarcastic post to demonstrate the absurdity of what you're requesting :-)
"In JavaScript, there is a beautiful, elegant, highly expressive language that is buried under a steaming pile of good intentions and blunders"
—Douglas Crockford

View my MOD, phpBB Mobile
User avatar
callumacrae
Website Team
Website Team
 
Posts: 881
Joined: Tue Apr 27, 2010 9:37 am
Location: England

Re: [RFC] Coding guidelines: var keyword in js per line

Postby Oleg » Tue Jun 26, 2012 4:58 am

Your sarcastic post failed to make any sort of a relevant point.

I was going to reply to the rest of your post, but:
callumacrae wrote:to demonstrate the absurdity of what you're requesting :-)

I am tired of you spewing assertions such as this, in tone that suggests that you are some kind of authority on the issue you speak about, when most of the time you are either clueless or blatantly wrong. Until you fix this I simply will not be having conversations with you.
Oleg
3.1 Release Manager
3.1 Release Manager
 
Posts: 1150
Joined: Tue Feb 23, 2010 2:38 am

Next

Return to [3.x] RFCs

Who is online

Users browsing this forum: No registered users and 10 guests