Coding StandardAssignment 5 and 6Your goal should be "clean code that works". This coding standard will help you make the code clean. This coding standard is the same one we used for the last assignment New this time:
New for assignment 4:
White Space and Indenting1) Use 2 to 4 spaces to indent blocks. Do not use the tab character - it may look OK on your display but it does not always print neatly. 2) Always use the same number of spaces to indent. 3) Always indent after an opening brace '{'. 4) Put white space in your files to separate blocks of related statements. Line Length1) Lines must be less than 80 characters in length. Longer lines don't print correctly. 2) When you have long statements, break them so that they take multiple lines. Indent the second and following lines. The following two examples illustrate this principle.
Comments1) There must be a header comment. At a minimum, it must include the following information:
You may want to use the header comment from the first lab exercise as a template. 2) In-line comments should be included above blocks of related statements. 3) Every method must have a block comment describing it. See the method section for details. Identifiers1) Follow the Java naming conventions. 2) Use meaningful names that express the intention of the identifier. If-Else and While1) Do not have two if statements such that the first one tests for one condition and the second one tests for the opposite. Use the else statement. For example, replace:
with
2) There should be no comparisons with true or false in your predicates. For example, the if statement
should be written as
3) Else if should appear on a single line. Each else should start in the same column as the first if in the sequence. if ( surfIsUp ) goSurfing(); else if ( isWindy ) goFlyKite(); else if ( goodSnow ) goSkiing(); else study(); 4) Put the statements that make up the then and else clauses of if statements on a new line. For example, if ( x < 0 ) x = -x; should be written as or it can be written asif ( x < 0 ) x = -x;
Methods1) Method names must reveal the intention of the method
2) The formal parameters must have meaningful identifier names 3) Methods should do one thing. Methods that do several things
are hard to read and modify. 4) Methods should be relatively short. In this course, you should not have methods that are more than 20 to 25 lines long. See me if you feel that you can justify having longer methods - I'm open to persuasion. 5) Methods must have a header comment. This comment must describe the method's purpose, input parameters, and return value. For example
Arrays1) Always use the array length instead of a constant. For example, given the definition int count[] = new int[5]; the for loop should be written like thisfor (int i = 0; i < 5; i++ ) sum += count[i]; for (int i = 0; i < count.length; i++ ) sum += count[i]; This makes it much easier to change the size of the array in your program. 2) Don't use arrays of length 1 to pass return values from methods. This is an ugly hack that can lead to unreadable and unmaintainable code. If you find yourself doing this, your design probably needs to be re-examined. You should be able to accomplish the same thing in a simpler, clearer way. Braces1) The opening brace must be at the end of the statement that starts the block. The closing brace must be in the same column as the first character of the statement that started the block. For example
or
|