Skip to content

Latest commit

 

History

History
542 lines (402 loc) · 13.5 KB

File metadata and controls

542 lines (402 loc) · 13.5 KB
name code-simplifier
displayName Code Simplifier
description Simplifies and refines code across frameworks for clarity, consistency, and maintainability while preserving all functionality. Adapts to your framework with up-to-date best practices.
keywords
code-simplification
refactoring
code-quality
maintainability
clean-code
code-review
author Raishin

Code Simplifier

Overview

Code Simplifier is a comprehensive guide for enhancing code clarity, consistency, and maintainability across multiple programming languages and frameworks. It provides framework-specific best practices while maintaining core principles that apply universally.

This power helps you:

  • Preserve Functionality: Simplify code without changing what it does
  • Enhance Clarity: Make code more readable and maintainable
  • Apply Standards: Follow framework-specific conventions and best practices
  • Maintain Balance: Avoid over-simplification that reduces clarity
  • Stay Current: Access up-to-date framework documentation via Context7 MCP

Whether you're working with C, C#, Java, Kotlin, Rust, TypeScript/React, JavaScript, Python, PHP/Laravel/Symfony, Spring Boot, or other languages and frameworks, this power provides targeted guidance for writing cleaner, more maintainable code.

Available Steering Files

This power includes language and framework-specific guides that you can access on-demand:

Core Principles

  • core - Universal code simplification principles that apply to all languages and frameworks

Languages

  • c - C code simplification patterns, memory management, and best practices
  • csharp - C# patterns with LINQ, async/await, and .NET conventions
  • java - Java modern features, streams, lambdas, and design patterns
  • js - JavaScript simplification techniques and modern ES6+ features
  • kotlin - Kotlin null safety, coroutines, and idiomatic patterns
  • python - Python code clarity and Pythonic patterns
  • rust - Rust idioms, ownership patterns, and error handling
  • typescript - TypeScript type system, generics, and React patterns

Frameworks

  • java-spring - Spring Boot dependency injection, REST APIs, and JPA patterns
  • php - PHP-specific simplification patterns and best practices
  • php-laravel - Laravel framework conventions and elegant patterns
  • php-symfony - Symfony framework best practices and standards
  • python-django - Django models, views, QuerySet optimization, and patterns
  • python-fastapi - FastAPI async patterns, Pydantic, and dependency injection
  • python-fastmcp - FastMCP server patterns, tools, and resource management
  • python-flask - Flask blueprints, application factory, and SQLAlchemy patterns

To access a specific guide:

Call action "readSteering" with powerName="code-simplifier", steeringFile="{language}.md"

Core Principles

These principles apply across all frameworks and languages:

1. Preserve Functionality

Never change what the code does - only how it does it.

  • All original features, outputs, and behaviors must remain intact
  • Refactoring should be behavior-preserving transformations
  • Test coverage should remain the same or improve

2. Enhance Clarity

Make code easier to understand and maintain.

  • Reduce unnecessary complexity and nesting
  • Eliminate redundant code and abstractions
  • Use clear, descriptive names for variables and functions
  • Consolidate related logic
  • Remove comments that describe obvious code
  • Avoid nested ternary operators - prefer explicit conditionals

3. Apply Framework Standards

Follow established conventions and best practices.

  • Use framework-specific patterns and idioms
  • Follow community-accepted coding standards
  • Leverage framework features appropriately
  • Maintain consistent naming conventions

4. Maintain Balance

Avoid over-simplification that reduces clarity.

  • Don't sacrifice readability for brevity
  • Keep helpful abstractions that improve organization
  • Avoid overly clever solutions
  • Don't combine too many concerns into single units
  • Explicit code is often better than compact code

5. Focus Scope

Prioritize recently modified code.

  • Review code that has been recently changed
  • Expand scope only when explicitly requested
  • Consider the context of surrounding code

MCP Server Integration

This power uses Context7 MCP to provide up-to-date documentation for frameworks and libraries:

Available Tools

  • resolve-library-id: Find the correct library identifier for documentation lookup
  • query-docs: Query up-to-date documentation and code examples for any framework

Usage Example

When simplifying Laravel code, you can query for current best practices:

