java - Is it good to declare a local variable in the middle level of your code? -
while reading book "clean code" came across following instruction:
"local variables should declared above first usage , should have small vertical scope. don’t want local variables declared hundreds of lines distant usages."
see following example:
public class { public static void main(string[] args) { string name = "robert"; string country = "iiiii"; int age = 21; string hobby = "soccer"; system.out.println("my name "+name+"i'm "+age+" , i'm "+country); /* * * lot of code here * * */ system.out.println("my hobby is: " + hobby); } }
here variable hobby throw stone usage , want make sure if clean write code bellow?, because see local variables declared in top level of function :
/*the previous code here*/ string hobby = "soccer"; system.out.println("my hobby is: " + hobby);
it recommended declare variable in least possible scope. declare u use it. makes code little cluttered.
Comments
Post a Comment