[prev]
 

9- Naming Conventions

Naming conventions make programs more understandable by making them easier to read. they can also give information about the function of the ientifier-for example,wheter its a constant,package,or class-which can be helpful in understanding the code.
Identifier Rules for Naming Examples
packages the prefix of a unique package name is always written in all-lowercase ASCII letters and should be one of the top-level domain names, currently com,edu,gov,mil,net,org, or one of the english two-letter codes identfying countries as specified in ISO standard 3166,1981. Subsequent components of the package name vary according to an organizations own internal naming conventions. such conventions might specify that certain directory name components be division, department, project, machine,or login names. com.sun.eng com.apple.quicktime.v2 edu.cmu.cs.bovik.cheese
Classes Class names should be nouns, in mixed case with the first letter of each internal word capitalized. try to keep your class name simple and descriptive. use whole word-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form,such as URL or HTML). class Raster; class ImageSprite;
Interface Interface names should be capitalized like class names. interface RasterDelegate; interface Storing;
methods Methods should be verbs, in mixed case with the first letter lowercase, with the first letter of each internal word capitalized. run(); runFast(); getBackground();
Variables Except for variables,all instance,class and class constants are in mixed case with a lowercase first letters. internal words start with capital letters. variable names should not start with underscore_or dollar sign $ characters,even though both are allowe. i; c; myWidth;
Constants the names of variables declared class constants and of ANSI constants should be all uppercase with words separated by underscore ("_").(ANSI should be avoided, for ease of debugging.) static final
10- Programming Practices 10.1 Providing Access to Instance and Class Variables Dont make any instance or class variables public without good reason. often, instance variables dont need to be explicity set or gotten--often that happens as a side effect of method calls One example of appropriate instance variables is the case where the class is esssentially a data structure, with no behavior. in other words, if you woul have used a struct instead of a class(if java supported struct). then its appropriate to make the class instance variables public. 10.2 Refering to class variables and method Constants numerical constants (literals) should not be coded directly,except of -1,0 and 1, which can appear in a for loop as counter values. 10.4 Variable Assignments avoid assigning several variables to the same value in a single statement. it is hard to read. example: foobar.fchar = barfoo.1char = 'c'; //avoid! do not use the assignment operator in a place where it can be easily confused with the quality operator. example: if (c++ = d++) { //avoid (java disallow)) ... } should be written as if { (c++ = d++) !=0) { ... } do not use embedded assignments in an attempt to improve run-time performance. this is the job of the compiler. Example: d = (a=b+c)+r; //avoid! should be written as a = b+c; d = a+r; 10.5 Miscellaneous Practices 10.5.1 Parenthesis it is generally a good idea to use parenthesis liberally in expression involving mixed operators to avoid operator precedence problems. even if the operator precedence seems clear to you, it might not be to others--you shouldnt assume that other programmer know precedence as well as you do. if (a == b && c == d) //avoid! if ((a == b) && (c == d)) //use 10.5.2 Returning Values try to make the structure of your program match the intent. example: if (booleanexpression0 { return true; } else { return false; } should instead be written as return booleanexpression; similarly, if (condition) { return x; } return y; should be written as return (condition ? x : y); 10.5.3 Expression before '?' in the conditional Operator if an expression containing a binary operator before the ? in the ternary?: operator, it should be parenthesized. example: (x >=0) ? x : -x; 10.5.4 Special Comments use xxx in a comment to flag something that is bogus but works. use fixme to flag something that is bogus and broken. 11- Code Examples 11.1 java source file example The following example shows how to format a java source file containing a single public classs. interfaces are formatted similarly. for more information see "class and interface declarations" on page 3 and 'documentations comments" on page 8.