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

Understanding C1 and C2 Compilers in Java

Understanding C1 and C2 Compilers in Java Understanding C1 and C2 Compilers in Java In Java, the Just-In-Time (JIT) compiler is a part of the Java Virtual Machine (JVM) that improves the performance of Java applications by compiling bytecode into native machine code at runtime. The JIT compiler includes two different compilers, known as the C1 and C2 compilers, each with distinct optimization strategies and purposes. C1 Compiler (Client Compiler) The C1 compiler, also known as the client compiler, is designed for fast startup times and lower memory consumption. It performs lighter and quicker optimizations, which makes it suitable for applications that require quick startup and responsiveness. Key characteristics of the C1 compiler include: Quick Compilation: Prioritizes fast compilation times over deep optimizations. Low Overhead: Consumes less memory and resources during compilation. Profile-Guided Optimization: Ca...

Understanding -XX:+PrintCompilation Output in Java

Understanding -XX:+PrintCompilation Output in Java Understanding -XX:+PrintCompilation Output in Java The -XX:+PrintCompilation flag in the Java Virtual Machine (JVM) prints information about the methods being compiled by the Just-In-Time (JIT) compiler. When you enable this flag, the JVM will output a log of compilation events to the standard output. Each line of the output provides information about a specific method being compiled. Here, I'll explain the meaning of the different columns and markers, specifically focusing on the n , s , and % markers as seen in your example. Explanation of Output Columns and Markers Here's a breakdown of what each column and marker means: Timestamp : The time (in milliseconds) since the JVM started when the compilation event occurred. Compilation ID : A unique identifier for each compilation task within the JVM's lifecycle. Optimization Level : The lev...

Aggregate multiple JSON responses with WSO2 Aggregate mediator

We have 2 APIs which are passing JSON responses as follows {     "name": "Harshana",     "address": "Warakapola",     "vehicleNo": "BGP 3417" } { "name": "madusanka", "address": "colombo", "vehicleNo": "BEG 8765" } Now we need to pass the response by aggregating these responses as a single response. {     "response1": {         "hAddress": "colombo",         "name": "madusanka",         "vehicle": "BEG 8765"     },     "response2": {         "hAddress": "Warakapola",         "name": "Harshana",         "vehicle": "BGP 3417"     } } Okay, We can do this by using the WSO2 Aggregrate mediator in WSO2 ESB. The Aggregate mediator implements the Message Aggregator enterprise integration pattern and aggregates the re...