J2EE Code Standards

What are the code standard used in J2EE project in real time?

Questions by kainattu

Showing Answers 1 - 9 of 9 Answers

naresh17201

  • Jul 16th, 2010
 

Good kainattu

Code standards are document to promote consistency and efficiency your J2EE project some coding practies can degrade performance.eg

1. String Class: Use string buffer instead of string for sudden changes and modifications.

2. Interface: Linkedlist is the first choice and next Arraylist 
ArrayList list=new ArrayList()//WRONG
List list=new ArrayList(100)//RIGHT

3. Proper variable declaration i.e instance versus static varialer,constants  etc to promote  consistency

4. Performance: Use arraylist, Hashmap instead of vector, Hashtable and
also set the initial capacity

5. Memory: Improper intialization of objects instead of object resue and object pooling ,not closing valuable resources in a finally block.

6. Thread Safety: Not thread safe classes like calender,DecimalFormat etc declaring variables in jsp,storing state information in struts or multithreaded servlet

7. Error Handling: Rethrowing exception without nesting original exception,EJB Methods not throwing for system Exception

8. Calling SetAutoCommit with in container managed transaction,binary OR "|" used instead of logical OR "||",Resultset not being closed on Exceptions, no reuse of code,Invalid  use of inheritance,servlets  using JDBC direct access instead of using DAO,used utility classess instead of flow controller.HTML code in struts action or servlet class etc.

The J2EE application is not developed without depthly Knowing the "Collection Framework"

Sriram_Geek

  • Aug 15th, 2010
 

1) Give meaningful comments.

2) Give meaning variable names.
3) Use lazy initialization of variables that is declare them as and when needed. Need not declare variables upfront like in C.
4) Make sure you use only the required imports. Do not use imports like java.util.* . Use specific imports like java.util.ABC.
5) Always make sure the code is properly indented and has a maximum width on screen so that it is easy to understand.
6)  Make sure you use short circuiting of operators like || and &&.
7) Use specific exception handling like NumberformatException etc. Do not use Exception always.
8) When doing String comparisons, please make sure that Constants are used first and then the variable names. This way you can avoid and extra null check for the variable under comparison.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions