Coding Standards for JAVA - abdulmukit98/techshopJU GitHub Wiki

Every member in team need to follow this coding standards for generating bug free code.

Naming Convention

  • Variable name in activity start with (camel case) activity short form.

    Ex: main activity, variable name should be

    int mNum1, mnNm2, mSum; mSum = mNum1 + mNum2;

  • Function parameter also be descriptive.

    public int sum ( num1, num2 )
    {
        ……
    }
    
  • Class name must be in Pascal Casing

    public class LoginManager 
    {
           	………
    }
    
    public class RegistrationForm 
    {
          	………..
    }
    
  • Interface name must be declare also in Pascal casing Starting with I

    public interface InterfaceDemo 
    {
        public void displayName(String name);
    }
    
  • Variable name must be in lowerCamelCasing

    private int bluetoothPair, pairId;
    
  • Constant name must be all upper case separating with underscore.

    MAX_LENGTH, CONNECTION_TYPE;
    
  • Method name in camel case

    getID( );
    computeSum();
    

Layout Conventions

  • Basic indentation should be 4 spaces.

    If( num1 > num2 )
        System.out.println(num1 –  num2);
    
  • If-else statement should have following form

    If ( condition ) 
    {
          Statement1;
    } 
    Else IF (condition 2) 
    {
      Statement2;
    } 
    Else 
    {
        Stmt3;
    }
    
  • For statement should have following form

    for( int i = 0; i<100; i++) 
    {
        System.out.format(“I = %d”,i);
    }
    
  • Try-Catch statement should be written in following form

    Try  
    {
        Statement;
    } 
    catch (Exception exception) 
    {
        statements;
    }
    
  • Switch statement should be written in following form

    Switch (condition) 
    {
      Case 1:
       Statement;
       Break;
      Case 2:
       Statement;
       Break;
      Default:
          Statement ;
      Break;
    }
    
  • The sequence of item appear in code is following order

    Constant
    Global Variable
    Class
    Interface
    Global Functions
    
  • Method should be declared in following order

    private / protected
    public
    

Comments

  • All comment should be written in English.
  • Comments should be indented relative to their position on code
      While (true) 
      {
          // Do Something
          Something();
      }
  • Block comment will be written as shown below
       /*
	*  This is a block comment
	*  Line 1
	*  Line 2
	*  Line 3
        */