Java Standards

Java Standards


In Java, the coding guidelines are the set of rules that are followed by the developer during application development. These guidelines provide readability to other developer and user who are dealing with the project. The guidelines must be followed because an application is not developed by a single programmer. Some major coding guidelines includes the following:

  • Naming conventions
  • Curley Braces
  • Indentation
  • White spaces
  • Comments

Naming conventions

These are the rules for naming variables, methods, constants, classes, interfaces, etc. Generally, Java follows the camel case convention. It describes the following:

  • The class and interface name must be noun, and the first letter of each internal word should be capitalized.
    Ex: Gun, Man
  • The method name must be verb in mixed case, each first letter should be in lower case with the first letter of each internal word should be capitalized.
    Ex: main()methodA()
  • All the constants should be in capital letters. 
    Ex: Int, String, Double
  • The variable name must be a meaningful letter or word.
    Ex: health, total



Curley Braces

A curly brace is applied at the end of the line that denotes the starting of the class, method, and loops. The closing braces should by lined up vertically where the opening braces is lined up. The closing should be placed at a separate line (new line). For example, consider the following loop.

    class  A {

        public void m() {
        
        }

    }


Indentation

The indentation should be 4 spaces. By pressing the Tab key, we get exactly 8-spaces. Indentation can be achieved by the space character and the tab characters.

The recognized standard for increasing readability of each line is:

  • Apply indentation to alike items in a vertical list (such as end-of-line comments, and identifiers in declarations).
  • Surround the binary operators (including assignment) by spaces.
  • Follow a semicolon or comma by a space.
  • Keep space between two keywords like "if", "while", "return", "catch", "switch", "for" and a succeeding parenthesis.
  • Excess parentheses help to highlight the structure of expressions. But we should avoid many nested parentheses because it may lead to error.
  • It is good to put an extra line to differentiate between the important piece of code.

White Spaces

It also plays a vital role in readability of the program. Any mathematical operations (+, -, *, %, etc.) should be surrounded by a space character. 

 Ex: a = (b + c) - this is correct

        a=(b+c) - this is not correct


Comments ( // )

Comments in programming, is very important because it enhance the readability of the program. It contains the relevant information regarding program. Note that comments are ignored by the compiler.

Why do we use comments in a code?

  • Comments are used to make the program more readable by adding the details of the code.
  • It makes easy to maintain the code and to find the errors easily.
  • The comments can be used to provide information or explanation about the variable, method, class, or any statement.
  • It can also be used to prevent the execution of program code while testing the alternative code.

Ex: 1) //comment1

      2) // single line comment
            a = (b + c)

      3) if(x == 2){
                a = false //Trailing comments
           }
    
      4) if(x == 2){
                //a = false  (we can remove the code temporary)
           }


Comments

Popular posts from this blog

Java Language - Object Generating & Objects / Java classes

Introduction of Java Programming Language