Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

At the place where I work, there are explicit guidelines for placement of declarations of variables. According to that, it is required to put them at the global level and / or at the beginning of functions, and not in inner blocks (such as a for loop). Since they've been specified by persons more experienced than I am, I'm sure that there must be a good reason for it, but I cannot figure out what that might be. It would be nice to know if there are any compile time / run time advantages at having them declared at a bigger scope.

share|improve this question
add comment

8 Answers 8

up vote 7 down vote accepted

I see two main advantages:

  • Reusing variable names with a different type is prevented.
  • It becomes clear at an earlier time that a routine needs to be refactored. The variables at the top become a major mess fairly quickly, and this mess is easy to recognize.

Any compiler worth its salt will optimize away the scope of the variables anyway, so it's purely a formatting concern.

For my two cents, I'd still prefer the innermost declaration of the variable to transport the intent of scope to the compiler. If you intended to have a variable only accessed within a loop, you can catch any later reference at compile time when you declare the variable in the loop.

share|improve this answer
    
+1 for mentioning refactoring. –  Péter Török Nov 11 '11 at 13:00
add comment

The only advantage I have found so far is code simplicity. Youalways know where to look for variable declarations and everybody in the team adopts the same coding style. These things make code maintenance easier but I am not sure that they make writing better code any easier. I do not mean that you write worse code only that sometimes it is harder to write code as good. Nevertheless if the development team is big or its members change frequently using code standards is helpfull.

share|improve this answer
add comment

This sounds like a decision to preserve consistency. It also prevents the use of same names for different variables in neighboring scopes and increase readability. As Gus points out, you'll also know where to look for variables. I think narrowest scope principle is better though because it prevents the variable clutter at the top. Outermost declaration is much like declaring private members of a class first IMO.

share|improve this answer
add comment

If the declaration is within an if clause that only rarely (if ever) gets exercised, but needs a lot of memory, your memory footprint is smaller (most of the time) than if you allocate everything at the beginning of the function.

If it is within a loop then you have to reallocate the memory repeatedly, this can be costly in performance terms.

There are reasons for doing things both ways.

share|improve this answer
add comment

The old C-standard of 1989 only allows variable declarations at the start of a block.

Only since C99 declarations are allowed anywhere. Maybe your place hasn't made the switch to C99 yet.

share|improve this answer
    
We use C99 - but more importantly, I was looking for what are the implications of declaring it in the innermost block, rather than at beginning of function. Perhaps, I wasn't clear enough... –  TCSGrad Nov 11 '11 at 15:09
add comment

Every language might differ in the preference of style and practice. Following is from JSF-AV-rules, which Stroustrup points to as the coding standards he prefers.

AV Rule 136
Declarations should be at the smallest feasible scope

The rationale for this is described as

This rule attempts to minimize the number of live variables that must be simultaneously considered. Furthermore, variable declarations should be postponed until enough information is available for full initialization

If you are in C++, declaring variable when you need them is preferred.

share|improve this answer
add comment

Not sure if you can call this a best-practice. When I set up guidelines for a new C project I always state that it is better to declare the variables close to where they are used. For two reasons, it makes it easier to refactor the code later on (i.e. when extracting a method). It also helps the compiler to do better optimization.

I'm not alone with this opinion. Here's a question that tackles the same problem: Where do you declare variables? The top of a method or when you need them? The answer here is to declare them where you use them. The same practice is described in the book 'Clean Code' by Robert C. Martin.

However, if you use an older C-standard (C-89), you must define local variables at the top of the function. So maybe the guideline is a remnant from the time when C-89 was used? It is probably better to ask the person that wrote the guidelines why the rule is still in there.

share|improve this answer
add comment

Seems like those who made this decision are use to a time when putting declarations at the top was the norm and have chosen not to switch to a preference to declare closer to where it is being used.

I'm not sure how helful this level of consistency is. Some IDE's probably make finding things easier than others. For global variables this makes sense, but if your function is so long it makes finding variable declarations difficult, you have bigger problems.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.