But in the second example, name is not used anywhere else, so you shouldn't?
I actually read that as it being more important to fail-fast and exit the function immediately. If you pass the fail-fast conditions then the function begins in earnest and variable definition is the next thing that happens - but there's no point in declaring them if we're just going to bail out of the function.
Hmm. That could be it; but it still seems to directly contradict their own advice. After all, the variable declaration is still hoisted before the if check, isn't it?
My point is that their advice says it should be the same: "Assign variables at the top of their scope.". The top of names scope is the top of the function. Therefore their advice would proscribe moving the assignment to the top of the function.
I'm not arguing about whether it's a good idea or not, I'm just trying to understand what they are advocating, since the statement and the example seem to be at odds, unless I am misunderstanding it.
5
u/mrkipling Mar 27 '15
I actually read that as it being more important to fail-fast and exit the function immediately. If you pass the fail-fast conditions then the function begins in earnest and variable definition is the next thing that happens - but there's no point in declaring them if we're just going to bail out of the function.