Skip to main content

Software Engineering Principles: DRY, KISS, and YAGNI

Software Engineering Principles: DRY, KISS, and YAGNI

Software Engineering Principles: DRY, KISS, and YAGNI

DRY (Don't Repeat Yourself)

Principle: The DRY principle emphasizes the importance of reducing repetition within code. Repetition can lead to more errors, higher maintenance costs, and inconsistency. When the same piece of code or logic appears in multiple places, it should be abstracted out into a single location that can be reused.

Example:

Without DRY:


public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }

    public int subtract(int a, int b) {
        return a - b;
    }

    public int multiply(int a, int b) {
        return a * b;
    }

    public double divide(int a, int b) {
        return a / b;
    }

    public int addAndSubtract(int a, int b, int c) {
        return a + b - c; // repeated addition and subtraction logic
    }
}
    

With DRY:


public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }

    public int subtract(int a, int b) {
        return a - b;
    }

    public int multiply(int a, int b) {
        return a * b;
    }

    public double divide(int a, int b) {
        return a / b;
    }

    public int addAndSubtract(int a, int b, int c) {
        return subtract(add(a, b), c); // reuse add and subtract methods
    }
}
    

KISS (Keep It Simple, Stupid)

Principle: The KISS principle advocates for simplicity in design and implementation. The idea is to avoid unnecessary complexity, making code easier to understand, maintain, and debug.

Example:

Without KISS:


public class OrderProcessor {
    public void processOrder(Order order) {
        if (order != null && order.getItems() != null 
               && !order.getItems().isEmpty() 
               && order.getCustomer() != null) {
            // complex processing logic
            for (Item item : order.getItems()) {
                // process each item
            }
        } else {
            // handle invalid order
        }
    }
}
    

With KISS:


public class OrderProcessor {
    public void processOrder(Order order) {
        if (!isValidOrder(order)) {
            // handle invalid order
            return;
        }
        // simple processing logic
        for (Item item : order.getItems()) {
            // process each item
        }
    }

    private boolean isValidOrder(Order order) {
        return order != null && order.getItems() != null 
                    && !order.getItems().isEmpty() 
                              && order.getCustomer() != null;
    }
}
    

YAGNI (You Aren't Gonna Need It)

Principle: YAGNI encourages developers to avoid adding functionality until it is necessary. This helps in reducing the complexity and avoiding wasted effort on features that may never be used.

Example:

Without YAGNI:


public class UserManager {
    public void createUser(String username, String password) {
        // create user
    }

    public void createUser(String username, 
           String password, String email, 
           String phoneNumber, String address) {
        // create user with additional details
    }
}
    

With YAGNI:


public class UserManager {
    public void createUser(String username, String password) {
        // create user
    }
}
    

Summary

  • DRY (Don't Repeat Yourself): Avoid duplication of code by abstracting repeated logic into reusable methods or components.
  • KISS (Keep It Simple, Stupid): Keep code simple and straightforward to make it more understandable and maintainable.
  • YAGNI (You Aren't Gonna Need It): Do not add functionality until it is necessary, preventing unnecessary complexity.

These principles collectively help in writing clean, maintainable, and efficient code.

Comments

Popular posts from this blog

How to use WSO2 Class Mediator in WSO2 ESB

The  Class Mediator  creates an instance of a custom-specified class and sets it as a mediator. If any properties are specified, the corresponding setter methods are invoked once on the class during initialization. Use the Class mediator for user-specific, custom developments only when there is no built-in mediator that already provides the required functionality.  The syntax of Class Mediator in ESB < class   name= "class-name" >     <property name= "string"   value= "literal" >     </property> </ class > Creating a Class Mediator lets use the Eclipse  WSO2 Developer Studio Create a New  Mediator project by selecting File --> New --> project --> Mediator Project Now you have class mediator by extending the AbstractMediator class. Then you need to implement the mediate methods Sample class mediator implementation is as follows. package lk.harshana; import org.apache.synapse.Mess

One to Many Mapping using Spring boot

In this blog, I will explain how to use one-to-many mapping in Spring boot Application What you need? JAVA MySql Eclipse IDE ( whatever you like IDE, I'm using Eclipse for this example) Maven ( you can use the Gradle as well) Initial Plan I will create a spring boot application project using the  Spring Initializer  web tool and import the project as a maven project. after configuring the all necessary setting, I will code for one-to-many mapping. Below diagram is the database model diagram which we going to install using the spring boot application. Let's Start to Code. You need to configure the application.properties file for database connections. add the following content to the src/main/resources/application.properties spring.datasource.url=jdbc:mysql://localhost:3306/learning spring.datasource.username=root spring.datasource.password=root spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.hibernate

When To Use Indexes In MySQL

When deciding when and how to create an index in your MySQL database, it's important to consider how the data is being used. Let's say you have a database of  students . We will create it like this: CREATE TABLE `students` ( `id` int ( 11 ) NOT NULL AUTO_INCREMENT , `first_name` varchar ( 255 ) DEFAULT NULL , `last_name` varchar ( 255 ) DEFAULT NULL , `class` varchar ( 255 ) DEFAULT NULL , PRIMARY KEY ( `id` ) ) ENGINE = InnoDB Indexes are best used on columns that are frequently used in where clauses, and in any kind of sorting, such as "order by". You should also pay attention to whether or not this information will change frequently, because it will slow down your updates and inserts. Since you wont frequently be adding students, you don't have to worry about the inserts Let's say that you will be looking up the students with a web interface and the end user will be typing in the students name to find them, since r