coding-principles
This Claude Code skill provides language-agnostic principles for writing maintainable, readable, and high-quality code across any programming language. It covers core philosophy like prioritizing maintainability over speed and simplicity first, along with practical guidance on code quality, function design, parameter management, and refactoring strategies. Use this skill when implementing new features, refactoring existing code, or conducting code reviews to ensure consistency with established best practices.
git clone --depth 1 https://github.com/shinpr/claude-code-workflows /tmp/coding-principles && cp -r /tmp/coding-principles/dev-workflows-fullstack/skills/coding-principles ~/.claude/skills/coding-principlesSKILL.md
# Language-Agnostic Coding Principles ## Core Philosophy 1. **Maintainability over Speed**: Prioritize long-term code health over initial development velocity 2. **Simplicity First**: Choose the simplest solution that meets requirements (YAGNI principle) 3. **Minimum Surface for Required Coverage**: When introducing maintenance-surface-bearing elements (persistent state, public-contract or cross-boundary fields/props, behavioral modes/flags/variants, reusable abstractions, or component splits), select the smallest design surface that covers the current user-visible requirements and accepted technical constraints (audit, data integrity, compatibility, security, performance, accessibility). Adoption is justified by naming a current requirement or constraint that smaller alternatives fail to cover; value-based arguments serve as tiebreakers. Distinct from YAGNI (time-axis judgment of present vs. future need), this principle governs surface-area minimization at a fixed coverage point. 4. **Explicit over Implicit**: Make intentions clear through code structure and naming 5. **Delete over Comment**: Remove unused code instead of commenting it out ## Code Quality ### Continuous Improvement - Refactor related code within each change set — address style, naming, or structure issues in the files being modified - Improve code structure incrementally - Keep the codebase lean and focused - Delete unused code immediately ### Readability - Use meaningful, descriptive names drawn from the problem domain - Use full words in names; abbreviations are acceptable only when widely recognized in the domain - Use descriptive names; single-letter names are acceptable only for loop counters or well-known conventions (i, j, x, y) - Extract magic numbers and strings into named constants - Keep code self-documenting where possible ## Function Design ### Parameter Management - **Recommended**: 0-2 parameters per function - **For 3+ parameters**: Use objects, structs, or dictionaries to group related parameters - **Example** (conceptual): ``` // Instead of: createUser(name, email, age, city, country) // Use: createUser(userData) ``` ### Single Responsibility - Each function should do one thing well - Keep functions small and focused (typically < 50 lines) - Extract complex logic into separate, well-named functions - Functions should have a single level of abstraction ### Function Organization - Pure functions when possible (no side effects) - Separate data transformation from side effects - Use early returns to reduce nesting - Keep nesting to a maximum of 3 levels; use early returns or extracted functions to flatten deeper nesting ## Error Handling ### Error Management Principles - **Always handle errors**: Log with context or propagate explicitly - **Log appropriately**: Include context for debugging - **Protect sensitive data**: Mask or exclude passwords, tokens, PII from logs - **Fail fast**: Detect and report errors as early as possible ### Error Propagation - Use language-appropriate error handling mechanisms - Propagate errors to appropriate handling levels - Provide meaningful error messages - Include error context when re-throwing ## Dependency Management ### Loose Coupling via Parameterized Dependencies - Inject external dependencies as parameters (constructor injection for classes, function parameters for procedural/functional code) - Depend on abstractions, not concrete implementations - Minimize inter-module dependencies - Facilitate testing through mockable dependencies ## Reference Representativeness ### Verifying References Before Adoption When adopting patterns, APIs, or dependencies from existing code: - **IF** referencing only 2-3 nearby files → **THEN** confirm the pattern is representative by checking usage across the repository before adopting - **IF** multiple approaches coexist in the repository → **THEN** identify the majority pattern and make a deliberate choice — selecting whichever is nearest is insufficient - **IF** adopting an external dependency (library, plugin, SDK) → **THEN** verify repository-wide usage distribution for the same dependency; if the appropriate version cannot be determined from repository state alone, escalate - **IF** following an existing pattern → **THEN** state the reason for following it when an alternative exists (e.g., consistency with surrounding code, avoiding breaking changes, pending coordinated update) ### Principle Nearby code is a starting point for investigation, not a sufficient basis for adoption. Verify that what you reference is representative of the repository's conventions and current best practices before using it as a model. ## Performance Considerations ### Optimization Approach - **Measure first**: Profile before optimizing - **Focus on algorithms**: Algorithmic complexity > micro-optimizations - **Use appropriate data structures**: Choose based on access patterns - **Resource management**: Handle memory, connections, and files properly ### When to Optimize - After identifying actual bottlenecks through profiling - When performance issues are measurable - Optimize only after measurable bottlenecks are identified, not during initial development ## Code Organization ### Structural Principles - **Group related functionality**: Keep related code together - **Separate concerns**: Domain logic, data access, presentation - **Consistent naming**: Follow project conventions - **Module cohesion**: High cohesion within modules, low coupling between ### File Organization - One primary responsibility per file - Logical grouping of related functions/classes - Clear folder structure reflecting architecture - Avoid "god files" (files > 500 lines) ## Commenting Principles ### When to Comment - **Document "what"**: Describe what the code does - **Explain "why"**: Clarify reasoning behind decisions - **Note limitations**: Document known constraints or edge cases - **API documentation**: Public interfaces need clear documentation
Generates integration/E2E test skeletons from Design Doc ACs using ROI-based selection and journey-based E2E reservation. Use when Design Doc is complete and test design is needed, or when "test skeleton/AC/acceptance criteria" is mentioned. Behavior-first approach for minimal tests with maximum coverage.
Validates Design Doc compliance and implementation completeness from third-party perspective. Use PROACTIVELY after implementation completes or when "review/implementation check/compliance" is mentioned. Provides acceptance criteria validation and quality reports.
Validates consistency between PRD/Design Doc and code implementation. Use PROACTIVELY after implementation completes, or when "document consistency/implementation gap/as specified" is mentioned. Uses multi-source evidence matching to identify discrepancies.
Analyzes existing codebase objectively for facts about implementation, user behavior patterns, and technical architecture. Use when existing code needs to be understood without hypothesis bias. Invoked before Design Doc creation to produce focused guidance for technical designers.
Detects conflicts across multiple Design Docs and provides structured reports. Use when multiple Design Docs exist, or when "consistency/conflict/sync/between documents" is mentioned. Focuses on detection and reporting only, no modifications.
Reviews document consistency and completeness, providing approval decisions. Use PROACTIVELY after PRD/UI Spec/Design Doc/work plan creation, or when "document review/approval/check" is mentioned. Detects contradictions and rule violations with improvement suggestions.
Verifies consistency between test skeleton comments and implementation code. Use PROACTIVELY after test implementation completes, or when "test review/skeleton verification" is mentioned. Returns quality reports with failing items and fix instructions.
Comprehensively collects problem-related information and creates evidence matrix. Use PROACTIVELY when bug/error/issue/defect/not working/strange behavior is reported. Reports only observations without proposing solutions.