These are chat archives for AstralDynamics/conventions

11th
Jul 2014
Noah
@eeue56
Jul 11 2014 14:21
I disagree with uninitialised variables, I don't think it's a good style especially not when you're working with beginners
var f = null is better than var f
Dan Prince
@danprince
Jul 11 2014 14:42
And that's fine, in the case that f is going to end up as an object
It makes even less sense for beginners if they have to set all their variables to null
If it makes sense for the variable to have an initial value, then it gets declared there, but in a number of cases, it doesn't.
Noah
@eeue56
Jul 11 2014 14:48
It might less sense, but it will be more helpful when they come to debug.
Really if it doesn't have an initial value then it should be declared as closely to usage as possible, but I can see that having a place where everything is var'd is handy
Dan Prince
@danprince
Jul 11 2014 14:50
If you declare as close to usage as possible then you risk clobbering in an upper scope
Noah
@eeue56
Jul 11 2014 14:50
It's always possible to set a sensible default, and a sensible default is better than nothing
Dan Prince
@danprince
Jul 11 2014 14:53
If let was supported all round, it wouldn't be an issue. Until then, js is stuck with function level scoping, so all variables are going to go at the top.
The amount of debugging time you will save with the differential of null (or some other arbitrary initial value) is going to negligible (or perhaps worse), than dealing with undefined
Noah
@eeue56
Jul 11 2014 14:54
If you're going for a declaring style, then it makes sense to follow how languages that require declared types work, by requiring either an init to an object or to null
or default
Yeah, I think it's fine to have it all at the top (well, not great, but it's fine)
Not arguing against that
Only the var a, b, c, d style
If your function has enough variables that single lines with inits is too noisy, then your function has too many variables in it's scope and needs to be redesigned to either use objects or into smaller functions
Then with a line for each there's no reason not to init it, and it makes it clearer to the developer what kind of data it will be dealing with
The whole point of keeping declarations as close to usage is to minimize the amount of data you have to hold in your head at once
Dan Prince
@danprince
Jul 11 2014 15:00
And in an ideal world that's how it would be done.
Noah
@eeue56
Jul 11 2014 15:00
Seeing a giant var block at the top with no inits provides little information on what type of variable it is, other than the name
Dan Prince
@danprince
Jul 11 2014 15:00
It's not supposed to
Noah
@eeue56
Jul 11 2014 15:00
New developers can't be trusted to choose good names
Dan Prince
@danprince
Jul 11 2014 15:00
It's purely safety net stuff
Noah
@eeue56
Jul 11 2014 15:00
By having the var block there, it increases the weight of the function
Dan Prince
@danprince
Jul 11 2014 15:01
Imagine that variables were scoped to their block
Noah
@eeue56
Jul 11 2014 15:01
To reduce that, sensible defaults show what they should be.
Dan Prince
@danprince
Jul 11 2014 15:01
Rather than to the window object
Then you would go through the code, declaring at usage
Noah
@eeue56
Jul 11 2014 15:01
Yeah, I know the reasons for it.
That's not my point
Dan Prince
@danprince
Jul 11 2014 15:02
If you understand the reasons for it, then you are seriously missing the point
Noah
@eeue56
Jul 11 2014 15:02
No, there are valid reasons for the var block
There aren't many for a var block without inits.
Dan Prince
@danprince
Jul 11 2014 15:02
Imagine it was python, there is no top level block of variables to reason about
You make your deductions by reading the variables on the lines they are declared on
There is no glob at the top which will relay name and type information
Noah
@eeue56
Jul 11 2014 15:03
Exactly
Dan Prince
@danprince
Jul 11 2014 15:03
That is the style that this convention promotes
Noah
@eeue56
Jul 11 2014 15:04
And because you declare them all at the top, that is where you should provide type information.
Dan Prince
@danprince
Jul 11 2014 15:04
No
The declaration at the top isn't there to be used for understanding
It's there as a safeguard
Noah
@eeue56
Jul 11 2014 15:04
I know, but it is there
And because it is there it increases the weight of the function in your mind whether you think it does or not
Dan Prince
@danprince
Jul 11 2014 15:05
The information associated with each variable should be extracted when you are reading in its usage
Noah
@eeue56
Jul 11 2014 15:05
Especially for new programmers who haven't learnt to skip it.
Dan Prince
@danprince
Jul 11 2014 15:05
Only if your mind suffers serious mental barriers in learning about how it works
Noah
@eeue56
Jul 11 2014 15:06
The other acceptable style would be not putting any inits in the var block
So that you don't have var declarations mixed with inits at all
Dan Prince
@danprince
Jul 11 2014 15:06
Most of the time, you don't
Noah
@eeue56
Jul 11 2014 15:06
It should be all or nothing
When writing conventions then you should keep things as consistent as possible
Dan Prince
@danprince
Jul 11 2014 15:08
Agreed, but in my opinion, there are edge cases, such as sets of require() which are completely afforded and exemption from convention
When you are working with a block of variables, whose initial state is guaranteed, then it's fine to declare them outright, but forcing consistency one way or the other is going to make your code look messy
Noah
@eeue56
Jul 11 2014 15:11
I think requires could get a free pass, but you wouldn't have them declared at the same scope as you would for a local var block anyway
I think you either need to treat a var block as purely being a safeguard
Dan Prince
@danprince
Jul 11 2014 15:12
Requires are translated into a local var block when you run browserify, or node wraps your code in a commonJS wrapper
Noah
@eeue56
Jul 11 2014 15:12
Or treat is as being a functioning part of the program where you follow standard programming rules
Dan Prince
@danprince
Jul 11 2014 15:13
Yeah, and for 95% of cases, then it is just the safeguard
And the other 5% is left to the programmer to be sensible
Noah
@eeue56
Jul 11 2014 15:13
Then you should identify the other cases and put them in as exceptions to the rule
Noah
@eeue56
Jul 11 2014 15:14
Then the rule should be that it is just a safeguard, with a footnote saying that sometimes you want to init things there too
That's the opposite, that implies they're on equal footing
It's basically like saying "do whatever you feel like here because both is fine any amount of the time"
Or at least that's how it reads
imo
Dan Prince
@danprince
Jul 11 2014 15:18
No, it's saying "This is how you do variable declaration, unless all your variables have initial values"
Main case, edge case
Noah
@eeue56
Jul 11 2014 15:20
It's not how it reads though
Just changing it to
Dan Prince
@danprince
Jul 11 2014 15:21
"declare all variables at the top of the functions they are used in." ... "Alternatively, if they all have values, initialize them there"
Feel free to fiddle with the wording if you think it won't make sense to other people
Noah
@eeue56
Jul 11 2014 15:21
When they all have initial values, you may initialize them there. Don't mix uninitialized with initialized blocks.
Basically it needs to read more like "I am telling you what to do and what not to do and when"