Docsity

CS-259 Java Course: Coding Standards and Best Practices - Prof. Joel Castellanos, Lab Reports of Computer Science

Toggle sidebar
bg1
1
CS-259
Data Structures with Java
CS-259 Code Standards and Setting
Code Styles in Eclipse
Instructor: Joel Castellanos
8/28/2009
web: http://cs.unm.edu/~joel/
Course website:
http://cs.unm.edu/~joel/cs259/
Today
๎š„Fly through Chapter 2
๎š„
Cod
in
g
Sta
n
da
r
ds
Cod 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 3
๎š„
Homework:
Due
Monday
,
Aug
31
Read
Chapter
3
๎š„A Simple Java Program
๎š„Comments
๎š„Data Types
๎š„Variables
๎š„Operator
2
Be ready for quiz!
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download CS-259 Java Course: Coding Standards and Best Practices - Prof. Joel Castellanos and more Lab Reports Computer Science in PDF only on Docsity!

CS-

Data Structures with Java

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/

Today

ย„ 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!

Core Java: Chapter 2

ย„ 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

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 youthese standards may not be the standards you are used to using.

ย„ However, in this class, these are the standards we will use. 4

Coding Standard: Comments

ย„ 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

Coding Standard โ€“ Open Brackets

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; }

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; 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; }

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;

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 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')

Quiz 1-4: Coding Standard

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; }

Setting Code Style in Eclipse

Window โ†’ Preferences ย„ย„ JavaJava โ†’ Code StyleCode Style โ†’ FormatterFormatter โ†’ New...New...

16

Code Style โ†’ Formatter โ†’ Braces

19

Code Style โ†’ Formatter โ†’ Control Statements

20

Applying Style

ย„ Click OK to close dialog.

21

ย„ Then, Right Click in source code, and select: Source โ†’ Format.

As You type: Automatically Close...

22