Coding conventions are a set of guidelines for a specific programming language that defines programming style, practices, and methods for each aspect of a program written in that language. These are guidelines for software structural quality.These conventions usually cover indentation, comments, declarations, statements, white space, naming conventions etc.

Software programmers are highly recommended to follow these conventions to improve the readability of their code and make their software maintenance easier. As these coding conventions are not enforced by compilers, it is all up to software programmers.

Comment Convention

  • A comment is a programmer-readable explanation or annotation in the source code of a computer program
  • A programmer should comment any part of their code that requires some clearification or explanation about that code.

The Header Section – should include – created Date, modified date, author, description, Project name
Methods – what the method does
Variables And Constants – if name is not descriptive enough

How To Comment

  • You can add comment in following ways :

1. By using two forward slashes, that will comment everything after that line :
Ex- This line is not commented. // This is commented

2. You can create block of comment starting with /* and ending with */ :
Ex- This is not commented.

/* This is commented
This is also commented
This is also commented */
This is not commented.

3. Comments in XML are introduced with
<!– and can spread over several lines until the terminator –>
Ex- <!– Name of the App –>
<string name=”app_name”>MyProject</string>

Indent Style Convention

Indentation means the physical layout of your code(how the code appears on the screen).

1 – The reason to learn style is to make your program easy to understand and code written with good style simply looks more professional and systematic.

2 – Perhaps the most important style rule you can learn is to indent properly. The compiler cares nothing about indentation, but if you indent well, you make your code so much easier to read, which means you’ll make it easier to debug.

3 – Indentation allows your physical layout to match with the logical layout (what the code means).


The first rule defines that anything inside a block (an open/close brace pair) should be indented. Usually, a good text editor or IDE has support for indentation. You can use 3 spaces of indentation. Some prefer more.

Class Definitions
public class Foo
// COD

 Methods and Instance Variables
public class Foo
private int count ;
private boolean isValid ;
public static void main( String [] args )

Notice how the instance variables and the methods are all lined up 3 spaces in. That’s because they are within a block, and everything is indented in 3 spaces in the block.


Here main() method also has its own block (i.e., the method body). So anything in main() should be indented 3 more spaces in (for a total of 6 spaces).

public class Foo
private int count ;
private boolean isValid ;
public static void main( String [] args )
// Indented 6 spaces in
int val = 3, x = 2 ;
System.out.println( “val is ” + val ) ;

Wrapping Lines

In Source code, lines longer than 80 characters should be avoided. When an expression will not fit on a single line, it should be broken according to these general principles:

  • Break after a comma.
  • Break after an operator.
  • Prefer higher-level breaks to lower-level breaks.


count = number.calculate(bytes, offset, length, value, 0,
long value = ((totalValue < plannedValue ) ?
totalValue : plannedValue );

White Space – Blank Lines :

Blank lines improve readability of your code by setting the sections of code that are logically related.
Two blank lines should be used in the following circumstances:

  • Between sections of a source file
  • Between class and interface definitions
  • One blank line should be used in the following circumstances:
  • Before and after comments.
  • Between methods.
  • Between the local variables in a method and its first statement.
  • Before a block or single-line comment.
  • Between logical sections inside a method to improve readability.

Blank Spaces – Blank spaces should be used in the following circumstances:A keyword followed by its opening parenthesis should be separated by a space.

Example: while (true)

  • A blank space should appear after commas in argument lists.
  • All binary operators except a period ‘.’ should be separated from their operands by spaces.
  • Blank spaces should never separate unary operators such as unary minus, increment (“++”), and decrement (“–”) from their operands.
  • The expressions in a for statement should be separated by blank spaces.

Example: for (expr1; expr2; expr3)

  • Casts should be followed by a blank space.
    getValue((byte) Num, (Object) y);
    getValue((int) (c + 15), (int) (j + 4) );

However blank space should not be used between a method name and its opening parenthesis. This helps to distinguish keywords from method calls.

Naming Convention

Reasons for using a naming convention include the following:

  • To enable code reviews to focus on more important issues than arguing over syntax and naming standards.
  • To reduce the effort needed to read and understand source code.

Class names are UpperCamelCase
ex- MyClass
Method names are LowerCamelCase
ex- getCurrentDate()
Constants names are ALL_CAPS
Variables are CamelCase
ex- currentDate

Exception Handling –

  • Users have high expectations for the code we produce.
  • Users will use our programs in unexpected ways.
  • Due to design errors or coding errors, our programs may fail in unexpected ways during execution
  • It is our responsibility to produce quality code that does not fail unexpectedly.
  • Consequently, we must design error handling into our programs.

Exceptions – What are they?

  • An exception is a representation of an error condition or a situation that is not the expected result of a method.
  • Exceptions are built into the Java language and are available to all program code.
  • Exceptions isolate the code that deals with the error condition from regular program logic.

Types Of Exceptions

Checked exceptions – Exceptions which are checked at the time of Compilation are called Checked Exception. All exceptions other than Runtime Exceptions are known as Checked exceptions as the compiler checks them during compilation to see whether the programmer has handled them or not. If these exceptions are not handled in the program, you will get compilation error. For example, SQLException, IOException, ClassNotFoundException etc.

Unchecked Exceptions – Runtime Exceptions are also known as Unchecked Exceptions. These exceptions are not checked at compile-time so compiler does not check whether the programmer has handled them or not but it’s the responsibility of the programmer to handle these exceptions and provide a safe exit. For example, ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc.

How to handle Exceptions

  • In Exception handling we are using try-catch mechanism to handle the exception.
  • Whenever the code throws a checked exception, you can decide to handle the exception yourself, or pass the exception “up the chain” (to a parent class).
  • To handle the exception, write a “try-catch” block. To pass the exception “up the chain”, declare a throws clause in your method or class declaration.

Try Block

The try block contains set of statements where an exception may trigger. A try block is always followed by a catch block, which handles the exception that occurs in associated try block. A try block must be followed by catch blocks or finally block or both.

Syntax of try block
//statements that may cause an exception

Catch Block

A catch block is used to handle the exceptions, catch block must follow the try block. A single try block can have several catch blocks associated with it. We can catch different exceptions in different catch blocks. When an exception occurs in try block, the corresponding catch block that handles that particular exception executes. For example if an arithmetic exception occurs in try block then the statements enclosed in catch block for arithmetic exception executes.

Syntax of try catch in java
//statements that may cause an exception
catch (Exception e)‏
//error handling code

Mechanism – Try-Catch Mechanism

  • Wherever your code trigger an exception, the normal code logic is placed inside a block of code starting with the “try” keyword:
  • After the try block, the code to handle the exception should be placed in a block of code starting with the “catch” keyword.
  • There may be an optional “finally” block. This block contains code that is ALWAYS executed, either after the “try” block code, or after the “catch” block code.
  • Finally blocks can be used for operations that will be always executed no matter what (i.e. cleanup operations such as closing a file)

Coding Exceptions


… normal program code
catch(ArithmeticException e)
… exception handling code
catch(Exception e)
… exception handling code


1st Row Valid
2nd Row Invalid


By-  Neha Gupta

Coding standards and guidelines in Programming language

Post navigation

One thought on “Coding standards and guidelines in Programming language

  1. Well done Neha!!!

    This post is very useful, if everyone will start following it, definitely their code will become well structured and this also will give reflection that this standard code is written by professionals.

    Hoping that in future too you will come forward with more such posts.


Leave a Reply

Your email address will not be published. Required fields are marked *