Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Java conventions


Published on

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

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Java conventions

  1. 1. Java conventionsA.K.M. Ashrafuzzaman
  2. 2. Java code conventionsSome of the code conventions of Java,suggested by sun, followed by the articlebelow, Code Conventions for the Java TMProgramming Language Revised April 20, 1999
  3. 3. Java code conventions● Indentation● Statements● Naming Conventions● Programming Practices Classes and methods good, bad and ugly
  4. 4. IndentationFour spaces should be used as the unit ofindentation  
  5. 5. Line LengthAvoid lines longer than 80 characters.
  6. 6. Wrapping LinesBreak after a comma someMethod(longExpression1, longExpression2, longExpression3, longExpression4, longExpression5); var = someMethod1(longExpression1, someMethod2(longExpression2, longExpression3));
  7. 7. Wrapping LinesBreak before an operator longName1 = longName2 * (longName3 + longName4 - longName5) + 4 * longname6; // PREFERlongName1 = longName2 * (longName3 + longName4 - longName5) + 4 * longname6; // AVOID 
  8. 8. Wrapping Lines● If the above rules lead to confusing code or to code thats 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
  9. 9. 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) { ...}
  10. 10. Wrapping Lines//DONT 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();}
  11. 11. Statementsif (condition) { statements;} else if (condition) { statements;} else { statements;}
  12. 12. StatementsAlways prefer foreach statement//ugly codevoid cancelAll(Collection<TimerTask> c) { for (Iterator<TimerTask> i = c.iterator(); i.hasNext(); );}//clean codevoid cancelAll(Collection<TimerTask> c) { for (TimerTask t : c) t.cancel();}
  13. 13. Naming ConventionsPackages● all lower case● starts with domain name[ com, edu, gov, mil, net, org]● the rest is organizational
  14. 14. Naming ConventionsClasses/Interfaces● Should be noun● Cap init camel case Note: If you can not name a class then that isan indication of "Something went wrong, whenyou were splitting the responsibility"
  15. 15. Naming ConventionsMethods● Should be verbs● Init small camel case Examplesrun();getBackground();findByUser(user);
  16. 16. Naming ConventionsVariables● Init small camel case● meaningful ExampleProduct product;List<Product> products; Note: Usually IDE suggestions are good candidates.
  17. 17. Naming ConventionsConstants● All caps separated by _ Examplesstatic final int MIN_WIDTH = 4;static final int MAX_WIDTH = 999;static final int GET_THE_CPU = 1;
  18. 18. Programming PracticesProviding Access to Instance and ClassVariables● Dont make any instance or class variable public
  19. 19. Programming PracticesReferring to Class Variables and Methods Use class to access the method, as theresponsibility goes to the class not the object. AClass.classMethod(); //OKanObject.classMethod(); //AVOID!
  20. 20. Programming PracticesKeep the code consiseif (booleanExpression) { return true;} else { return false;}should instead be written as,return booleanExpression;
  21. 21. Programming Practicesif (condition) { return x;}return y; Better be,return (condition ? x : y);
  22. 22. Classes, objects and methods Thinking in terms of "object" Not as easy as you think
  23. 23. Object Has Object Property Has Behaviour
  24. 24. Object● An actor (Noun)● Has some properties● Has some behavior
  25. 25. ObjectExample● An actor (Noun) => Car● Has some properties => Fuel● Has some behavior => start, stop, accelerate, etc
  26. 26. Object Nothing special right???
  27. 27. Objectcar.addFuel(10);
  28. 28. Objectcar.start();
  29. 29. ObjectNow let us say we have a driver
  30. 30. Object● Driver drives a car● A car is driven by a driver Driven by Drives Car Driver
  31. 31. The big question ???.addFuel(10);  VS
  32. 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. 33. ObjectAnyone can add fuel to the car, even thedriver Class Driver { ... public void refillCar() { car.addFuel(10); }}
  34. 34. Methods Behavior of an object, that can change the properties of that object
  35. 35. Relationships How we set the relationship of driver with the car ???
  36. 36. Relationships
  37. 37. Relationships● Driver owns a car● Driver drives a car● Loves a car
  38. 38. In shortWhen 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. 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
  40. 40. MethodsExamplesuser.isPermitedToEdit(task);story.totalEstimatedHours();classService.findClassesFor(user);classService.findClasseByTeacherAndSemester(teacher, semester);
  41. 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