Coding Standards-III

White Spaces

White spaces means space characters inserted when the space bar or enter key is pressed on the keyboard. The white spaces in Java code provide readability and better understanding of the code.There are four other types of white spaces in Java: the horizontal tab, the form feed, the carriage return, and the linefeed depending on the Operating System(OS).

The concept of white spaces will be discussed on the basis of the following two aspects:
Blank Lines, Blank Spaces
Blank Lines
The blank lines in Java source code improve readability by arranging the sections of code that are logically related.
Two blank lines are provided between:

  • Sections of a source file.
  • The class and interface definition.

Single blank lines are provided:

  • Between methods.
  • Between the local variable in a method and its first statement.
  • Before a block or a single line comment.
  • Between logical sections within a method to improve readability.

Blank Spaces
Blank space refer to an extra space between two words provided by pressing the space bar key on the keyboard. The blank spaces should be provided under the following conditions.

  1. A keyword followed by a parenthesis should be separated by a space like this
    while (true) {
    …..
    }
  2. A blank space should be provided after commas in argument lists, like this
    public String myarg (a1, a2, a2, a4){
    …….
    }
  3. All binary operators except ‘.’ should be separated from their operands by spaces. Blank spaces should not separate unary operators, such as minus(-), increment(++), and ┬áso on.
    x += y + z;
    x = (y + z) / (p * q);
  4. The expressions in the for statement should be separated by blank spaces. For exmaple
    for ( i = 0; i < 10; i++) {
    …..}
  5. Data type of a variable should be followed by a blank space, like
    public void newmethod( int x , int y) {
    ……
    }

Comments

Java allows to place descriptive comments in your code.This facilitates you in explaining the code and the working of the program with its purpose.
Java supports 3 types of comments

  • A comment of any length with the characters /* and */ like
    /* This application prints out “Hello from Java!”
    Created by: kogent 1/09/08 */.
  • Java also supports one-line comment, using the double slash(//). The Java compiler ignores everything on a line after the // marker ┬álike // single line comment.
  • Finally, Java also supports a documentation comment, which starts with /** and ends with */. This comment is designed to be used with the Javadoc tool, which can create documentsation for automatically. For Example
    /** This application prints out “Welcome To Java!”
    Author: Kavitha*/

    public class App {
    public static void main(String[] args) {
    System.out.println(“Welcome to Java!”); }
    }

One thought on “Coding Standards-III”

Leave a Reply

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