$npx -y skills add saifoelloh/golang-best-practices-skill --skill design-patternsGo design patterns and refactoring skill. Use when refactoring complex code, reducing technical debt, or applying design patterns. Detects code smells and suggests pattern-based solutions.
| 1 | # Golang Design Patterns & Refactoring |
| 2 | |
| 3 | Expert-level code refactoring and design pattern application for Go. Detects code smells, suggests refactoring strategies, and applies proven design patterns to improve maintainability. |
| 4 | |
| 5 | ## When to Apply |
| 6 | |
| 7 | Use this skill when: |
| 8 | - Refactoring complex or legacy code |
| 9 | - Reducing technical debt |
| 10 | - Extracting reusable patterns |
| 11 | - Simplifying large functions (God Objects) |
| 12 | - Improving code maintainability |
| 13 | - Applying Gang of Four patterns to Go |
| 14 | |
| 15 | ## Rule Categories by Priority |
| 16 | |
| 17 | | Priority | Count | Focus | |
| 18 | |----------|-------|-------| |
| 19 | | High | 2 | Critical refactoring needs | |
| 20 | | Medium | 11 | Code quality improvements | |
| 21 | |
| 22 | ## Rules Covered (13 total) |
| 23 | |
| 24 | ### High-Impact Patterns (2) |
| 25 | |
| 26 | - `high-god-object` - Extract logic from 300+ line functions |
| 27 | - `high-extract-method` - Name complex code blocks with descriptive methods |
| 28 | |
| 29 | ### Medium Improvements (11) |
| 30 | |
| 31 | - `medium-primitive-obsession` - Replace primitives with value objects |
| 32 | - `medium-long-parameter-list` - Use parameter objects for >5 params |
| 33 | - `medium-data-clumps` - Extract repeated parameter groups |
| 34 | - `medium-feature-envy` - Move logic closer to data |
| 35 | - `medium-magic-constants` - Replace magic numbers with named constants |
| 36 | - `medium-builder-pattern` - Fluent API for complex construction |
| 37 | - `medium-factory-constructor` - Validated object creation |
| 38 | - `medium-introduce-parameter-object` - Group related parameters |
| 39 | - `medium-switch-to-strategy` - Replace type switches with interfaces |
| 40 | - `medium-middleware-decorator` - Decorator pattern for http.Handler |
| 41 | - `medium-law-of-demeter` - Reduce coupling, avoid message chains |
| 42 | |
| 43 | ## Common Refactoring Patterns |
| 44 | |
| 45 | ### God Object → Extracted Methods |
| 46 | ```go |
| 47 | // ❌ 500 line function |
| 48 | func (u *Usecase) Process() { ... } |
| 49 | |
| 50 | // ✅ Extracted methods |
| 51 | func (u *Usecase) Process() { |
| 52 | u.validate() |
| 53 | u.transform() |
| 54 | u.persist() |
| 55 | } |
| 56 | ``` |
| 57 | |
| 58 | ### Primitive Obsession → Value Object |
| 59 | ```go |
| 60 | // ❌ Primitive types |
| 61 | func CreateUser(email string) { ... } |
| 62 | |
| 63 | // ✅ Value object |
| 64 | type Email struct { value string } |
| 65 | func CreateUser(email Email) { ... } |
| 66 | ``` |
| 67 | |
| 68 | ### Type Switch → Strategy Pattern |
| 69 | ```go |
| 70 | // ❌ Type switch |
| 71 | switch v := val.(type) { ... } |
| 72 | |
| 73 | // ✅ Strategy pattern |
| 74 | type Processor interface { Process() } |
| 75 | ``` |
| 76 | |
| 77 | ## Trigger Phrases |
| 78 | |
| 79 | This skill activates when you say: |
| 80 | - "Refactor this code" |
| 81 | - "Reduce complexity" |
| 82 | - "Extract methods from large function" |
| 83 | - "Apply design patterns" |
| 84 | - "Improve maintainability" |
| 85 | - "Simplify this usecase" |
| 86 | - "Find code smells" |
| 87 | |
| 88 | ## How to Use |
| 89 | |
| 90 | ### For Code Refactoring |
| 91 | |
| 92 | 1. Identify code smells (God Objects, long parameter lists, etc.) |
| 93 | 2. Apply appropriate refactoring pattern |
| 94 | 3. Verify tests still pass |
| 95 | 4. Check for improved readability |
| 96 | |
| 97 | ### For Pattern Application |
| 98 | |
| 99 | 1. Identify appropriate pattern for use case |
| 100 | 2. Apply pattern incrementally |
| 101 | 3. Ensure pattern improves, not complicates code |
| 102 | |
| 103 | ## Output Format |
| 104 | |
| 105 | ``` |
| 106 | ## High Priority Refactoring: X |
| 107 | |
| 108 | ### [Rule Name] (Line Y) |
| 109 | **Code Smell**: God Object / Long Parameter List / Primitive Obsession |
| 110 | **Impact**: Hard to maintain / Test / Understand |
| 111 | **Refactoring**: Extract Method / Introduce Parameter Object / Create Value Object |
| 112 | **Example**: |
| 113 | ```go |
| 114 | // Refactored code |
| 115 | ``` |
| 116 | |
| 117 | ## Related Skills |
| 118 | |
| 119 | - [golang-clean-architecture](../clean-architecture/SKILL.md) - For usecase complexity patterns |
| 120 | - [golang-idiomatic-go](../idiomatic-go/SKILL.md) - For interface design |
| 121 | |
| 122 | ## Philosophy |
| 123 | |
| 124 | Based on Martin Fowler's Refactoring: |
| 125 | |
| 126 | - **Code smells indicate problems** - Detect and address systematically |
| 127 | - **Refactor incrementally** - Small, safe steps |
| 128 | - **Patterns are solutions** - Apply when appropriate, not dogmatically |
| 129 | - **Maintainability matters** - Code is read more than written |
| 130 | |
| 131 | ## Notes |
| 132 | |
| 133 | - Focus on common Go refactoring patterns |
| 134 | - All patterns adapted for Go idioms |
| 135 | - Emphasizes readability and maintainability |
| 136 | - Includes Gang of Four patterns applicable to Go |