Clean Code : Keep code clean :: Variable Declarations
Developers are authors. And one thing about authors is that they have readers.
Variable Declarations |
Variables should be declared as close to their usage as possible. Because our functions are very short.
- local variables should appear at the top of each function
- Instance variables on the other hand, should be declared at the top of the class.
This should not increase the vertical distance of these variables, because in a well-designed class, they are used by many, if not all, of the methods of the class.
private static void readFilePreferences()
{
InputStream is= null;
try {
is= new FileInputStream(getPreferencesFile());
setPreferences(new Properties(getPreferences()));
getPreferences().load(is);
} catch (IOException e) {
try {
if (is != null)
is.close();
} catch (IOException e1) {
}
}
}
Control variables for loops should usually be declared within the loop statement
public int countTestCases() {
int count= 0;
for (Test eachTest : tests)
count += eachTest.countTestCases();
return count;
}
Recent Comments