Monday, April 5, 2010

Coding Guidelines - Java Programming Fundamentals

  • Your Java programs should always end with the .java extension.
  • Filenames should match the name of your public class. So for example, if the name of your public class is Hello, you should save it in a file called Hello.java.
  • You should write comments in your code explaining what a certain class does, or what a certain method do.

  • In creating blocks, you can place the opening curly brace in line with the statement, like for example,
  • public static void main(String[] args){
    
    or you can place the curly brace on the next line, like,
    public static void main(String[] args)
    {
    
  • You should indent the next statements after the start of a block, for example,
  • public static void main(String[] args){
        System.out.println("Hello ");
        System.out.println("world");
    }
    

  • For names of classes, capitalize the first letter of the class name. For names of methods and variables, the first letter of the word should start with a small letter. For example:
  • ThisIsAnExampleOfClassName thisIsAnExampleOfMethodName
  • In case of multi-word identifiers, use capital letters to indicate the start of the word except the first word. For example, charArray, fileName,ClassName.
  • Avoid using underscores at the start of the identifier such as _read or _write.

  • In defining a long value, a lowercase L is not recommended because it is hard to distinguish from the digit 1.

  • It always good to initialize your variables as you declare them.
  • Use descriptive names for your variables. Like for example, if you want to have a variable that contains a grade for a student, name it as, grade and not just some random letters you choose.
  • Declare one variable per line of code. For example, the variable declarations,
  • double exam = 0;
    double quiz = 10;
    double grade = 0;
    
    is preferred over the declaration,
    double exam = 0, quiz = 10, grade = 0;
    
Java, JavaScript, HTML, XHTML, AJAX, CSS, etc.