DEV Community

Code That Lasts: Mastering Software Design Principles (with Golang)

Have you ever looked at code you wrote six months ago and thought: "Who wrote this monster?"? Relax, it happens to all of us.

In software engineering, writing code that a machine understands is the easy part. The real challenge is writing code that other humans (including your future self) can understand, maintain, and scale. This is exactly where Software Design Principles come into play.

In this extensive article, we are going to break down the fundamental design principles (SOLID, DRY, KISS, YAGNI) and apply them to a real-world scenario using Golang.

Grab your cup of coffee ☕, open your favorite editor, and let's get to it!


🧭 1. The Three Musketeers of Clean Code: KISS, YAGNI, and DRY

Before diving into complex architectures, we need to master the baseline philosophy. These three acronyms are your first line of defense against "spaghetti code."

💋 KISS: Keep It Simple, Stupid

Complexity is the enemy of maintainability. If you have to read a function five times to understand what it does, it's not clever; it's unnecessarily complex.

  • Symptom of violation: 300-line functions, multiple nested loops, variables with cryptic names (x, val, data2).
  • The solution: Divide and conquer. Write declarative code whenever possible.

🔮 YAGNI: You Aren't Gonna Need It

Developers are often frustrated fortune tellers. We frequently build abstractions for use cases that might happen in the future. 99% of the time, those cases never arrive, and we are left with dead, abstract code that only causes confusion.

  • Golden rule: Implement things when you actually need them, never when you just foresee needing them.

🌵 DRY: Don't Repeat Yourself

Every piece of knowledge or logic must have a single, unambiguous representation within a system.

Watch out for "False Duplication": Not all code that looks the same violates DRY. If two identical blocks of code change for entirely different reasons (they belong to different business domains), merging them can accidentally couple the system.


🏛️ 2. The Heart of Design: The S.O.L.I.D. Principles

Proposed by Robert C. Martin (Uncle Bob), these five principles are the compass for object-oriented programming and structured system design. Golang is not a traditional object-oriented language (it doesn't have classes or inheritance), but its use of structs and interfaces makes it a brilliant candidate for SOLID.

[S] Single Responsibility Principle (SRP)

A struct, package, or function should have one, and only one, reason to change.

❌ Bad Design: A service that calculates taxes and also sends emails.
✅ Good Design:

// 📦 package: order
type OrderProcessor struct {
    // Pure order logic
}

// 📦 package: notification
type EmailSender struct {
    // Pure email sending logic
}
Enter fullscreen mode Exit fullscreen mode

[O] Open/Closed Principle (OCP)

Software entities should be open for extension, but closed for modification.

Instead of modifying existing code (and risking breaking it) every time there is a new requirement, you should be able to add new behavior by extending the code. In Go, we achieve this using Interfaces.

[L] Liskov Substitution Principle (LSP)

Objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program.

In Go, this means that any type implementing an interface must fulfill the "contract" of that interface without causing hidden panics or unexpected behaviors.

[I] Interface Segregation Principle (ISP)

Clients should not be forced to depend upon interfaces that they do not use.

Visual Outline of the Principle:

[Client A] ---X---> (Giant Interface: Read(), Write(), Delete(), Audit())
                 |
[Client A] -------> (Small Interface: Read())
Enter fullscreen mode Exit fullscreen mode

In Go, small interfaces (1 to 3 methods) are the norm. Think of io.Reader or io.Writer.

[D] Dependency Inversion Principle (DIP)

High-level modules should not depend on low-level modules. Both should depend on abstractions.

Inverting the dependency means that your business logic dictates the contracts (interfaces), and the infrastructure (databases, external APIs) adapts to them.


🚀 3. Real-World Case: A Payment Processing System in Golang

Let's see all of this in action. Imagine we work at an e-commerce company and we need to process payments.

🚨 Scenario 1: Spaghetti Code (Violating the principles)

Look at this code. Changing anything here is a nightmare. If we want to add "Cryptocurrency" payments, we have to modify the main function, violating OCP and SRP.

package main

import "fmt"

type PaymentProcessor struct{}

// Massive violation of SRP and OCP
func (p *PaymentProcessor) Process(amount float64, method string) {
    if method == "credit_card" {
        fmt.Printf("Connecting to Card API... paying $%.2f\n", amount)
        // 100 lines of HTTP logic...
    } else if method == "paypal" {
        fmt.Printf("Connecting to PayPal... paying $%.2f\n", amount)
        // Another 100 lines of SDK logic...
    }

    // SRP Violation: The payment processor also saves to the DB
    fmt.Println("Saving transaction to MySQL...")
}
Enter fullscreen mode Exit fullscreen mode

✨ Scenario 2: Clean Architecture using SOLID (The Golang Way)

Let's refactor this monster by applying Small Interfaces (ISP), Dependency Inversion (DIP), and keeping it Closed for modification (OCP).

Step 1: Define the Abstractions (Contracts)

We create clear interfaces. The order system doesn't need to know how the payment is processed or how it's saved.

package payment

// ISP: Small, focused interfaces
type Payer interface {
    Pay(amount float64) error
}

type TransactionRepository interface {
    Save(amount float64, status string) error
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Implement the Details (Low Level)

Now we create concrete structs for each payment method. If tomorrow we need Apple Pay, we just create a new struct. The existing code remains untouched! (OCP).

package payment

import "fmt"

// Credit Card Implementation
type CreditCard struct {
    Token string
}

func (cc *CreditCard) Pay(amount float64) error {
    fmt.Printf("💳 Processing $%.2f via Credit Card...\n", amount)
    return nil
}

// PayPal Implementation
type PayPal struct {
    Email string
}

func (pp *PayPal) Pay(amount float64) error {
    fmt.Printf("🅿️ Processing $%.2f via PayPal...\n", amount)
    return nil
}

// Database Implementation
type MySQLRepository struct{}

func (db *MySQLRepository) Save(amount float64, status string) error {
    fmt.Printf("💾 Saving transaction of $%.2f in the MySQL DB...\n", amount)
    return nil
}
Enter fullscreen mode Exit fullscreen mode

Step 3: The High-Level Module (Dependency Inversion)

Our core service now depends on abstractions, not on concrete details.

package payment

import "fmt"

// CheckoutService coordinates the operation, but ignores the details
type CheckoutService struct {
    paymentMethod Payer                  // Injected Dependency
    repository    TransactionRepository  // Injected Dependency
}

// Constructor to ensure we inject the dependencies
func NewCheckoutService(p Payer, r TransactionRepository) *CheckoutService {
    return &CheckoutService{
        paymentMethod: p,
        repository:    r,
    }
}

func (c *CheckoutService) Checkout(amount float64) {
    err := c.paymentMethod.Pay(amount)
    if err != nil {
        fmt.Println("❌ Payment error")
        return
    }

    c.repository.Save(amount, "SUCCESS")
    fmt.Println("✅ Checkout completed successfully")
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Putting it all together in main

The "magic" happens when assembling the pieces.

package main

import (
    "example/payment" // Assuming this is the package path
    "fmt"
)

func main() {
    // 1. Instantiate the infrastructure (Low Level)
    db := &payment.MySQLRepository{}

    fmt.Println("--- Client 1 chooses Card ---")
    cc := &payment.CreditCard{Token: "tok_123"}
    checkout1 := payment.NewCheckoutService(cc, db)
    checkout1.Checkout(150.50)

    fmt.Println("\n--- Client 2 chooses PayPal ---")
    paypal := &payment.PayPal{Email: "user@test.com"}
    checkout2 := payment.NewCheckoutService(paypal, db)
    checkout2.Checkout(89.99)
}
Enter fullscreen mode Exit fullscreen mode

Resulting Architecture Outline:

+-----------------------+       +-------------------+       +-----------------------+
|  High-Level Module    |       |    Abstraction    |       |   Low-Level Module    |
|-----------------------|       |-------------------|       |-----------------------|
|                       |       |                   |       |                       |
|   CheckoutService     | ----->|   Payer (Int.)    |<----- |   CreditCard (Struct) |
|                       |       |                   |       |                       |
+-----------------------+       +-------------------+       +-----------------------+
Enter fullscreen mode Exit fullscreen mode

🎯 Conclusion: Why go through this trouble?

At first glance, applying these principles (especially SOLID) seems to generate more code. And technically, it's true: we went from 1 file and 20 lines to multiple files and structs.

However, the goal of Software Design is not to write less code, but to write code that is highly cohesive and loosely coupled.

Thanks to this design in Go:

  1. Testing: We can easily mock payments and the database using interfaces, allowing for lightning-fast unit tests.
  2. Teamwork: One developer can work on PayPal while another works on CreditCard without running into Git conflicts.
  3. Maintainability: If there is a bug in card processing, we know exactly which file to look at, without fear of breaking the PayPal logic.

Code is like a house. If you build on mud (coupled code), any future renovation will tear down the walls. If you build on a solid foundation (design principles), you can add infinite floors to your skyscraper.

Top comments (0)