41
Java conventions A.K.M. Ashrafuzzaman

Java conventions

Embed Size (px)

DESCRIPTION

Some code convensions for Java. And some basic concept for the object orientation in Java.

Citation preview

Page 1: Java conventions

Java conventionsA.K.M. Ashrafuzzaman

Page 2: Java conventions

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

Page 3: Java conventions

Java code conventions

● Indentation● Statements● Naming Conventions● Programming Practices Classes and methods good, bad and ugly

Page 4: Java conventions

Indentation

Four spaces should be used as the unit of indentation  

Page 5: Java conventions

Line Length

Avoid lines longer than 80 characters.

Page 6: Java conventions

Wrapping Lines

Break after a comma someMethod(longExpression1, longExpression2, longExpression3, longExpression4, longExpression5); var = someMethod1(longExpression1, someMethod2(longExpression2, longExpression3));

Page 7: Java conventions

Break before an operator longName1 = longName2 * (longName3 + longName4 - longName5) + 4 * longname6; // PREFER

longName1 = longName2 * (longName3 + longName4 - longName5) + 4 * longname6; // AVOID 

Wrapping Lines

Page 8: Java conventions

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

Page 9: Java conventions

Wrapping Lines//CONVENTIONAL INDENTATIONsomeMethod(int anArg, Object anotherArg, String yetAnotherArg, Object andStillAnother) { ...} //INDENT 8 SPACES TO AVOID VERY DEEP INDENTSprivate static synchronized horkingLongMethodName(int anArg, Object anotherArg, String yetAnotherArg, Object andStillAnother) { ...}

Page 10: Java conventions

Wrapping Lines

//DON'T USE THIS INDENTATIONif ((condition1 && condition2) || (condition3 && condition4) ||!(condition5 && condition6)) { //BAD WRAPS doSomethingAboutIt(); //MAKE THIS LINE EASY TO MISS}

//USE THIS INDENTATION INSTEADif ((condition1 && condition2) || (condition3 && condition4) ||!(condition5 && condition6)) { doSomethingAboutIt();}

//OR USE THISif ((condition1 && condition2) || (condition3 && condition4) ||!(condition5 && condition6)) { doSomethingAboutIt();}

Page 11: Java conventions

Statements

if (condition) { statements;} else if (condition) { statements;} else { statements;}

Page 12: Java conventions

Always prefer foreach statement//ugly codevoid cancelAll(Collection<TimerTask> c) { for (Iterator<TimerTask> i = c.iterator(); i.hasNext(); ) i.next().cancel();}//clean codevoid cancelAll(Collection<TimerTask> c) { for (TimerTask t : c) t.cancel();}

Statements

Page 13: Java conventions

Naming Conventions

Packages● all lower case● starts with domain name[ com, edu, gov,

mil, net, org]● the rest is organizational standardsExamplescom.sun.engcom.apple.quicktime.v2edu.cmu.cs.bovik.cheese

Page 14: Java conventions

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"

Page 15: Java conventions

Naming Conventions

Methods● Should be verbs● Init small camel case Examplesrun(); getBackground();findByUser(user);

Page 16: Java conventions

Naming Conventions

Variables● Init small camel case● meaningful ExampleProduct product;List<Product> products; Note: Usually IDE suggestions are good candidates.

Page 17: Java conventions

Naming Conventions

Constants● All caps separated by '_' Examplesstatic final int MIN_WIDTH = 4;static final int MAX_WIDTH = 999;static final int GET_THE_CPU = 1;

Page 18: Java conventions

Programming Practices

Providing Access to Instance and Class Variables● Don't make any instance or class variable

public

Page 19: Java conventions

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(); //OKanObject.classMethod(); //AVOID!

Page 20: Java conventions

Programming Practices

Keep the code consiseif (booleanExpression) { return true;} else { return false;}should instead be written as,return booleanExpression;

Page 21: Java conventions

Programming Practices

if (condition) { return x;}return y; Better be, return (condition ? x : y);

Page 22: Java conventions

Classes, objects and methods

Thinking in terms of "object"Not as easy as you think

Page 23: Java conventions

Object

Object Property

Behaviour

Has

Has

Page 24: Java conventions

Object

● An actor (Noun)● Has some properties● Has some behavior

Page 25: Java conventions

Object

Example● An actor (Noun) => Car● Has some properties => Fuel● Has some behavior => start, stop,

accelerate, etc

Page 26: Java conventions

Object

Nothing specialright???

Page 27: Java conventions

Object

car.addFuel(10);

Page 28: Java conventions

Object

car.start();

Page 29: Java conventions

Object

Now let us say we have a driver

Page 30: Java conventions

Object

● Driver drives a car● A car is driven by a driver

Car DriverDriven by Drives

Page 31: Java conventions

The big question

???.addFuel(10); 

VS

Page 32: Java conventions

Object

● Responsibility● Which object is being impacted

○ Which are the properties that are changed○ Which object has changed his behavior

car.addFuel(10);

Page 33: Java conventions

Object

Anyone can add fuel to the car, even the driver Class Driver { ...

public void refillCar() { car.addFuel(10); }}

Page 34: Java conventions

Methods

Behavior of an object,that can change the properties of

that object

Page 35: Java conventions

Relationships

How we set the relationship of driver with the car ???

Page 36: Java conventions

Relationships

Page 37: Java conventions

Relationships

● Driver owns a car● Driver drives a car● Loves a car

Page 38: Java conventions

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

Page 39: Java conventions

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

Page 40: Java conventions

Methods

Examplesuser.isPermitedToEdit(task);story.totalEstimatedHours();classService.findClassesFor(user);classService.findClasseByTeacherAndSemester(teacher, semester);

Page 41: Java conventions

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