






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







CS-259 Code Standards and Setting Code Styles in Eclipse
Instructor: Joel Castellanos e-mail : [email protected]
8/28/
web: http://cs.unm.edu/~joel/ Course website: http://cs.unm.edu/~joel/cs259/
ย Fly through Chapter 2 ย CodCoding Standards g Sta da ds ย 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 3Homework: Due Monday, Aug 31 Read Chapter 3 ย A Simple Java Program ย Comments ย Data Types ย Variables 2 ย^ Operator
Be ready for quiz!
ย Installing the Java Development Kit
ยย Choosing a Development EnvironmentChoosing a Development Environment
ย Using the Command-Line Tools
ย Using an Integrated Development Environment
ย Running a Graphical Application
ย Building and Running Applets
3
ย 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 youthese standards may not be the standards you are used to using.
ย However, in this class, these are the standards we will use. 4
ย Each Java Source file shall begin with a heading in the form: //** *@version 1.2 Aug, 24 2009 @author Ender Wiggin /
ย Each method shall begin with a comment explaining its inputs, what it does, how it does it, and its return value.
7
Open brackets will be placed at the beginning of a line (not at the end).
ok
if (x == 5) { y = y+1; }
if (x == 5) {
8
Not CS- standard
if (x 5) { y = y+1; }
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; Bad
if (x == 5) { y=y+1;
9
{ y=y+1; } else if (x == 7) { y=y+2; }
{ y=y+1; } //Comment here ok else if (x == 7) { y=y+2; }
ย 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;
ย 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 == '/')*
ย 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 aIn such cases, the statement should be broken in a logical placelogical 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')
Which line does NOT follow the standard?
a: for (i=0; i<10; i++) f (i 0 i 10 i ) b: { int c = i10; c: if (c == 30) d: c=c+6;*
e: else if (c == 40) c = c-6;
15
e: else if (c == 40) c = c 6; }
Window โ Preferences ยย JavaJava โ Code StyleCode Style โ FormatterFormatter โ New...New...
16
19
20
ย Click OK to close dialog.
21
ย Then, Right Click in source code, and select: Source โ Format.
22