Skip to content
control-flow-check logo

Go Control Flow Check Skill

control-flow-check

Audits Go code for control flow excellence - early returns, minimal nesting, small blocks. Checks for happy path readability, guard clauses, and refactoring opportunities. Use before committing Go code or during refactoring.

SKILL.md

Full skill instructions

Go Control Flow Check Skill

Purpose

Audit Go code for control flow best practices based on RMS Go coding standards. This skill identifies control flow anti-patterns and suggests refactoring opportunities to improve readability and maintainability.

What This Skill Checks

1. Early Return Pattern (Priority: HIGH)

Golden Rule: Use early returns for error cases and edge conditions to keep the happy path at the lowest indentation level.

Good Pattern:

func ProcessTask(task *Task) error {
    if task == nil {
        return errors.New("task is nil")
    }
    if !task.IsValid() {
        return errors.New("invalid task")
    }

    // Happy path at lowest indentation
    result := task.Execute()
    return result.Save()
}

Bad Pattern:

func ProcessTask(task *Task) error {
    if task != nil {
        if task.IsValid() {
            // Happy path nested 2 levels
            result := task.Execute()
            return result.Save()
        } else {
            return errors.New("invalid task")
        }
    } else {
        return errors.New("task is nil")
    }
}

2. Nesting Depth (Priority: HIGH)

Golden Rule: Maximum 2-3 levels of nesting. Deeper nesting indicates need for refactoring.

