I am working to set some programming practices standards for my organization. Doing so i came across the issue "Title of this question"
Some finds it best declaring variables as late as possible and some finds its good to have them at the top of method starting brace.
Oracle Standard also says to declare them as early as possible. So do i in favor of declaring them at the closest curly brace they belong.
Concerns are
Code Readability
Performance
Less Error Prone
Any Comments are welcomed.
This is what McConnell says in his must-read book Code Complete 2:
Ideally, declare and define each variable close to where it’s used. A declaration establishes a variable’s type. A definition assigns the variable a specific value. In languages that support it, such as C++ and Java, variables should be declared and defined close to where they are first used.
He also recommends keeping variables alive for as short a time as possible, and to minimize scope.
The important thing for me is following naming conventions (Sun's Java Code Conventions are widely used). As for where are they first declared, I'd delay the declaration as much as possible for performance reasons (declaring a var you might not use is kind of a waste). Unless you know in advance it is going to be used for sure, in that case you can group it with other vars to improve readability. I think that is what JCC says about it.
For Code Code Readability I find declaring the variable at the beginning of a block the best option.
When going thru code you then always know where to look for the declaration instead of having to look between lines of code. It is also better when looking at a method to first see the declared variables and then the code. I think of it like a recipe in a cook book you list the needed ingredients separate from the recipe instruction.
As for performance I dont see how this would make any difference. While declaring late might save abit of memory by only creating the variable when needed at the end of a block you will use the same amount of memory you would have if you declared at the beginning of a the block.
Error prone i would say is the same reasons as readability.
Declare in the smallest scope needed and declare at the beginning of the block.
I think 'Code Readability' is the only important point here. And code is definitely more readable if variables are declared closer to where they are used.
But, sometimes, in order to prevent 'errors' for new programmers transitioning between languages, a particular style of coding method may be adopted. For example, JavaScript has functional scoping vs Java which has block scoping. A Java programmer can run into issues in JavaScript if she is unaware of JavaScript's functional scoping. Therefore, in case of JavaScript, in order to avoid potential bugs, it is just recommended to declare everything at the beginning of a function/method.
Like I mentioned above, 'Performace' shouldn't be a concern. But, if you choose to declare variables closer to where they are used, it could help in reducing declaration and initialization costs of variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With