Coding conventions

From Wikibooks, open books for an open world
Jump to: navigation, search

Language Fundamentals Java Programming
Coding conventions
Statements
Navigate Language Fundamentals topic: v  d  e )


The coding conventions for Java are defined by Oracle in the coding conventions document. In short, these conventions ask the user to use camel case when defining classes, methods, or variables. Classes start with a capital letter and should be nouns, like CalendarDialogView. For methods, the names should be verbs in imperative form, like getBrakeSystemType, and shouls start with a lowercase letter.

It is important to get used to and follow coding conventions, as the code becomes better readable. Projects may define new variances of the standard coding conventions to better fit their needs. These may be a list of allowed abbreviations, as too arbitrarily used abbreviations would also make the code difficult to understand for other designers. Another point to consider concerning readability is, of course, to write always the documentation with the code and while writing the code.

One example from the coding conventions is how to define a constant. Constants should be written with capital letters in Java, where the words are separated by an underscore ('_') character. Java defines in the coding conventions a constant arbitrarily as a static final field in a class, although such a field can be modified in real life.

The reason for this diversion is that Java is not 100% object-oriented and discerns between "simple" and "complex" types. These will be handled in detail in the following sections. An example for a simple type is the byte type. An example for a complex type is a class. A subset of the complex types are classes that cannot be modified after creation, like a String, which is a concatenation of characters.

For instance, consider the following "constants":

  1. public class VehicleMotor {
    
  2.   /** Number of motors */
    
  3.   private static final byte MOTORS = 1;
    
  4.  
    
  5.   /** Number of motors */
    
  6.   private static final String MOTOR_NAME = "Mercedes V8";
    
  7.  
    
  8.   /** The motor object */
    
  9.   private static final IMotor THE_MOTOR = new MercedesMotor();
    
  10.  
    
  11.   /**
    
  12.    * Constructor
    
  13.    */
    
  14.   public VehicleMotor() {
    
  15.     MOTORS = 2;   // Gives a syntax error!
    
  16.     THE_MOTOR = new ToshibaMotor(); // Gives a syntax error!
    
  17.     MOTOR_NAME = "Toshiba V12";     // Gives a syntax error!
    
  18.     MOTOR_NAME.toLowercase();       // Gives NO error, but will NOT modify motorName,
    
  19.                                     // as it cannot be changed!
    
  20.     THE_MOTOR.fillFuel(20.5);       // Gives NOT a syntax error, although the object 
    
  21.                                     // THE_MOTOR is modified.
    
  22.   }
    
  23. }
    


Language Fundamentals Java Programming
Coding conventions
Statements