Include a comment block at the top of EVERY file, which includes the filename, author, course with section number, assignment, and a description of the program or what the file contains. Note the SPACING and ORDER.
/* Filename : Primes.java Author : Gary M. Zoppetti Course : CSCI 162-01 Assignment : Program 2, part A (be specific) Description: Calculate prime numbers between 2 and 10,000,000 (use your own words) */
Use BLANK LINES between blocks of code to enhance readability. Think of the code as if you are writing paragraphs in a paper. When your code shifts to a “new thought,” place a blank line.
Use BLANK LINES to separate method definitions.
public static void printIntro () { ... } public static double calcInterest (double balance, double rate) { ... }
Flank binary operators with spaces.
int a = 3 * (4 + 5); // NOT int a=3*(4+5)
Use the SAME number of spaces (between 2 and 4) for each level of indentation. Be CONSISTENT.
Use Java naming conventions (lowerCamelCase and UpperCamelCase) and choose meaningful, mnemonic names. If a value with units is represented, include the units in the name. Constants should be in all CAPS, with underscores used to separate words.
Avoid using SINGLE LETTER names except when there is no easy way to create a variable name that is descriptive. For example, "s" is OK for the name of a String if you are referring to any generic string and not something specific such as a word or name (in which cases "word" or "name" would be preferable).
float rotationAngleRadians; final long NUM_SORT_VALUES = 10000; // Start method names with a verb to convey action void convertToLower (String s); // Use nouns for class names class Dictionary;
Use pre-increment and pre-decrement instead of the postfix versions.
++i; --j;
Define "main" BEFORE any other methods.
Don't write useless comments.
// Loop while "cond" is false while (! cond) { ... } // end while // Make "i" point to the beginning element i = v.begin ();