Essential Software Design Principles To Improve Code Quality
Posted By : Kiran Sharma | 09-Jan-2020
In this blog post, we will learn about a good approach to writing our code by studying software design principles. Let's discuss DRY and KISS software design principles.
DRY states " Don't Repeat Yourself " and KISS states " Keep It Simple Stupid ".
DRY - It's a basic principle of software development that aims to reduce the repeated information. As per this, the logic must have single, unambiguous representation. It is an abstraction of common information and putting it into a single place. The basic fundamental of this approach is to discourage repetition.
for e.g.
class VehicleService {
void serviceTwoWheeler() {
System.out.println("Service Two Wheeler");
}
void serviceFourWheeler() {
System.out.println("Service Four Wheeler");
}
}
Here we have two methods in VehicleService class, in which two-wheelers and four-wheelers will be serviced. There can be different ways of servicing two and four-wheelers but some of the services can be common like washing and polishing the vehicle. Let's update the above code with these servicing tasks.
class VehicleService {
void serviceTwoWheeler() {
System.out.println("Service Two Wheeler");
// washing the vehicle
// polishing the vehicle
}
void serviceFourWheeler() {
System.out.println("Service Four Wheeler");
// washing the vehicle
// polishing the vehicle
}
}
Now we can see the problem here, whenever some procedure changes both the methods will also change. And this leads to code duplication as one code is repeating for the same purpose. So as per DRY principle, we can write a separate method that performs the extra servicing tasks which are common for both categories of vehicles. Thus it makes code cohesive and more effectively maintainable.
class VehicleService {
void serviceTwoWheeler() {
System.out.println("Service Two Wheeler");
}
void serviceFourWheeler() {
System.out.println("Service Four Wheeler");
}
void performCommonTasks() {
System.out.println("Performing common tasks on both vehicles");
}
}
To achieve "DRY" divide your logic into smaller reusable parts and use it where you want.
KISS -
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Kiran Sharma
Kiran has good knowledge of java with Servlets, JSPs, Spring, and hibernate frameworks. She is very honest towards her work. Her hobby is listening to music.