Ask HN: Variable Declaration Location
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?