|
| 1 | +--- |
| 2 | +description: "Detect duplicated logic and refactor large functions into reusable, tested utility functions." |
| 3 | +globs: *.go, pkg/**/*.go |
| 4 | +alwaysApply: false |
| 5 | +--- |
| 6 | + |
| 7 | +# Utility Extraction & Function Decomposition Rule |
| 8 | + |
| 9 | +**Purpose:** Identify repeating patterns and overly complex functions in Go |
| 10 | +code, extract shared logic into small, semantic utility functions, and |
| 11 | +decompose large functions for readability and maintainability. Ensure all new |
| 12 | +utilities have independent unit tests. |
| 13 | + |
| 14 | +## 1. Detect Duplication & Complexity |
| 15 | + |
| 16 | +* **Search for repeated code blocks:** Use `grep`, `git diff`, or IDE "Find |
| 17 | + Duplicates" features to locate similar logic across files. |
| 18 | + |
| 19 | +* **Identify large functions:** Functions exceeding \~50 lines or containing |
| 20 | + multiple distinct responsibilities. |
| 21 | + |
| 22 | +## 2. Extract Utility Functions |
| 23 | + |
| 24 | +1. **Define a clear purpose:** Name utilities descriptively (e.g., |
| 25 | + `ParseConfigField`, `ValidateUserInput`). |
| 26 | + |
| 27 | +2. **Move shared logic:** Extract common code into a new function in an |
| 28 | + appropriate package (e.g., `internal/util`). |
| 29 | + |
| 30 | +3. **Update callers:** Replace inlined code in each original location with |
| 31 | + calls to the new utility. |
| 32 | + |
| 33 | +```go |
| 34 | +// Before: repeated parsing logic in multiple handlers |
| 35 | +field, err := strconv.Atoi(params["count"]) |
| 36 | +if err != nil { |
| 37 | + return fmt.Errorf("invalid count: %v", err) |
| 38 | +} |
| 39 | + |
| 40 | +// After: single utility |
| 41 | +field, err := util.ParseIntParam(params, "count") |
| 42 | +if err != nil { |
| 43 | + return err |
| 44 | +} |
| 45 | +``` |
| 46 | + |
| 47 | +## 3. Decompose Large Functions |
| 48 | + |
| 49 | +* **Single Responsibility:** Split functions that perform multiple tasks (e.g., |
| 50 | + parsing, validation, storage) into smaller helper or utility calls. |
| 51 | + |
| 52 | +* **Maintain clear flow:** Orchestrator functions should focus on high-level |
| 53 | + logic, delegating details to extracted utilities. |
| 54 | + |
| 55 | +## 4. Unit Test Utilities Independently |
| 56 | + |
| 57 | +* **Create dedicated `*_test.go` files** for each new utility. |
| 58 | + |
| 59 | +* **Cover edge cases and error paths** using table-driven tests. |
| 60 | + |
| 61 | +* **Use `b.ReportAllocs()`** in benchmarks for performance-sensitive utilities. |
| 62 | + |
| 63 | +```go |
| 64 | +func TestParseIntParam(t *testing.T) { |
| 65 | + tests := []struct { key, want string; wantErr bool }{ |
| 66 | + {"count", "5", false}, |
| 67 | + {"missing", "", true}, |
| 68 | + {"bad", "abc", true}, |
| 69 | + } |
| 70 | + for _, tc := range tests { |
| 71 | + t.Run(tc.key, func(t *testing.T) { |
| 72 | + _, err := ParseIntParam(map[string]string{tc.key: tc.want}, tc.key) |
| 73 | + if (err != nil) != tc.wantErr { |
| 74 | + t.Fatalf("ParseIntParam error = %v, wantErr %v", err, tc.wantErr) |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +## 5. Verify and Clean Up |
| 82 | + |
| 83 | +* **Run coverage:** Ensure utilities are fully covered. |
| 84 | + |
| 85 | +* **Run linters and formatters:** Maintain code style. |
| 86 | + |
| 87 | +* **Refactor call sites:** Remove any remaining duplication and update imports. |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +*Apply this rule to improve code reuse, readability, and testability across Go |
| 92 | +modules.* |
0 commit comments