Tuesday, July 26, 2011

How to write Code in Java?

If you Google then you will find lots of code and solution of your coding issue. but
The most important point is How Code should be?
When you are writing code in Java, As per my knowledge - There are points which should be implemented into code

Key Pointes are :

1. Always generate logs for your application.You can use apache Log4j.jar for that.

2. Don't generate log file into root of your application. Try to create new Log directory into your application,
   and generate new log file for earch Run/Build into log folder

3. Naming convension of log file is important as well. it should be Ex. _DataandTimeStamp.log.
   In order to create new log file each time your applicaton runs, you can create inherit superclass FileAppender
   and overrid it's method and write logic to create new name with timestamp.


4. If your application require Global variable,which will be used throught your appplication(like Configuration) then you
   should make define it into Properties and access it using ResourceBundle from Java.util package.
 

5. Exception Handling is one of the important factor which should be implemented into your program.

6. Don't use static absolute path. Better to use relative path Ex. path config/Resource.properties is better than
   c:/myproject/src/config/Resource.properties


7. Write all your java files into packages rather than in Default package


8. if your application requires some thirdparty jar to be used, then copy all required jars into lib directory and
   all to build path of your application.
 

9. write Ant Script which perform operation like, clean the build,compile java files and generate java classes, Generate
   .jar file from generated classes and add Main-Class to manifest.mf, and run.
   By using Ant Script,It's easy to figure out if there are any compilation or runtime error exist or not.
 

10. Always write Interface.Code should be written such that if there is any need to do customization to existing code in future,
    Then it can be done by writing subclasses which inherited from Super Class,override method and add your own logic into it.
 

11. Sometime using keyword "public" will solve your issues but It's very important to make use of keyword "private and protected"
    along with "Public".
 

12. If there is bunch of code for parsign XML is there, then It's better to write function Ex. parseXML(String XMLFile) into which
    you can pass XMLFilename as parameter and write your logic there.
 

13. keep usage of POJO classes and getter,setter method in it.
 

14. write JUnit classes to test application with various input condition.
 

15. Always make proper versioning of your application so it is easy to figure out which version contains which features.
 

16. Each application should have it's functional document mentioning which functionality included into given version.

Author : Anuj Patel
LinkedIn : http://www.linkedin.com/in/anujjpatel
Blog : http://goldenpackagebyanuj.blogspot.in/

No comments:

Post a Comment