Posts

Showing posts from June, 2014

#8. Define variables as close as possible to where they are used

Define variables as close as possible to their first use. Prefer variables with the most local scope as possible. Inside a function body, at run time, a variable begins to exist once the code execution reaches the point where it is defined - not the point where it is only declared. (The Stack Overflow question  What is the difference between a definition and a declaration?  will help you understand the difference between a variable declaration and a variable definition. See the  Answer  by sbi.) It benefits the readability of your code that you keep functions short and well structured. To achieve this, It is a key factor that every concept is limited to the exact scope where it belongs to. Define, initialize and use each variable exactly where it is needed, not any earlier. By following this simple guideline you will write code which is more readable, contains less defects, and is easier to debug. Refactoring a function body is one of the most common tasks in software engineering