Following Java variable naming conventions is a very important practice. First of all, if you are breaking the rules, then you will be punished with "Invalid Character" compilation error. Secondly, if you are not following the conventions, it will make your variable names confusing or unmeaningful or both to yourself and others who will use your code. We don't want our life difficult by even a micro-unit. You too.
Not So Good Code
//count of boys in the class int count1 = 22; //count of girls in the class int count2 = 25; //....some more code //somewhere in the program double ratio = count1/count2;
Not So Bad Code
//count of boys in the class int countOfBoys = 22; //count of girls in the class int countOfGirls = 25; //....some more code //somewhere in the program double ratio = countOfBoys/countOfGirls;
The "Not So Good Code" snippet variables can not be identified of their purpose by their names. We just know some count (worst it'd have been if it were 'c1' or 'c2') is represented by those variables, but what?!! Dumbo!
In "Not So Bad Code" snippet, we can understand the meaning of the variables and also coolly find out the "ratio" means what: The ratio of boys to girls of the class.
Voila!
If we follow the convention, it becomes vary convenient for developers, as well as viewers and maintainers of the code, unless the code was developed with intention of hiding the real purpose like some malicious code or a code that you do not want others to understand easily. Then you can write confusing name. Your fingers, your wish!
In addition to the conventions, I have felt at many times that adding the first letter of the variable's type to the meaningful name adds more value, at least in case of primitive variables with whom we do so many operations.
Like, prepending 'l' to long, 's' to String, 'i' to int, 'c' to char variables, etc,. This will give me more control of the variable at first sight 'cause I know how to use it without going to the variable declaration part. This has helped me in codes having many lines, so that I do not have to relook their type whilst using. Saves 1 sec atleast :) !!
Example
//count of boys in the class int iCountOfBoys = 22; //count of girls in the class int i_countOfGirls = 25; //just tried a different style //....some more code //somewhere in the program double dratio = iCountOfBoys/i_CountOfGirls;
Iterating, it is in your hands how you handle your code.
More better, we can do like
//instance variables by prepending i_ int i_iCountOfShrubs; int i_iCountOfGardeners; float i_fAreaOfGarden; String i_sNameOfGarden; //static variables by prepending s_ static boolean s_bCountOfVisitors; //local variables by prepending l_ int l_iCountOfChildrenPlaying; //method parameters by prepending p_ print AreaOfCircle(double p_dRadius){ l_dArea = Math.PI * p_dRadius * p_dRadius; System.out.println("Area is: " + l_dArea); }
Reiterating, everything you do is in your own hands. This was just a suggesstion from me who found it slightly useful. But remember, do it at your convenience, as introducing far too many things like a_, z_ will only recreate confusion.
I know you may come up with brighter ideas, so please post below as well as in other forums that you're active in. Thanks for reading.
No comments:
Post a Comment
Liked or hated the post? Leave your words of wisdom! Thank you :)