Get the FREE Ultimate OpenClaw Setup Guide →

golang-design-patterns

npx machina-cli add skill saifoelloh/golang-best-practices-skill/design-patterns --openclaw
Files (1)
SKILL.md
4.2 KB

Golang Design Patterns & Refactoring

Expert-level code refactoring and design pattern application for Go. Detects code smells, suggests refactoring strategies, and applies proven design patterns to improve maintainability.

When to Apply

Use this skill when:

  • Refactoring complex or legacy code
  • Reducing technical debt
  • Extracting reusable patterns
  • Simplifying large functions (God Objects)
  • Improving code maintainability
  • Applying Gang of Four patterns to Go

Rule Categories by Priority

PriorityCountFocus
High2Critical refactoring needs
Medium11Code quality improvements

Rules Covered (13 total)

High-Impact Patterns (2)

  • high-god-object - Extract logic from 300+ line functions
  • high-extract-method - Name complex code blocks with descriptive methods

Medium Improvements (11)

  • medium-primitive-obsession - Replace primitives with value objects
  • medium-long-parameter-list - Use parameter objects for >5 params
  • medium-data-clumps - Extract repeated parameter groups
  • medium-feature-envy - Move logic closer to data
  • medium-magic-constants - Replace magic numbers with named constants
  • medium-builder-pattern - Fluent API for complex construction
  • medium-factory-constructor - Validated object creation
  • medium-introduce-parameter-object - Group related parameters
  • medium-switch-to-strategy - Replace type switches with interfaces
  • medium-middleware-decorator - Decorator pattern for http.Handler
  • medium-law-of-demeter - Reduce coupling, avoid message chains

Common Refactoring Patterns

God Object → Extracted Methods

// ❌ 500 line function
func (u *Usecase) Process() { ... }

// ✅ Extracted methods
func (u *Usecase) Process() {
    u.validate()
    u.transform()
    u.persist()
}

Primitive Obsession → Value Object

// ❌ Primitive types
func CreateUser(email string) { ... }

// ✅ Value object
type Email struct { value string }
func CreateUser(email Email) { ... }

Type Switch → Strategy Pattern

// ❌ Type switch
switch v := val.(type) { ... }

// ✅ Strategy pattern
type Processor interface { Process() }

Trigger Phrases

This skill activates when you say:

  • "Refactor this code"
  • "Reduce complexity"
  • "Extract methods from large function"
  • "Apply design patterns"
  • "Improve maintainability"
  • "Simplify this usecase"
  • "Find code smells"

How to Use

For Code Refactoring

  1. Identify code smells (God Objects, long parameter lists, etc.)
  2. Apply appropriate refactoring pattern
  3. Verify tests still pass
  4. Check for improved readability

For Pattern Application

  1. Identify appropriate pattern for use case
  2. Apply pattern incrementally
  3. Ensure pattern improves, not complicates code

Output Format

## High Priority Refactoring: X

### [Rule Name] (Line Y)
**Code Smell**: God Object / Long Parameter List / Primitive Obsession
**Impact**: Hard to maintain / Test / Understand
**Refactoring**: Extract Method / Introduce Parameter Object / Create Value Object
**Example**:
```go
// Refactored code

Related Skills

Philosophy

Based on Martin Fowler's Refactoring:

  • Code smells indicate problems - Detect and address systematically
  • Refactor incrementally - Small, safe steps
  • Patterns are solutions - Apply when appropriate, not dogmatically
  • Maintainability matters - Code is read more than written

Notes

  • Focus on common Go refactoring patterns
  • All patterns adapted for Go idioms
  • Emphasizes readability and maintainability
  • Includes Gang of Four patterns applicable to Go

Source

git clone https://github.com/saifoelloh/golang-best-practices-skill/blob/main/design-patterns/SKILL.mdView on GitHub

Overview

Golang Design Patterns & Refactoring provides expert-level guidance for applying proven patterns and refactoring strategies in Go. It detects code smells, suggests pattern-based solutions, and helps you improve maintainability and reduce technical debt.

How This Skill Works

The skill analyzes Go code to identify smells such as God Objects, long parameter lists, and primitive obsession. It then recommends targeted refactorings (e.g., extract method, value object, strategy) and demonstrates patterns with concrete Go examples to guide incremental improvements.

When to Use It

  • Refactoring complex or legacy Go code
  • Reducing technical debt and cruft
  • Extracting reusable patterns across modules
  • Simplifying large functions (god objects)
  • Applying Gang of Four patterns to Go

Quick Start

  1. Step 1: Identify code smells (God Object, long parameter lists) in your Go code.
  2. Step 2: Choose the appropriate pattern and implement it incrementally.
  3. Step 3: Run tests and review readability to ensure maintainability improvements.

Best Practices

  • Identify code smells first (God Object, long parameter lists, primitive obsession)
  • Prefer small, testable refactors and incremental changes
  • Replace type switches with interfaces via the Strategy pattern
  • Use value objects and builder-pattern for complex construction
  • Verify tests pass after each change and measure readability

Example Use Cases

  • Extract a 500-line function into smaller methods
  • Convert primitive types to a Value Object (e.g., Email)
  • Replace a type switch with the Strategy pattern
  • Implement a Builder for complex object creation
  • Apply Decorator pattern to http.Handler for middleware

Frequently Asked Questions

Add this skill to your agents
Sponsor this space

Reach thousands of developers