java coding-convention and cleancode

This document consolidates the best practices and recommendations of the Java developers community gained from the past ten years of language use.

Rule 1. a) Don't use the wild-card character in package import statement. Always use the qualified name of package with import statement.

Ex. import* // wrong
     import; //Right
       b) Import statement should be alphabetically sorted, separated in groups with a blank line.
         .static import
         .java, javax , then org and com package
         .others imports

  Ex. import static java.lang.Math.min;

      import javax.servlet.GenericServlet
      import org.springframework.context.ApplicationContext;

      import backtype.storm.tuple.Values;

Rule 2. One class per file. The Name of the file must be the same as the class name.

Rule 3. Avoid deep Nesting: Too many levels of nesting can make code harder to read and follow.

Rule 4.Class names should be nouns unless you have a good reason for it not to be a noun. First letter of the class name should be in capital letter.

      Ex class MTBolt{
         // code goes here

Rule 5. Name of the methods using verb-object pair, such as
            Ex. readTranslatedFile();

Rule 6. Methods or functions returning a boolean type should generally start with verb is, or alternatives that fits better in   some   situations like has/have, can or should, etc.
     isValid(); isOpen();

Rule 7. a) Variables should be named so that they make it clear what it contains.
Variable names should always be easy-to-read, be short yet meaningful, mixed case with a lower-case first letter English words.

        b)Use plural names for arrays/collections of objects.
        c) Constant (static final) variable names should be in all capital letters and separate words in the name with the underscore, i.e.,   PRIORITY_NORMAL,

Clean code rules

1. Keep code where it belongs.

2. Keep methods short. single responsibility for a single function. means function    should do one thing. don't use boolean arguments because    they cause confusion. Boolean (flag) arguments loudly declare that the function does more than one thing.

3. Avoid OS dependent behavior, e.g., Unix-centric constants "\n", "/", etc.

4. Make all fields private. provide public get and set method to read and write properties. it restricts the direct access to properties and field and using get and set you can validate your data.

5. Declare a local variable as close as possible to its first use.

6. Document all your methods even the private ones if they are complex.

7. Every class should inside a package

8. Private class variables should have underscore suffix.
underscore  nicely resolves the problem of finding reasonable variable names for setter and getter methods: 

void setName(String name)
    name = name;
9. The term compute can be used in methods where something is computed.

10. Array specifiers must be attached to the type not the variable.
    ex int a[] = new int[10] //wrong
         int[] a = new int[10]//Right



2. Clean Code: A Handbook of Agile Software Craftsmanship:
Robert C. Martin

1 comment: