Question of the Week
What are the principles to be followed for developing a good maintainable project in an Object Oriented Programming language like Java?
|
|
Read this article if you have at least some experience as a Java programmer and
knowledge of OOPs concepts. Try to follow these basic details while developing the
project so that you can achieve good level of maintainability.
(1) Hide Implementation details
As Java is an object oriented programming language, it provides encapsulation. Always hide the implementation details by utilizing the encapsulation. Encapsulation hides the internal state of the object and performs all the interactions through methods. So, the internal implementation details will be safe from outside hackers.
|
(2) Don’t do much subclassing
It is always better not to subclass more than two to three levels deep. Only create a subclass if it is strongly necessary for adding important specific functionality in a separate class.
(3) Use local variables when possible rather than instance variables
When applicable, it is always better to make use of variables that have small scope like local variables declared in blocks, methods or loops.
(4) Initialize local variables
Local variables have to be assigned a value before they are used because local variables are not initialized automatically like the instance or static variables.
(5) Utilize Overloading
If two or more methods are doing the same job but have to handle different method arguments, assign the same name to all those methods but with unique method arguments.
(6) Don’t use too much arguments in a method
If the method is taking too much arguments then think of creating its own class with all that stuff.
(7) Utilize Interfaces
If you want to protect your code by showing only the definition to outside world and hiding the implementation details, make use of interfaces
(8) Only create an object when it is really necessary
Avoid creating objects thinking that they may come to use later in the code as this may lead to lag in the performance and unnecessary confusions.
(9) Manage objects
Always watch the objects you have created. If they are no longer useful, set them to null for better memory management and avoid confusion.
(10) Don’t nest if or if else blocks heavily
Always better to design and follow simple logics rather than using deeply nested blocks. Consider writing separate methods for inserting those logics.
(11) Use meaningful names
Always use meaningful names for variables, methods, classes, interfaces and Exceptions.
(12) Declare a method as static only if necessary
Using non-static methods is always better than using static methods as the non-static methods contain the knowledge of the current object. But if you want to know some things which are not object specific, then make use of static methods.
(13) Getters and Setters
Using getter and setter methods for accessing and setting the value of a property is a good standard.
(14) Make use of standard classes
Utilize the classes and code that are already in core Java APIs rather than writing your own code for the already existing ones. This will provide the opportunity for you to concentrate more on your business logic.
(15) Use Exceptions and be specific while throwing Exceptions
Go for exceptions options rather than error codes. Declare specific Exception types rather than the generic ones. Always provide useful messages while throwing Exceptions which will help for the users to easily fix the code. You can also create your own Exceptions according to your code if you can’t find the appropriate one in java.lang.
Good Luck with Java Project.
|