Java Programming Best Practices

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 languages 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 java.io.BufferedInputStream instead of import java.io.*

Ajmal Abbasi

Ajmal Hussain Abbasi is Integration Consultant By Profession with 13+ years experience in Integration domain mainly with TIBCO products. He has extensive practical knowledge of TIBCO Business Works, TIBCO Cloud, TIBCO Flogo, TIBCO Mashery, TIBCO Spotfire, EMS and TIBCO ActiveSpaces. He has worked on a number of highly critical integration projects in various sectors by using his skills in TIBCO Flogo, TIBCO API Management (Mashery), TCI, Tibco Designer, TIBCO Business Studio, Adapters, TIBCO EMS, RV, Administrator, TIBCO BE, TIBCO ActiveSpaces etc. Ajmal Abbasi has experience with MuleSoft ESB as well. Ajmal Abbasi is also experienced in the area of API Management particularly with WSO2 API management platforms. Ajmal Abbasi is also experienced in developing solutions using Core Java and J2EE Technologies. You can contact Ajmal Abbasi for Consultancy, Technical Assistance and Technical Discussions.

More Posts - Website - Facebook - LinkedIn - YouTube

Leave a Reply

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