Naming conventionsFrom WikiJavabuy this book
the articleIt is a best practice to always use the naming conventions for all your packages, classes, methods and variables. The official java naming conventions are stated in this document. The document from Sun contains the naming conventions for what can be verified in a correct or wrong point of view, there are several more rules which suggest how to choose the names in your code. package namesThe packages normally are in the following format: com.<company_name>.<project_name>.<component_name>.<eventual_more_subpackaging>
examples package com.wikijava.examples.naming package com.wikijava.examples.naming.package.subpackage variable namesThe main rule in chosing the name for a variable, is to be sure that it clearly explains what the variable contains and why it is used. the problemIdeally no comment should be needed to explain what a variable represents. Imagine the case in which you are reviewing a piece of code written by someone else, and you stumble upon a row like the following: We would not understand any of this if the row would have been: String user = ses.getAttribute(mySes.U_NAME); this row doesn't tell us much about the meaning of the variables or what they contain. We may realize that the result of the assignment is some kind of information about the user and that ses is a container of attributes. To understand what type is mySes.U_NAME we need to search it's declaration. The same row could be written as follows: String userName = httpSession.getAttribute(mySession.USER_NAME);
Moreover, it is clear from that example that we should expect somewhere in the code above the row we are analyzing an import similar to: import javax.servlet.http.HttpSession; and a inizialization similar to: HttpSession httpSession = request.getSession(); All this information is immediately understood from one single row of code. the solutionWe may try to express a rule of the thumb on how to choose variable names: A variable should be called as the name of the interface it will accessed with if it is clear what the object contains for example: HttpSession httpSession;leaves no doubt about what the object will contain. If the interface is not specific enough then we should choose a name more bound to the content, for example: List<Customer> customers; the variable customers is clearly a kind of set containing information about the customers. The use of this variable in the code will be much clearer. The name of a variable should be bound as close as possible to the actual content of it See Alsohttp://java.sun.com/docs/codeconv/html/CodeConventions.doc8.html |