1. Resolve library: resolve-library-id with libraryName="laravel"
2. Query docs: query-docs with libraryId="/laravel/docs" and query="eloquent relationship best practices"

This ensures your simplifications follow the latest framework conventions.

Agent Hooks

This power includes two agent hooks to automate code simplification:

1. Simplify Code On-Demand (Manual Trigger)

Location: hooks/simplify-on-demand.json

Type: User-triggered hook (manual)

Purpose: Manually trigger code review when you want feedback on your code

How to use:

  1. Open the Agent Hooks panel in Kiro (View → Agent Hooks)
  2. Find "Simplify Code On-Demand" in the hooks list
  3. Click the trigger button when you want a code review
  4. The agent will review your current or recently modified files

What it reviews:

  • Unnecessary complexity and nesting
  • Variable and function naming
  • Code duplication
  • Framework convention adherence
  • Overall readability

2. Simplify Code On Save (Automatic - Disabled by Default)

Location: hooks/simplify-on-save.json

Type: File-edited hook (automatic)

Status: ⚠️ DISABLED BY DEFAULT

Purpose: Automatically review code every time you save a file

Why disabled by default: Automatic reviews can interrupt your workflow. Enable this only if you want continuous feedback.

How to enable:

  1. Open the Agent Hooks panel in Kiro
  2. Find "Simplify Code On Save (Disabled by Default)"
  3. Click to enable the hook
  4. Now every file save will trigger a quick code review

Supported file types:

  • JavaScript/TypeScript: .js, .ts, .jsx, .tsx
  • Python: .py
  • Java/Kotlin: .java, .kt
  • Rust: .rs
  • C/C++: .c, .cpp
  • C#: .cs
  • PHP: .php

What it does:

  • Provides brief, actionable feedback (2-3 suggestions max)
  • Acknowledges when code is already clean
  • Focuses on significant improvements only

To disable: Return to Agent Hooks panel and disable the hook

Common Workflows

Workflow 1: Manual Code Review (On-Demand Hook)

Goal: Manually trigger code simplification review for current or recently modified files

Using the Hook:

  1. Open the Agent Hooks panel in Kiro
  2. Find "Simplify Code On-Demand" hook
  3. Click the trigger button when you want a code review
  4. The agent will review your code and provide specific suggestions

Manual Workflow (without hook):

  1. Identify the recently modified code sections
  2. Read the appropriate framework steering file for specific guidance
  3. Analyze for opportunities to improve clarity and consistency
  4. Apply framework-specific best practices
  5. Verify functionality remains unchanged
  6. Document significant changes if needed

Example:

User: "I just modified the UserController, can you simplify it?"

Agent:
1. Read the modified UserController code
2. Call readSteering with steeringFile="php-laravel.md"
3. Apply Laravel-specific simplification patterns
4. Suggest improvements while preserving functionality

Workflow 2: Automatic Code Review (On-Save Hook - Disabled by Default)

Goal: Automatically review code for simplification opportunities when saving files

⚠️ Important: This hook is disabled by default to avoid interrupting your workflow. Enable it explicitly if you want automatic reviews.

To Enable:

  1. Open the Agent Hooks panel in Kiro
  2. Find "Simplify Code On Save (Disabled by Default)" hook
  3. Click to enable the hook
  4. Now every time you save a code file, it will be automatically reviewed

What it does:

  • Monitors file saves for common code file extensions
  • Provides quick, actionable feedback on simplification opportunities
  • Keeps reviews brief (2-3 suggestions max)
  • Acknowledges when code is already clean

To Disable:

  • Return to Agent Hooks panel and disable the hook

Workflow 3: Framework-Specific Refactoring

Goal: Apply framework-specific patterns to existing code

Steps:

  1. Determine the framework being used
  2. Load the appropriate steering file
  3. Query Context7 for current framework best practices (if needed)
  4. Apply framework-specific simplification patterns
  5. Ensure code follows framework conventions

Example:

User: "Refactor this Python function to be more Pythonic"

Agent:
1. Call readSteering with steeringFile="python.md"
2. Apply Python-specific patterns (list comprehensions, context managers, etc.)
3. Ensure PEP 8 compliance
4. Suggest idiomatic Python improvements