Detection Pattern:

  • Count opening braces { in nested structures
  • Flag any function with nesting > 3 levels
  • Suggest extraction of nested logic into helper functions

3. Block Size (Priority: MEDIUM)

Golden Rule: If/​else blocks should be < 10 lines. Large blocks need extraction.

Detection Pattern:

  • Measure lines between if { and closing }
  • Measure lines between else { and closing }
  • Flag blocks > 10 lines for extraction

4. Guard Clauses (Priority: MEDIUM)

Golden Rule: Validate inputs and preconditions at function start with early returns.

Good Pattern:

func UpdateTask(ctx rms.Ctx, taskID string, updates map[string]interface{}) error {
    // Guard clauses at top
    if taskID == "" {
        return errors.New("taskID required")
    }
    if len(updates) == 0 {
        return errors.New("no updates provided")
    }

    // Main logic after guards
    task, err := fetchTask(ctx, taskID)
    // ...
}

5. Else Statements (Priority: LOW)

Guidance: Prefer early returns over else statements when possible.

Refactoring Pattern:

// Before
func IsValid(x int) bool {
    if x > 0 {
        return true
    } else {
        return false
    }
}

// After
func IsValid(x int) bool {
    if x > 0 {
        return true
    }
    return false
}

Step-by-Step Execution

Step 1: Identify Go Files to Check

# Find all Go files in the repository
find . -name "*.go" -not -path "*/​vendor/​*" -not -path "*/​mock*" -not -path "*_test.go"

Step 2: Read Target Go Files

Use the Read tool to examine Go files, focusing on:

  • Function definitions
  • Control flow structures (if/​else, switch, for, select)
  • Error handling patterns

Step 3: Analyze Each Function

For each function, check:

A. Early Return Pattern

  1. Identify error cases and edge conditions
  2. Verify they appear at function start with early returns
  3. Verify happy path is at lowest indentation
  4. Flag violations with line numbers

B. Nesting Depth

  1. Track indentation level as you scan through function
  2. Count maximum nesting depth
  3. Flag any depth > 3 levels
  4. Identify the nested section causing the issue

C. Block Size

  1. For each if/​else block, count lines between braces
  2. Flag blocks > 10 lines
  3. Note what the block is doing (for extraction suggestion)

D. Guard Clauses

  1. Check if input validation is at function start
  2. Verify validations use early returns
  3. Flag missing guard clauses for required inputs

E. Unnecessary Else

  1. Find if-return followed by else-return patterns
  2. Suggest removing else and de-indenting
  3. Find if-return followed by else-continue logic patterns

Step 4: Generate Report

Create a structured report:

## Control Flow Audit: [file_path]

### ✅ Functions Following Best Practices

- `FunctionName` ([file:line]) - Early returns, minimal nesting

### ⚠️ Issues Found

#### HIGH Priority: Excessive Nesting

- **Function**: `ComplexFunction` ([file:line])
  - **Issue**: Nesting depth of 4 levels at line X
  - **Location**: Lines X-Y
  - **Suggestion**: Extract nested logic into `helperFunction`

#### HIGH Priority: Missing Early Returns

- **Function**: `ProcessData` ([file:line])
  - **Issue**: Happy path nested 2 levels, error checks not using early returns
  - **Location**: Lines X-Y
  - **Suggestion**: Add guard clauses at function start

#### MEDIUM Priority: Large Block

- **Function**: `HandleRequest` ([file:line])
  - **Issue**: If block spans 15 lines (lines X-Y)
  - **Suggestion**: Extract to `validateAndProcess` helper

#### LOW Priority: Unnecessary Else

- **Function**: `IsAuthorized` ([file:line])
  - **Issue**: Else after return at line X
  - **Suggestion**: Remove else, de-indent remaining code

Step 5: Suggest Refactoring

For each issue found, provide:

  1. Current code snippet (showing the problem)
  2. Refactored code snippet (showing the solution)
  3. Explanation (why the refactoring improves readability)

Step 6: Summary Statistics

## Summary

- Files checked: X
- Functions analyzed: Y
- Issues found: Z
  - HIGH priority: A
  - MEDIUM priority: B
  - LOW priority: C
- Clean functions: W

Integration Points

This skill is invoked by:

  • quality-check skill for Go projects
  • safe-commit skill (via quality-check)
  • Directly when refactoring Go code

Exit Criteria

  • All Go functions analyzed
  • Report generated with line-specific issues
  • Refactoring suggestions provided for all issues
  • Summary statistics calculated

Example Usage

# Manual invocation
/​skill control-flow-check

# Automatic invocation via quality-check
/​skill quality-check  # Detects Go project, invokes control-flow-check

References

More skills from majiayu000

xiaohongshu logo
majiayu000/claude-arsenal

xiaohongshu

xiaohongshu

286 148
View
agent-task-conductor logo
majiayu000/claude-skill-registry

agent-task-conductor

Conduct multi-agent task orchestration and workflow coordination.

663 1
View
conductor-setup logo
majiayu000/claude-skill-registry

conductor-setup

Initialize project with Conductor artifacts (product definition,

663 1
View
animation-designer logo
majiayu000/claude-skill-registry

animation-designer

Expert in web animations, transitions, and motion design using Framer Motion and CSS

663 1
View
diagramming logo
majiayu000/claude-skill-registry

diagramming

Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...

663 1
View
h3-pg logo
majiayu000/claude-skill-registry-data

h3-pg

PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.

23 1
View
conductor-development logo
majiayu000/claude-skill-registry

conductor-development

Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...

663 1
View
conductor-status logo
majiayu000/claude-skill-registry

conductor-status

Display project status, active tracks, and next actions

663 1
View
dockerization logo
majiayu000/claude-skill-registry

dockerization

Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.

663 1
View

Popular AI tools

Kaiber logo
Video

Kaiber

Generate, edit, and beat-sync AI video with leading models in one workspace.

Paid
View
Vimcal logo
Productivity

Vimcal

The world's fastest calendar for remote work

Free
View

Transform Your Design with AI Designer by ImgCreator.ai

Freemium
View
Akool AI logo
Content & writing

Akool AI

Revolutionizing Video Production with AI-Powered Creativity

Paid
View

Extend an image past the frame and let AI fill the new aspect ratio.

Freemium
View
StarByFace logo
Security

StarByFace

Discover your celebrity doppelgänger with StarByFace!

Free
View
C

ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.

Freemium
View
Opus Clip logo
Coding & apps

Opus Clip

Opus.ai: Revolutionize Your Web Experience

Free
View