Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

let or var? #10

Open
nelsonic opened this issue Jun 2, 2016 · 2 comments
Open

let or var? #10

nelsonic opened this issue Jun 2, 2016 · 2 comments

Comments

@nelsonic
Copy link
Member

nelsonic commented Jun 2, 2016

asked by @SimonLab in https://github.com/FAC-GM/app/pull/465#issuecomment-222940039 #discuss

@wwalser
Copy link

wwalser commented Jun 11, 2016

I read your comment in the other thread. I just thought I'd add that I prefer using const and let in my own code. I agree with you in a sense, var could be considered clearer.

Q: "What should I use?"
A: "var, always."

However, I think my overall code is clearer when I use const and let properly. Variables are always defined using const, unless they will change after being defined. To a new reader, the very presence of a let tells them something about the code that they are about to read. "This specific variable is about to change". It also causes me to lean toward clarity when as I'm writing code. "I've just used let, do I need to do that? Can it be avoided?". I enjoy Hapi partly because the identity of a route is often declared right along side the route itself. The route implementation lives besides the identity of the route. Similarly, const and let affords me the ability to embed additional behavioral identity into my variable declarations.

My $0.02. I'm looking forward to where this repo goes 👍 .

@nelsonic
Copy link
Member Author

nelsonic commented Sep 13, 2016

@wwalser your $0.02 are very much welcome.
I agree that using let and const could make code clearer, but in my experience of reviewing the code of many developers that more often than not people have missunderstood the point of both of these ... 😞

const is a whole other discussion: #13

Let's focus on let ...

The point of let is that it allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used.
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/let

The examples on MDN are a good reference.

The idea of declaring a block-level variable with the same name (e.g: x) as another block-level variable within the same function is like calling your twin children the same name, sure you know they are distinct but confusion is inevitable.

image

All non-global variables are already _functionally scoped_ so provided you don't try to have multiple block/expression-scoped variables with the same name in the same function, I don't see any advantage to using let. (but I'd be delighted if someone can explain the advantages to me!)

And, in my experience they are confusing to inexperienced developers end up using let everywhere a var would be functionally equivalent because they see the "cool kids" on Medium using let everywhere ...

image

_Last, but not least_, there's the fact that let is not widely adopted/supported by the Browsers people are actually using: http://caniuse.com/#feat=let 32% of browsers in use don't support let
IE market share
so to write JS that works in those browsers we have to "transpile" our code which totally defeats the objective because the "transpiler" just uses var everywhere.

So the question we could ask is: once IE < 11 "goes away" should we adopt let ?
To which I reply: let's come back to this when that happens.
good one

Until let is 100% supported we can simplify things by just using var and a new variable name for "block scope".

@eliasmalik eliasmalik mentioned this issue Sep 23, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants