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