JAVA Programming Best Practices

By | October 6, 2014

Programming in any language should be done in accordance with the standards and best practices should be followed in order to ensure that developed code is maintainable, re-usable, extendable and presentable.

In this post, I am going to talk about JAVA Programming Best Practices. However, many of the things discussed here will be true in general for other programming lanauges as well.

Follow proper Naming Conventions

Coding Conventions are the guidelines and recommendations which should be followed to write consistent and standardized code. Conventions are not hard and fast rules and you can write dirty yet Working code without following the conventions. However, If you follow the conventions; your code will be neat and consistent throughout and easy to understand.

Some of the conventions that JAVA programmers should follow are given below:

      • For methods returning Boolean; has, can etc. should be used. E.g. hasText(), canProcess()
      • For Boolean variables, Is prefix should be used. E.g. IsValid, isTested
      • For Private variables in the classes, Underscore (_) suffix should be used with the names. E.g. age_, status_
      • Java method names should always be Verbs and should have mixed cases starting with small case. E.g. getAge, printData, validateForm
      • When defining constants (Final variables), use upper case only. E.g. MAX_RECORDS, AVG_SALARY
      • Variable names should be in mixed case and should start with small case. E.g. studentAge, partnerFile
      • Names of Packages should be in small letters. E.g. tutorialpackage,mypackage,com.tutorialpedia.tutorial


 Comment the code as and when needed

Code written without any useful comments is always difficult to extend and reuse. With proper commenting, code becomes more understandable for other programmers and hence future enhancements and bug fixes take less time.  Comments should be excessively added to the code with proper wordings and correct grammar.


Write well organized, reusable code

In order to fulfill certain requirements; you can write the whole code logic in one class jumbled up or you can write the same logic properly distributed in various packages, classes and their methods. When writing code for a JAVA application; think in terms of re-usable components so that you don’t need to re-invent the wheel every time. For example; If you are writing code for Logging into a file or a database; create a separate method for this so that same method can be called every time logging is required.

For data, avoid hard coding as much as possible. Use of configuration files should be prioritized so that configuration changes can be made later without re-coding and re-deployment the application.

When writing JAVA code, utilize the open source API’s as much as you can instead of writing everything from the scratch.


Don’t keep loopholes–Handle Exceptions Properly

Exceptions are a known phenomenon and every programmer must be critical about it. In order to ensure that your application doesn’t crash abruptly; handle all possible exception scenarios efficiently in your JAVA code.


Avoid Unnecessary Imports

When writing import statements in your JAVA class; you should know what you are doing and what you need to do. Don’t bluntly import whole packages if you need only a handful of classes. For example, If you need to use BufferedInputStream, simply use import instead of import*

Ajmal Abbasi

Ajmal Hussain Abbasi is a TIBCO Consultant By Profession with more than 6 years experience in TIBCO products. He has extensive practical knowledge of TIBCO Business Works, TIBCO Spotfire, TIBCO BE, EMS and TIBCO ActiveSpaces. He has worked on a number of highly critical integration projects in Telecom sector by using his skills in Tibco Designer, Adapters, TIBCO EMS, RV, Administrator, TIBCO BE, TIBCO ActiveSpaces etc. Ajmal Abbasi is also experienced in developing solutions using Oracle PL/Sql, Linux and Java. You can contact Ajmal Abbasi for Consultancy, Technical Assistance and Technical Discussions.

More Posts - Website - Facebook - LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *