C. Java Code Conventions THE FOLLOWING ARE SOME OF THE GUIDELINES I like and use from the Code Conventions for the Java Programming Language prescribed by Sun on the java.sun.com website. (Note:There are many more conventions recommended by Sun, so be sure to visit this website.) All source files will have a beginning Javadoc comment. The first line of code in the source file will be the package statement followed by any import statements. Package names should begin with a lowercase top-level domain name (for example, com. or edu.). Class and interface names should be nouns and should use a mixed case with each word being capitalized (for example, EmployeeHours). All class files should have the following in the order listed here: Have a Javadoc for the class. List variables as follows: static variables, instance variables (public, protected, no access specified, and then private). List methods as follows: constructors and then methods (methods should be grouped by functionality, not scope).
Method names should be verbs and should use a mixed case with each word being capitalized, except that the first letter is lowercase (for example, getHoursWorked). Variable names should be verbs and should use a mixed case with each word being capitalized, except that the first letter is lowercase (for example, hoursWorked). Variables should start with alphabets. One-character variables (for example, i, j, or k) should be avoided and used only for temporary variables (for example, in a for statement). Try to make all class variables nonpublic and accessible only via methods. Constants should be all uppercase, with words separated by an underscore (for example MAX_WORK_HOURS). Try to use numeric values as constants (for example, int MAX_WORK_HOURS=24;). Try to initialize local variables where they are declared. Avoid lines longer than 80 characters. Each line should contain only one statement. If-else, for, while, do, and switch statements should always use braces. |