Git Product home page Git Product logo

Comments (31)

ikokostya avatar ikokostya commented on May 27, 2024

👎

(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.

twirl avatar twirl commented on May 27, 2024

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.

dmikis avatar dmikis commented on May 27, 2024

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.

twirl avatar twirl commented on May 27, 2024

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.

dmikis avatar dmikis commented on May 27, 2024

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.

ikokostya avatar ikokostya commented on May 27, 2024

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.

twirl avatar twirl commented on May 27, 2024

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.

twirl avatar twirl commented on May 27, 2024

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.

dmikis avatar dmikis commented on May 27, 2024

(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.

tarmolov avatar tarmolov commented on May 27, 2024

So interesting tred

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.

ikokostya avatar ikokostya commented on May 27, 2024

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.

dodev avatar dodev commented on May 27, 2024

@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.

twirl avatar twirl commented on May 27, 2024

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.

twirl avatar twirl commented on May 27, 2024

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.

dmikis avatar dmikis commented on May 27, 2024

@twirl, stop, we were talking about functions and now you are referring to blocks. Which would it be?

from mapsapi-codestyle.

dmikis avatar dmikis commented on May 27, 2024

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.

ikokostya avatar ikokostya commented on May 27, 2024

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 avatar twirl commented on May 27, 2024

@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.

ikokostya avatar ikokostya commented on May 27, 2024

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.

twirl avatar twirl commented on May 27, 2024

But why? What's the differences between initializing variables and initializing local aliases to other modules?

from mapsapi-codestyle.

vtambourine avatar vtambourine commented on May 27, 2024

👍
I personally like single var rule. It makes code cleaner and simpler, wiping off ugly and probably long var-column.

from mapsapi-codestyle.

dfilatov avatar dfilatov commented on May 27, 2024

Multiple var do nothing valuable just adding a redundant noise.

from mapsapi-codestyle.

alt-j avatar alt-j commented on May 27, 2024

I think, we can remove this rule from general codestyle and each project will decide for himself how to declare variables.

from mapsapi-codestyle.

tarmolov avatar tarmolov commented on May 27, 2024

👍 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.

ikokostya avatar ikokostya commented on May 27, 2024

It doesn't break backward compatibility

Actually it breaks bc in yandex preset in jscs.

from mapsapi-codestyle.

ikokostya avatar ikokostya commented on May 27, 2024

@dfilatov

Multiple var do nothing valuable just adding a redundant noise.

http://benalman.com/news/2012/05/multiple-var-statements-javascript/

from mapsapi-codestyle.

tarmolov avatar tarmolov commented on May 27, 2024

Multiple var is becoming more and more popular ^^
npm/npm@7feb52e

from mapsapi-codestyle.

dfilatov avatar dfilatov commented on May 27, 2024

@tarmolov It's just because everything is better than "comma-first" style )

from mapsapi-codestyle.

ascheglov avatar ascheglov commented on May 27, 2024

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.

twirl avatar twirl commented on May 27, 2024

@ascheglov Yep, see #14 (comment)

from mapsapi-codestyle.

ikokostya avatar ikokostya commented on May 27, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.