Ask HN: Variable Declaration Location
8 points| PlanetFunk | 16 years ago | reply
The tech lead is the same tech lead who developed the companies previous application and is well regarded.
The following code snippet is a bogus function the illustrates his variable declarations (all at the top of the function).
public virtual Token LogOn(Credentials credentials) {
Token token;
User user;
token = null;
if (ModelState.IsValid) {
if (validateCredentials(out user)) {
token = user.CreateToken();
}
}
return token;
}
I've always declared my variables within the smallest scope possible. If later it needs to be accessed higher up, then it gets moved during the code change/refactor. public virtual Token LogOn(Credentials credentials) {
Token token = null;
if (ModelState.IsValid) {
User user;
if (validateCredentials(out user)) {
token = user.CreateToken();
}
}
return token;
}
The tech leads var dec bugs me no end :PHell why bother declaring it in the function at all, GLOBAL VARS FOR EVERYONE!
Am I just being anal retentive? Am I wrong? Is it something I should address, conform with (at least in this project) or do I need to build a bridge and get over it?
[+] [-] sparky|16 years ago|reply
Another thought; if there is a mammoth difference between top-of-function and narrowest-scope-possible, that suggests that you may have very large functions, and might consider refactoring so that the difference between your method and his is small?
[+] [-] PlanetFunk|16 years ago|reply
The size of the functions isn't really an issue, but I see your point.
If they're as small as the examples above, it doesn't make much difference.
[+] [-] Daniel_Newby|16 years ago|reply
Mutable composite types that use the heap should be scoped broadly, to reuse their heap buffer without touching the memory allocator. Example: C++ std::string.
Immutable heap composites could be scoped narrowly to reuse heap as soon as possible. On the other hand that reduces locality of reference for the deallocator code, and probably spams the branch predictor. If the immutable is small and deeply nested, it should probably be broadly scoped because branch prediction is made of solid gold (unless the language is garbage collected, making deallocation branchless).
Hmm... That got complicated. Stick them all at the top unless you are optimizing a nested loop.
[+] [-] jaydub|16 years ago|reply
[+] [-] scorpioxy|16 years ago|reply
Also, declaring variables at the top helps with languages that don't have block scopes.
[+] [-] gills|16 years ago|reply
But...there are probably more important battles to choose to fight during your project.
[+] [-] allenbrunson|16 years ago|reply
I also prefer declaring all variables at the start of a function. My rationale is that, if all functions are written that way, it's less cognitive load when reading them. All variables used are known at the beginning, and once I've digested that, I can think about the code that will be working with them. These days, if I'm reading a function and a new variable is declared partway in, I feel like I've been ambushed with a new thing that makes it harder to understand.
That said, I tend to write such short functions that it's never an issue. If a particular variable isn't needed until halfway into the function, that's a good sign to me that it needs to be broken into two or more smaller functions.
[+] [-] unknown|16 years ago|reply
[deleted]