Download CS-259 Java Course: Coding Standards and Best Practices - Prof. Joel Castellanos and more Lab Reports Computer Science in PDF only on Docsity! 1 CS-259 Data Structures with Java CS-259 Code Standards and Setting Code Styles in Eclipse Instructor: Joel Castellanos e-mail: joel@unm.edu 8/28/2009 web: http://cs.unm.edu/~joel/ Course website: http://cs.unm.edu/~joel/cs259/ Today Fly through Chapter 2 Coding Standards Solutions to Lab 1 Prime Numbers and Prime Factors Lab 2: Area by Upper Sum and Lower Sum Homework: Due Monday Aug 31 Read Chapter 3 , A Simple Java Program Comments Data Types Variables Operator2 Be ready for quiz! 2 Core Java: Chapter 2 Installing the Java Development Kit Choosing a Development Environment Using the Command-Line Tools Using an Integrated Development Environment Running a Graphical Application Building and Running Applets 3 CS-259 Coding Standards All project and Labs must follow the great and hallowed CS-259 coding standards. These standards do not necessarily represent the best nor the only good way to write Java code. If you have experience programming, then these standards may not be the standards you are used to using. However, in this class, these are the standards we will use. 4 5 Coding Standard – Closing Brackets Closing brackets will be indented on a line with no other commands. The only exception being comments placed on the line with a closing bracket. if (x == 5) { y=y+1; Badif (x == 5) { y=y+1; 9 } else if (x == 7) { y=y+2; } } //Comment here ok else if (x == 7) { y=y+2; } Coding Standard – Blocks and { } Whenever a structure spans more than one line, brackets must be used. For example: ok if (x == 5) y=y+1; ok if (x == 5) { y=y+1; } 10 Not CS-259 standard if (x == 5) y=y+1; 6 Coding Standard - Indenting Code blocks will be indented to show the block structure with two spaces per level. Tab characters shall not be used for indenting. All statements within a block must be indented to the same level. 11 Coding Standard – 80 Character Line Max No line shall be more than 80 characters. The best way to avoid overly long statements is by not doing too h i i l t t tmuc n a s ng e s a emen . 1 if (getVolume(length1, width1, height1) > getVolume(length2, width2, height2)) System.out.println ("box 1 is bigger"); else System.out.println ("box 2 is bigger"); 2 3 4 int volume1 = getVolume(length1, width1, height1); int volume2 = getVolume(length2, width2, height2); if (volume1 > volume2) 12 5 6 7 8 9 { System.out.println("box 1 is bigger"); } else { System.out.println("box 2 is bigger"); } Stored in register 7 Fixing Too Long a Line Example 2 Another case where a temporary variable can shorten a line and improve readability. Creating the temporary variable c also improves code maintenance: If the code changes so that the comparison needs to check stack[topOfStack] or stack[topOfStack-2], then Line 2 and 3 require only a single change while line 1 requires 4 changes. 13 1 if (stack[topOfStack - 1] == '*' || stack[topOfStack - 1] == '+' || stack[topOfStack - 1] == '-' || stack[topOfStack - 1] == '/') 2 3 char c = stack[topOfStack - 1]; if (c == '*' || c == '+' || c == '-' || c == '/') Fixing Too Long a Line Example 3 There are times when breaking a long statement in to multiple statements is more awkward than keeping the long statement. In such cases the statement should be broken in a logical place , and each line over which the long statement is continued must be indented. The indenting must be at least 2 spaces, but can be more spaces it that improves readability. Code example 8, indents line 3 so that the comparisons match up. 14 1 if (commandOption =='f' || commandOption == 'c' || commandOption == 'd' || commandOption == 'g') 2 3 if (commandOption == 'f' || commandOption == 'c' || commandOption == 'd' || commandOption == 'g') 10 Code Style → Formatter → Braces 19 Code Style → Formatter → Control Statements 20 11 Applying Style Click OK to close dialog. 21 Then, Right Click in source code, and select: Source → Format. As You type: Automatically Close... 22 12 Quiz 1-5: Coding Standard Which line does NOT follow the standard? for (i=0; i<10; i++) { h i [i] c ar c = nStr ; if (c == '+') c=a+b; else if (c == '*') c = a*b; a: else if (c>='0' && c<='9') b: { for (j=0; j<c; j++) c: { System out print("j="+ j); . . d: } e: System.out.print("\n"); } } 23