3 min read

Software Design Principles

Software design principles are guidelines that help developers create software that is easy to understand, maintain, and extend. While patterns provide solutions to specific problems, principles provide a general philosophy for writing code.

1. KISS (Keep It Simple, Stupid)

The KISS principle states that most systems work best if they are kept simple rather than made complicated. Simplicity should be a key goal in design, and unnecessary complexity should be avoided.

  • Core Idea: Don't use a complex solution when a simple one will do.
  • Example: Don't use a heavy design pattern or a new library just to format a date string if a built-in function can do it in one line.
  • Benefit: Simple code is easier to read, debug, and test.

2. YAGNI (You Aren't Gonna Need It)

YAGNI is a principle of Extreme Programming (XP) that states a programmer should not add functionality until it is necessary.

  • Core Idea: Don't implement features "just in case" you might need them in the future.
  • The Trap: "I'll add this extra parameter to the function now because we might need it later."
  • The Reality: You usually don't need it, or when you do, the requirements will be different than what you guessed.
  • Benefit: Saves time, reduces code bloat, and keeps the codebase focused on current requirements.

3. DRY (Don't Repeat Yourself)

The DRY principle is stated as "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system."

  • Core Idea: Avoid code duplication. If you change logic, you should only have to change it in one place.
  • Violation (WET - Write Everything Twice): Copy-pasting the same validation logic into 5 different forms. If the validation rule changes, you have to find and update all 5 places.
  • Solution: Extract the logic into a shared function or component.
  • Caveat: Don't apply DRY too aggressively. Sometimes duplication is better than the wrong abstraction (coupling unrelated code just because it looks similar).

4. SoC (Separation of Concerns)

Separation of Concerns is a design principle for separating a computer program into distinct sections such that each section addresses a separate concern.

  • Core Idea: A program should be broken down into distinct modules that overlap as little as possible.
  • Example: HTML (Structure), CSS (Presentation), and JavaScript (Behavior) are separated. In backend, MVC separates Model, View, and Controller.

5. Law of Demeter (LoD)

The Law of Demeter (LoD), also known as the Principle of Least Knowledge, states that a given object should assume as little as possible about the structure or properties of anything else.

  • Motto: "Only talk to your immediate friends."
  • The Rule: A method should not invoke methods of objects returned by another method call.
  • Violation (Train Wreck): order.getCustomer().getAddress().getZipCode(). This couples the code to the structure of Customer and Address.
  • Solution: order.getCustomerZipCode(). The Order object should handle the delegation internally.

programming/solid-principles programming/refactoring-techniques programming/clean-architecture programming/extreme-programming