3 ms·
Given this post I am actually curious as to whether there are certain edge cases in which a single var statement prevents a semantic error that multiple var sta
by TheZenPsycho 13y ago
Given this post I am actually curious as to whether there are certain edge cases in which a single var statement prevents a semantic error that multiple var statements would hide.
This phrase piqued my interest:
> variables being hoisted half-way through a scope are declared before being necessarily defined
now I know how hoisting works. Every var statement is silently split from its assignment so all the declarations are semantically at the top of a function scope regardless of what you do, but the assignments stay where they are. Which means that the bugs we're trying to prevent probably have to do with building up a value through sequences of assignments using previously defined variables- which could well behave differently depending on which of these patterns is used.
I'm just wondering if there's some set of variable assignments that actually behaves differently depending on which of these two presented patterns are used. So I'll need to do some testing I think.
Aside from that, I always assumed the main advantage of this pattern is as an obvious visual indicator of code smell. If you have enough variables that the single var pattern starts becoming unmanageable, that is a strong indication that the function you are writing probably should be several different functions.
- basicallydan 13y ago> I'm just wondering if there's some set of variable assignments that actually behaves differently depending on which of these two presented patterns are used. So I'll need to do some testing I think. You could be onto something, I'd love to hear if you come to any conclusions about that. Off the top of my head I can't actually think of anything since as far as I can tell, the only difference is that one `var` statement is used and in the other it's one for each variable - they are still declared in the same place. As for the code smell thing, that's a good point. I think after roughly the same amount of time using multiple vars would cause the same indication but it's probably more obvious with single var.