Comments (31)
👎
(a) first of all, our code style says, that all function arguments casting and default values assigning must be done at the beginning of the function
I think it looks good:
function addEvent(options) {
var type = Array.isArray(options.type) ? options.type: [options.type];
var context = options.context || null;
}
This explicitly leads to an idea of having variables initialization block.
We should declare variables as close as possible to the place where it's first used. It helps resolve problem with name conflicts. Moreover you can use let
in ECMAScript 6:
{
let x = 1;
}
// x undefined here
As for requirement to declare variables at the point when they're used first time, this is, in our opinion, bad architectural principle. Partly because of (a) rule, since you can't easily check whether every variable is initialized without reading entire function through, but mostly because such code is obviously under-normalized:
I don't want see temporary variables in the begining of function, for example var i, j
. Why you need information about initialization values without other code of the function?
- Now we have simple rule: always use var.
- I am very confused when see code like this:
var a = 1,
b = 2,
f1 = function () {
},
// After 15 lines of code I don't see `var` statement on the screen and I don't understand what happens
f2 = function () {} // global var?
- Your style increases diff every time when I add some line:
var a = 1,
- b = 2;
+ b = 2,
+ c = 3;
from mapsapi-codestyle.
We should declare variables as close as possible to the place where it's first used.
Who said that?
It helps resolve problem with name conflicts.
IDE resolves this problem. Our intent is to make code more readable. Variable declarations are usually very different from code blocks, so mixing them makes code less consistent.
I don't want see temporary variables in the begining of function, for example var i, j.
You may declare temporary variables inside corresponding condition or loop block.
// After 15 lines of code I don't see
var
statement on the screen and I don't understand what
f2 = function () {} // global var?
IDE will notify you if that's a global var.
But otherwise, how will you check that in those 15 lines of code there is no calling f2
function?
Your style increases diff every time when I add some line:
Are we writing code for people or for robots?
That's, in general, is just a problem of git highlighter, and could possibly be fixed.
from mapsapi-codestyle.
Well, your arguments look a bit subjective to me. Whereas the statements about handling function arguments at the beginning and using only initialised variables makes complete sense, the conclusion about necessity of a single var statement at the beginning of a function is far-going.
First of all, I believe single/multiple var
dilemma isn't about any technical arguments, it's just about cosmetic aspects of styleguide. One of questions you just answer with any variant and stick to it for the sake of uniformity.
What about place you define your variables, it has nothing to do with architecture. And since JS is not C89 let's not invent any limitations here.
from mapsapi-codestyle.
I agree that 'initialize-at-the-beginning' rule doesn't explicitly require single var
statement: it just requires some cosmetics to separate declaration block from code, and single var
fits this purpose well.
from mapsapi-codestyle.
Declaration block is the code. Also, you may define a bunch of variables which purpose is only to hold some reference for a while to make a subsequent expression more readable, and hoisting that crap to the beginning of a function will make it only harder to read.
from mapsapi-codestyle.
IDE resolves this problem.
IDE will notify you if that's a global var.
I don't use IDE.
You may declare temporary variables inside corresponding condition or loop block.
Yes, we should declare variables as close as possible to the place where it's first used.
Are we writing code for people or for robots?
Sometimes I need detect who change some line of code.
from mapsapi-codestyle.
Declaration block is the code.
It truly is, but this code block is different from others:
(a) it MUST initialize every variable;
(b) it MUST NOT contain any imperatives, i.e. change program state.
In my view, declaration block and imperative block differs just like an algorithm or equation differs from its initial conditions; simply different parts of program.
Also, you may define a bunch of variables which purpose is only to hold some reference for a while to make a subsequent expression more readable, and hoisting that crap to the beginning of a function will make it only harder to read.
Then you just need to split your code into several functions, aren't you? Could you provide an example of obviously non-splittable and well-written code which introduce such a pattern?
from mapsapi-codestyle.
I don't use IDE.
You're saying this just like you think there is something good in it.
Yes, we should declare variables as close as possible to the place where it's first used.
Who said that?
Sometimes I need detect who change some line of code.
Then you should insist on writing code like
var a = {};
a.optionOne = 'x';
a.optionTwo = 'y';
a.optionThree = 'z';
or like
if (condition1) {
if (condition2) {
if (condition3) {
// code
}
}
}
Why don't you do that?
from mapsapi-codestyle.
(b) it MUST NOT contain any imperatives, i.e. change program state.
I think "must" is a strong word: actually there is no guarantee that you will never have a situation when initialisation of a variable (or even a set on variables) requires some changes to the state of the application.
Then you just need to split your code into several functions, aren't you?
Of course I do. In theory. But, as it happens, reality is a bitch and function calls cost you performance and even sometimes readability. Here is an example:
// ...
// - determine RGB bbox of the block;
for (var m = 0; m < 16; ++m) {
var offset = 4 * ((i + (m >> 2)) * w + j + (m & 3));
if (maxR < pixels[offset]) { maxR = pixels[offset]; }
if (maxG < pixels[offset + 1]) { maxG = pixels[offset + 1]; }
if (maxB < pixels[offset + 2]) { maxB = pixels[offset + 2]; }
if (minR > pixels[offset]) { minR = pixels[offset]; }
if (minG > pixels[offset + 1]) { minG = pixels[offset + 1]; }
if (minB > pixels[offset + 2]) { minB = pixels[offset + 2]; }
}
// ...
Here we have the variable offset
which we use just here, in the body of this loop. And we really don't want to introduce a function enclosing the body of this loop for performance reasons. Moreover, we even don't want to enclose the loop itself to a separate function for the very same reasons since it is a part of large loop body.
It's worth a note that I don't think of this code as generally well-written or anything like that. Usually I try to dislike my code:) But it works, tested, it's, well, sufficiently fast (actually, as fast as it can be in a browser).
from mapsapi-codestyle.
I think multiple var
provides a little bit more freedom in writing code.
If you want to put all variable declarations, you can do it.
If you want to declare variable where it's first used., you can do it.
I vote for more flexible rule — multiple var
.
Multiple var
is really handy. @twirl, just give it a try. You'll like it! 😸
from mapsapi-codestyle.
You're saying this just like you think there is something good in it.
I think that IDE should help programming, but not specify development way.
Who said that?
You in previous comment
I don't want see temporary variables in the begining of function, for example var i, j.
You may declare temporary variables inside corresponding condition or loop block.
from mapsapi-codestyle.
@twirl the codestyle rule The variable should be declared as close as possible to the place where it's first used
is open to interpretation. In my practice, the variable declaration block is always in the beginning of the block statements (that is if you keep away from writing long and confusing blocks), and so I'm not braking the rule.
As for the multiple variable declaration inside a single var
statement - it pretty much depends on the personal aesthetic preferences for each developer. I prefer the per-variable var
statement.
from mapsapi-codestyle.
Here is an example:
@dmikis Your example doesn't contradict the rule discussed: you're declaring a variable inside a block, nobody wants to prohibit that (but I still insist that all such declarations should be done at the beginning of the block).
If you want to put all variable declarations, you can do it.
If you want to declare variable where it's first used., you can do it.
@tarmolov Then why use codestyle at all? My main objection is declaring vars whatever you want (like codestyle states!), single var vs multiple vars is just a minor question here.
I think that IDE should help programming, but not specify development way.
You in previous comment
@ikokostya I don't understand that.
the codestyle rule The variable should be declared as close as possible to the place where it's first used is open to interpretation
@dodev It simply means that rule isn't working.
from mapsapi-codestyle.
By the way, your own importing rule for Node.js contradicts this ‘declare at the point of usage’ rule. Why all ‘require’ statements must be placed at the beginning of function, not the point of first usage?
from mapsapi-codestyle.
@twirl, stop, we were talking about functions and now you are referring to blocks. Which would it be?
from mapsapi-codestyle.
By the way, your own importing rule for Node.js contradicts this ‘declare at the point of usage’ rule. Why all ‘require’ statements must be placed at the beginning of function, not the point of first usage?
Cause it's more like C/C++ #include
than variable declaration.
from mapsapi-codestyle.
Why all ‘require’ statements must be placed at the beginning of function, not the point of first usage?
Cause it's more like C/C++ #include than variable declaration.
Yes, it's module dependencies.
from mapsapi-codestyle.
@twirl, stop, we were talking about functions and now you are referring to blocks.
@dmikis I've been talking since the beginning, ‘We prefer using single var block at the beginning of function or block’.
Cause it's more like C/C++ #include than variable declaration.
Yes, it's module dependencies.
@dmikis @ikokostya
Nope. Nothing in common. ECMAScript6 modules will be like C++ includes.
Nodejs require
is just regular statement and behaves like any other synchronous function.
from mapsapi-codestyle.
Nodejs require is just regular statement and behaves like any other synchronous function.
It doesn't matter. Using require
we define module dependencies.
from mapsapi-codestyle.
But why? What's the differences between initializing variables and initializing local aliases to other modules?
from mapsapi-codestyle.
👍
I personally like single var rule. It makes code cleaner and simpler, wiping off ugly and probably long var-column.
from mapsapi-codestyle.
Multiple var
do nothing valuable just adding a redundant noise.
from mapsapi-codestyle.
I think, we can remove this rule from general codestyle and each project will decide for himself how to declare variables.
from mapsapi-codestyle.
👍 to remove this rule from codestyle and add it to specific projects (for maps project, for example).
It doesn't break backward compatibility. It just makes our codestyle loyaler. Any project or team can use stricter subset of yandex codestyle. Looks good for me.
from mapsapi-codestyle.
It doesn't break backward compatibility
Actually it breaks bc in yandex
preset in jscs.
from mapsapi-codestyle.
Multiple var do nothing valuable just adding a redundant noise.
http://benalman.com/news/2012/05/multiple-var-statements-javascript/
from mapsapi-codestyle.
Multiple var is becoming more and more popular ^^
npm/npm@7feb52e
from mapsapi-codestyle.
@tarmolov It's just because everything is better than "comma-first" style )
from mapsapi-codestyle.
Did you take into account that such code style leads to huge diffs and merge conflicts which cannot be resolved automatically?
- var a = 0; |
+ var a = 0, |
b = 0; |
|
a = f(); | var a = f();
+ b = g(a); | + var b = g(a);
from mapsapi-codestyle.
@ascheglov Yep, see #14 (comment)
from mapsapi-codestyle.
So, majority of the participants disagreed to the proposal and I don't see important advantages.
Also I think that we should not remove this rule, because it's important.
from mapsapi-codestyle.
Related Issues (20)
- async without await HOT 1
- Use tslint "max-line-length" rule HOT 1
- Обязательный вызов конструктора родителя в конструкторе дочернего класса HOT 1
- Codestyle for ES6 HOT 3
- Enums code style HOT 12
- Spaces around comma HOT 3
- Required space in class methods names HOT 6
- JSCS 3 support for loris preset HOT 2
- Update eslint rules HOT 3
- Clarifying object notation HOT 2
- Class properties order HOT 5
- Type assertions style
- Move react preset to separate package
- Add tslint config
- Add rule for beginning underscore for private fields HOT 1
- Mandatory return type for functions and methods HOT 3
- Identifier references in object literals HOT 7
- Template parameter naming HOT 9
- Add rule for spaces in imports HOT 2
- Ternary operator HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mapsapi-codestyle.