Most Important & Necessary Programming tips to remember for Java Developers

Try to write self-documented programs:  
Use logical variable names and function names :    
not :  int x;     
but :  int studentCount;

Comments :
Use comments whenever something is unclear. Think that you are telling some other person about your code. (After a long time, this person might be you)

Coding Conventions :
  • Class names start with Capital
         class MyClass {
  • Instance names, fields, function names start with lower, etc.
         Object  myObject ;
         int myInt ;
         void myFunc() { …

  • Static Final variables are All-Capitalized
         static final MAXVALUE = 200 ;

Necessary Tabs and spaces in Blocks. (Theoretically you can write a long program in a single line) 
Use getter, setter, and is methods.
        int getCount() ;
        int setCount() ;
        isEmpty() ;

Self-checking classes 
  • Use main() almost in every class to check class functionality. 
  • You can make an expected output check, in order to verify changes in the class.
In highly coupled classes, consider to make inner classes.

Keep method definitions and scopes as short as possible. (For easy visualization and readability.)

Compiler-time errors are better then run time errors.

Prefer Interface definition to Abstract Class
       Not : abstract class X { abstract void f() ; }
       But : interface X { void f() ;
Spelling problems in overriding Classes
        class X { void encode() { … } }
        class Y extends X { void Encode() { … } }    // Does not overload the method

Use Java Container Library classes
List l = new ArrayList() ;
Map hm = new HashMap() ;
Set s = new HashSet() ;
List  myStack = new LinkedList() ;


Recent Posts