Use of redundant var

# Fergus Cooney (17 years ago)

Ingvar von Schoultz wrote:

For very small and simple projects it's great that JavaScript lets you sprinkle your declarations all over the code, with redundant repetitions if you like. Block scope should be available also with this usage pattern, in my view.

Jon Zeppieri wrote:

I do know, however, that I don't like this.

I think you, Ingvar, are the lone champion of redundant variable

declarations.

I'm not sure who the champions of var hoisting are. (In another thread, Brendan referred to hoisting as a wart.)

Yuh-Ruey Chen wrote:

Actually, I'm pretty sympathetic to redundant variable declarations, [...]

I'm also in favour of multiple, "redundant" vars. I use them semantically and as a discipline.

if (something)
    {
    var x = 1;
    :  :  :
    }
else
    {
    var x = 2;
    :  :  :
    }

When I write the above pattern you won't see x being used outside those inner blocks. The two vars are my way of saying that they are independant 'x's, although the same name would be being used because they have the same meaning.

If I want to use x after in the outer scope then I'll use

var x;
if (something)
    {
    x = 1;
    :  :  :
    }
else
    {
    x = 2;
    :  :  :
    }

I know that the language makes this unnecessary but I've always considered hoisting to be, if not a bug, then an undesirable necessity. That is, it's undesirable to me as I prefer everything to be block scoped and not hoisted, but necessary in a language which doesn't require pre-declared variables. I've always secretly hoped that the "bug" would be "fixed" but, of course, backward compatibility means that it can't. The feature of block scoping is a very welcome addition, hence me giving a bit of energy to promoting "here" over "let". ;-)

.Fergus Cooney

# Jon Zeppieri (17 years ago)

On Sat, Aug 23, 2008 at 9:25 AM, Fergus Cooney <f.cooney at gmx.co.uk> wrote:

I'm also in favour of multiple, "redundant" vars. I use them semantically and as a discipline.

if (something) { var x = 1; : : : } else { var x = 2; : : : }

When I write the above pattern you won't see x being used outside those inner blocks. The two vars are my way of saying that they are independant 'x's, although the same name would be being used because they have the same meaning.

Okay, but this suggests that if you had 'let' you wouldn't use redundant declarations.

Or, to put it another way, you only use redundant declarations because the language lacks block scoping. I don't think this makes you a champion of redundant declarations.

# Fergus Cooney (17 years ago)

Jon Zeppieri wrote:

On Sat, Aug 23, 2008 at 9:25 AM, Fergus Cooney <f.cooney at gmx.co.uk> wrote:

I'm also in favour of multiple, "redundant" vars. I use them semantically and as a discipline.

if (something) { var x = 1; : : : } else { var x = 2; : : : }

When I write the above pattern you won't see x being used outside those inner blocks. The two vars are my way of saying that they are independant 'x's, although the same name would be being used because they have the same meaning.

Okay, but this suggests that if you had 'let' you wouldn't use redundant declarations.

Or, to put it another way, you only use redundant declarations because the language lacks block scoping. I don't think this makes you a champion of redundant declarations.

-Jon

Lol, that's true. "Champion" is too strong a word. "Supporter" is better.

Another use I have for redeclaring variables is when I want to reuse the name but there's a clear separation between the previous use and the new use. For example, I might have a variable to amass a bunch of html snippets and then stick them in a div. I'll redeclare that variable again if I'm doing separate content for another div. It makes it explicit that there's no carry-over from above. Also, as I'm the declare-everything-first type, it also helps if I decide rearrange the code.

.Fergus

# Brendan Eich (17 years ago)

On Aug 23, 2008, at 8:23 AM, Fergus Cooney wrote:

Another use I have for redeclaring variables is when I want to
reuse the name but there's a clear separation between the previous use and
the new use. For example, I might have a variable to amass a bunch of html
snippets and then stick them in a div. I'll redeclare that variable again if
I'm doing separate content for another div. It makes it explicit that
there's no carry-over from above. Also, as I'm the declare-everything-first
type, it also helps if I decide rearrange the code.

That last point is a great one.

We did talk about redundant declarations when discussing strict mode.
IIRC we agreed not to punish them, mainly for the last reason and the
consequent abundant use of var in several paragraphs of code where
the vars have the same name and hoist to the same body scope, and
nothing's formally wrong with the code.

We can't have strict mode exact an inordinate migration tax, or it
won't be used. We shouldn't make it too pedantic, or it will either
not be used, or else waste programmer energy on non-problems or
hypothetical problems.