Re: Should "const" be favored over "let"?

2015-04-17 Thread Marius Gundersen
I too have found that most of my variables are defined with const, as they
never change. Let var die is a catchy saying, but I believe it should be
let var die, use const.


On Fri, Apr 17, 2015 at 6:16 PM, Allen Wirfs-Brock 
wrote:

>
> I agree, 'let' is likely to win because of it's length.  I find that I
> fall into using it solely or that reason.  I think it also wins on
> readability.
>
> If we had a "do-over".  I'd make `let` means what `const` now means and
> have something different for defining mutable lexical bindings.  Maybe `let
> var foo=...;`.
>
>
let and mut?  Oh well, too late for that now.

Marius Gundersen
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Should "const" be favored over "let"?

2015-04-17 Thread Allen Wirfs-Brock

On Apr 17, 2015, at 5:09 AM, Alex Kocharin wrote:

>  
> There won't be any performance gain. "const" is used to be much slower in v8 
> actually. But they fixed it as far as I know.
>  
> I think it's a code style matter. And speaking about that, realistically, 
> most code base will never use "const" widely. Just one reason: 5 characters 
> vs 3 characters to type. So in the name of keeping an amount of different 
> code styles smaller, I'd say stick with "let" (except for obvious constant 
> literals like `const PI = 3.14` on top). Just something to consider.

I agree, 'let' is likely to win because of it's length.  I find that I fall 
into using it solely or that reason.  I think it also wins on readability. 

If we had a "do-over".  I'd make `let` means what `const` now means and have 
something different for defining mutable lexical bindings.  Maybe `let var 
foo=...;`.

But the let/const pairing was a firmly established direction long before work 
on ES6 even started.  There was so much other stuff to work on and so much 
inertia behind let/const that nobody ever seriously challenged that direction. 

Allen


___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Should "const" be favored over "let"?

2015-04-17 Thread Andreas Rossberg
On 17 April 2015 at 14:17, Mathias Bynens  wrote:

> On Fri, Apr 17, 2015 at 7:53 AM, Glen Huang  wrote:
> > I've completely replaced "var" with "let" in my es 2015 code, but I
> noticed most variables I introduced never change.
>
> Note that `const` has nothing to do with the value of a variable
> changing or not. It can still change:
>
> const foo = {};
> foo.bar = 42; // does not throw
>
> `const` indicates the *binding* is constant, i.e. there will be no
> reassignments etc.
>

I have to nitpick on this. In usual nomenclature of programming language
semantics the "value" in question is the _reference_ to the object (for
types with reference semantics), and that does not change. That the object
itself actually can be mutable is a separate property.

The idea that const somehow applies transitively is a specific idiosyncrasy
of low-level languages like C and friends, that make flattening and copying
of mutable objects an implicit part of their semantics. It is not the norm,
and actually rather contorted semantically.

/Andreas


> In my post-ES6 code, I use `const` by default, falling back to `let`
> if I explicitly need rebinding. `var` is for legacy code.
> ___
> es-discuss mailing list
> es-discuss@mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Should "const" be favored over "let"?

2015-04-17 Thread Glen Huang
@Mathias

Thanks for the clarification. I should use "never rebind".

@Alex @Frankie

You guys are right. There won't be any performance gain since it's already 
statically analyzable.

FWIW, I opened a feature request on eslint concerning this coding style: 
https://github.com/eslint/eslint/issues/2333 
 

> On Apr 17, 2015, at 8:17 PM, Mathias Bynens  wrote:
> 
> On Fri, Apr 17, 2015 at 7:53 AM, Glen Huang  wrote:
>> I've completely replaced "var" with "let" in my es 2015 code, but I noticed 
>> most variables I introduced never change.
> 
> Note that `const` has nothing to do with the value of a variable
> changing or not. It can still change:
> 
>const foo = {};
>foo.bar = 42; // does not throw
> 
> `const` indicates the *binding* is constant, i.e. there will be no
> reassignments etc.
> 
> In my post-ES6 code, I use `const` by default, falling back to `let`
> if I explicitly need rebinding. `var` is for legacy code.

___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Should "const" be favored over "let"?

2015-04-17 Thread Mathias Bynens
On Fri, Apr 17, 2015 at 7:53 AM, Glen Huang  wrote:
> I've completely replaced "var" with "let" in my es 2015 code, but I noticed 
> most variables I introduced never change.

Note that `const` has nothing to do with the value of a variable
changing or not. It can still change:

const foo = {};
foo.bar = 42; // does not throw

`const` indicates the *binding* is constant, i.e. there will be no
reassignments etc.

In my post-ES6 code, I use `const` by default, falling back to `let`
if I explicitly need rebinding. `var` is for legacy code.
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Should "const" be favored over "let"?

2015-04-17 Thread Alex Kocharin
 There won't be any performance gain. "const" is used to be much slower in v8 actually. But they fixed it as far as I know. I think it's a code style matter. And speaking about that, realistically, most code base will never use "const" widely. Just one reason: 5 characters vs 3 characters to type. So in the name of keeping an amount of different code styles smaller, I'd say stick with "let" (except for obvious constant literals like `const PI = 3.14` on top). Just something to consider. io.js core uses "const" everywhere though.  17.04.2015, 08:53, "Glen Huang" :I've completely replaced "var" with "let" in my es 2015 code, but I noticed most variables I introduced never change.Should I replace them with "const"? Will there be any performance gain for most browsers? If so, should such performance gain be considered micro optimization?___es-discuss mailing listes-discuss@mozilla.orghttps://mail.mozilla.org/listinfo/es-discuss___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Re: Should "const" be favored over "let"?

2015-04-17 Thread Frankie Bagnardi
I've switched to let/const completely.  The gain of using const isn't
performance (you can statically analyze whether any non-global is
potentially assigned to).  The gain from const is that it's very very easy
for a human to statically analyze.

If I see a let binding, I know I need to be a bit more careful with editing
that piece of code.

I originally thought this'd be a terrible idea, but it's been working great
for me.


On Thu, Apr 16, 2015 at 10:53 PM, Glen Huang  wrote:

> I've completely replaced "var" with "let" in my es 2015 code, but I
> noticed most variables I introduced never change.
>
> Should I replace them with "const"? Will there be any performance gain for
> most browsers? If so, should such performance gain be considered micro
> optimization?
> ___
> es-discuss mailing list
> es-discuss@mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss


Should "const" be favored over "let"?

2015-04-16 Thread Glen Huang
I've completely replaced "var" with "let" in my es 2015 code, but I noticed 
most variables I introduced never change.

Should I replace them with "const"? Will there be any performance gain for most 
browsers? If so, should such performance gain be considered micro optimization?
___
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss