2. Java code conventions
Some of the code conventions of Java,
suggested by sun, followed by the article
below,
Â
Code Conventions for the Java TM
Programming Language Revised April 20, 1999
3. Java code conventions
â—Ź Indentation
â—Ź Statements
â—Ź Naming Conventions
â—Ź Programming Practices
Â
Classes and methods good, bad and ugly
6. Wrapping Lines
Break after a comma
Â
someMethod(longExpression1, longExpression2, longExpression3,
longExpression4, longExpression5);
Â
var = someMethod1(longExpression1,
someMethod2(longExpression2,
longExpression3));
7. Wrapping Lines
Break before an operator
Â
longName1 = longName2 * (longName3 + longName4 - longName5)
+ 4 * longname6; // PREFER
longName1 = longName2 * (longName3 + longName4
- longName5) + 4 * longname6; // AVOID
Â
8. Wrapping Lines
â—Ź If the above rules lead to confusing code or
to code that's squished up against the right
margin, just indent 8 spaces instead
â—Ź Align the new line with the beginning of the
expression at the same level on the
previous line
12. Statements
Always prefer foreach statement
//ugly code
void cancelAll(Collection<TimerTask> c) {
for (Iterator<TimerTask> i = c.iterator(); i.hasNext(); )
i.next().cancel();
}
//clean code
void cancelAll(Collection<TimerTask> c) {
for (TimerTask t : c)
t.cancel();
}
13. Naming Conventions
Packages
â—Ź all lower case
â—Ź starts with domain name[ com, edu, gov,
mil, net, org]
â—Ź the rest is organizational standards
Examples
com.sun.eng
com.apple.quicktime.v2
edu.cmu.cs.bovik.cheese
14. Naming Conventions
Classes/Interfaces
â—Ź Should be noun
â—Ź Cap init camel case
Â
Note: If you can not name a class then that is
an indication of "Something went wrong, when
you were splitting the responsibility"
16. Naming Conventions
Variables
â—Ź Init small camel case
â—Ź meaningful
Â
Example
Product product;
List<Product> products;
Â
Note: Usually IDE suggestions are good candidates.
17. Naming Conventions
Constants
â—Ź All caps separated by '_'
Â
Examples
static final int MIN_WIDTH = 4;
static final int MAX_WIDTH = 999;
static final int GET_THE_CPU = 1;
19. Programming Practices
Referring to Class Variables and Methods
Â
Use class to access the method, as the
responsibility goes to the class not the object.
Â
AClass.classMethod(); //OK
anObject.classMethod(); //AVOID!
20. Programming Practices
Keep the code consise
if (booleanExpression) {
return true;
} else {
return false;
}
should instead be written as,
return booleanExpression;
32. Object
â—Ź Responsibility
â—Ź Which object is being impacted
â—‹ Which are the properties that are changed
â—‹ Which object has changed his behavior
car.addFuel(10);
33. Object
Anyone can add fuel to the car, even the
driver
Â
Class Driver {
...
public void refillCar() {
car.addFuel(10);
}
}
34. Methods
Behavior of an object,
that can change the properties of
that object
38. In short
When we design a software
â—Ź Search for classes (Entity)
â—Ź Search for behavior
â—Ź Identify which is the behavior of which
entity
â—Ź Identify the relationship between the
entities
39. Methods
â—Ź Should properly express an action
â—Ź Should have very few arguments
â—Ź Should be readable as a plain english
language not as a geeky programming
language
â—Ź Better be an active voice
â—Ź Should be short, should look like a pseudo
code, which means the details should be
hidden in the sub methods
41. Conclusion
â—Ź Follow the conventions
â—Ź Any code is 20% of its time is written and
80% time is read, so write it well
â—Ź Top level method should only contain a
pseudo code
â—Ź Methods should be small
â—Ź Think in terms of object and identify
responsibilty