Workflow 4: Cross-Framework Code Review

Goal: Review code across multiple files and frameworks

Steps:

  1. Read the core principles (core.md)
  2. For each file, load the appropriate framework steering file
  3. Apply universal principles + framework-specific patterns
  4. Ensure consistency across the codebase

Best Practices

When to Simplify

Do simplify when:

  • Code has unnecessary nesting or complexity
  • Variable/function names are unclear
  • Logic is duplicated across multiple places
  • Framework conventions are not being followed
  • Code is harder to understand than necessary

Don't simplify when:

  • The code is already clear and maintainable
  • Simplification would reduce readability
  • The complexity is inherent to the problem domain
  • Existing abstractions serve a clear purpose

Simplification Patterns

Reduce Nesting:

// Before
if ($user) {
    if ($user->isActive()) {
        if ($user->hasPermission('edit')) {
            // do something
        }
    }
}

// After
if (!$user || !$user->isActive() || !$user->hasPermission('edit')) {
    return;
}
// do something

Avoid Nested Ternaries:

// Bad - nested ternary
const status = user ? user.active ? user.verified ? 'full' : 'partial' : 'inactive' : 'guest';

// Good - explicit conditions
let status = 'guest';
if (user) {
    if (user.active) {
        status = user.verified ? 'full' : 'partial';
    } else {
        status = 'inactive';
    }
}

// Better - match/switch when available
const status = match(true) {
    !user => 'guest',
    !user.active => 'inactive',
    user.verified => 'full',
    default => 'partial'
};

Clear Naming:

# Before
def proc(d):
    return [x for x in d if x > 0]

# After
def filter_positive_numbers(numbers):
    return [num for num in numbers if num > 0]

Troubleshooting

Context7 MCP Connection Issues

Problem: Cannot query framework documentation Symptoms:

  • Error: "MCP server not responding"
  • resolve-library-id or query-docs fails

Solutions:

  1. Verify Context7 API key is set in mcp.json
  2. Check internet connection
  3. Restart Kiro to reconnect MCP server
  4. Verify Context7 MCP server is not disabled

Framework Not Recognized

Problem: Agent doesn't apply correct framework patterns Symptoms:

  • Generic suggestions instead of framework-specific
  • Wrong conventions being applied

Solutions:

  1. Explicitly specify the framework in your request
  2. Ensure the appropriate steering file exists
  3. Check that file extensions match expected patterns (.php, .js, .py)

Over-Simplification

Problem: Suggested changes reduce code clarity Symptoms:

  • Code becomes harder to understand
  • Helpful abstractions are removed
  • Too much logic combined into single units

Solutions:

  1. Review the "Maintain Balance" principle in core.md
  2. Provide feedback: "This is too compact, keep it more explicit"
  3. Request specific aspects to preserve: "Keep the service layer separation"

Configuration

Environment Variables

The Context7 MCP server requires an API key:

  • CONTEXT7_API_KEY: Your Context7 API key for accessing documentation

Getting a Context7 API Key

  1. Visit: https://context7.com
  2. Sign up for an account
  3. Navigate to API Keys section
  4. Generate a new API key
  5. Add it to your mcp.json configuration

MCP Config Placeholders

IMPORTANT: Before using this power, replace the following placeholder in mcp.json:

  • YOUR_CONTEXT7_API_KEY: Your Context7 API key for accessing up-to-date framework documentation.
    • How to get it:
      1. Go to https://context7.com
      2. Sign up or log in to your account
      3. Navigate to Settings → API Keys
      4. Click "Generate New API Key"
      5. Copy the API key
      6. Replace YOUR_CONTEXT7_API_KEY in mcp.json with your actual key

After replacing the placeholder, your mcp.json should look like:

{
	"mcpServers": {
		"context7": {
			"command": "npx",
			"args": [
				"-y",
				"@upstash/context7-mcp",
				"--api-key",
				"ctx7_your_actual_key_here"
			],
			"env": {},
			"disabled": false,
			"autoApprove": [
				"resolve-library-id",
				"query-docs"
			]
		}
	}
}

Package: @upstash/context7-mcp MCP Server: context7