home / skills / sickn33 / antigravity-awesome-skills / clean-code

clean-code skill

/skills/clean-code

This skill applies Clean Code principles to writing, reviewing, and refactoring Python code for readability and maintainability.

npx playbooks add skill sickn33/antigravity-awesome-skills --skill clean-code

Review the files below or copy the command above to add this skill to your agents.

Files (1)
SKILL.md
4.3 KB
---
name: clean-code
description: "Applies principles from Robert C. Martin's 'Clean Code'. Use this skill when writing, reviewing, or refactoring code to ensure high quality, readability, and maintainability. Covers naming, functions, comments, error handling, and class design."
user-invocable: true
risk: safe
source: "ClawForge (https://github.com/jackjin1997/ClawForge)"
---

# Clean Code Skill

This skill embodies the principles of "Clean Code" by Robert C. Martin (Uncle Bob). Use it to transform "code that works" into "code that is clean."

## đź§  Core Philosophy
> "Code is clean if it can be read, and enhanced by a developer other than its original author." — Grady Booch

## When to Use
Use this skill when:
- **Writing new code**: To ensure high quality from the start.
- **Reviewing Pull Requests**: To provide constructive, principle-based feedback.
- **Refactoring legacy code**: To identify and remove code smells.
- **Improving team standards**: To align on industry-standard best practices.

## 1. Meaningful Names
- **Use Intention-Revealing Names**: `elapsedTimeInDays` instead of `d`.
- **Avoid Disinformation**: Don't use `accountList` if it's actually a `Map`.
- **Make Meaningful Distinctions**: Avoid `ProductData` vs `ProductInfo`.
- **Use Pronounceable/Searchable Names**: Avoid `genymdhms`.
- **Class Names**: Use nouns (`Customer`, `WikiPage`). Avoid `Manager`, `Data`.
- **Method Names**: Use verbs (`postPayment`, `deletePage`).

## 2. Functions
- **Small!**: Functions should be shorter than you think.
- **Do One Thing**: A function should do only one thing, and do it well.
- **One Level of Abstraction**: Don't mix high-level business logic with low-level details (like regex).
- **Descriptive Names**: `isPasswordValid` is better than `check`.
- **Arguments**: 0 is ideal, 1-2 is okay, 3+ requires a very strong justification.
- **No Side Effects**: Functions shouldn't secretly change global state.

## 3. Comments
- **Don't Comment Bad Code—Rewrite It**: Most comments are a sign of failure to express ourselves in code.
- **Explain Yourself in Code**: 
  ```python
  # Check if employee is eligible for full benefits
  if employee.flags & HOURLY and employee.age > 65:
  ```
  vs
  ```python
  if employee.isEligibleForFullBenefits():
  ```
- **Good Comments**: Legal, Informative (regex intent), Clarification (external libraries), TODOs.
- **Bad Comments**: Mumbling, Redundant, Misleading, Mandated, Noise, Position Markers.

## 4. Formatting
- **The Newspaper Metaphor**: High-level concepts at the top, details at the bottom.
- **Vertical Density**: Related lines should be close to each other.
- **Distance**: Variables should be declared near their usage.
- **Indentation**: Essential for structural readability.

## 5. Objects and Data Structures
- **Data Abstraction**: Hide the implementation behind interfaces.
- **The Law of Demeter**: A module should not know about the innards of the objects it manipulates. Avoid `a.getB().getC().doSomething()`.
- **Data Transfer Objects (DTO)**: Classes with public variables and no functions.

## 6. Error Handling
- **Use Exceptions instead of Return Codes**: Keeps logic clean.
- **Write Try-Catch-Finally First**: Defines the scope of the operation.
- **Don't Return Null**: It forces the caller to check for null every time.
- **Don't Pass Null**: Leads to `NullPointerException`.

## 7. Unit Tests
- **The Three Laws of TDD**:
  1. Don't write production code until you have a failing unit test.
  2. Don't write more of a unit test than is sufficient to fail.
  3. Don't write more production code than is sufficient to pass the failing test.
- **F.I.R.S.T. Principles**: Fast, Independent, Repeatable, Self-Validating, Timely.

## 8. Classes
- **Small!**: Classes should have a single responsibility (SRP).
- **The Stepdown Rule**: We want the code to read like a top-down narrative.

## 9. Smells and Heuristics
- **Rigidity**: Hard to change.
- **Fragility**: Breaks in many places.
- **Immobility**: Hard to reuse.
- **Viscosity**: Hard to do the right thing.
- **Needless Complexity/Repetition**.

## 🛠️ Implementation Checklist
- [ ] Is this function smaller than 20 lines?
- [ ] Does this function do exactly one thing?
- [ ] Are all names searchable and intention-revealing?
- [ ] Have I avoided comments by making the code clearer?
- [ ] Am I passing too many arguments?
- [ ] Is there a failing test for this change?

Overview

This skill applies the core principles from Robert C. Martin's Clean Code to help you write, review, and refactor code for greater readability, maintainability, and correctness. It focuses on naming, small functions and classes, comments, error handling, and unit testing to turn "code that works" into code other developers can confidently change.

How this skill works

The skill inspects code and recommends concrete changes: better names, smaller functions and classes, removal or replacement of misleading comments, clearer control flow, safer error handling, and appropriate tests. It also provides a short checklist and targeted heuristics for common code smells (rigidity, fragility, needless complexity) and prescribes corrective patterns like single-responsibility extraction, DTOs, and Law of Demeter fixes.

When to use it

  • During authoring of new features to keep code high-quality from the start.
  • When reviewing pull requests to provide actionable, principle-based feedback.
  • Refactoring legacy modules to identify and remove code smells.
  • Improving team standards and onboarding new developers to a consistent style.
  • Designing APIs, classes, or public interfaces that other teams will consume.

Best practices

  • Prefer intention-revealing, searchable names for variables, functions, and classes.
  • Keep functions and classes small; aim for single responsibility and one level of abstraction.
  • Replace many comments with clearer code; use comments only for legal, clarifying, or intent-bearing notes.
  • Use exceptions over return codes and avoid returning or passing null values.
  • Write unit tests first where possible and follow F.I.R.S.T. principles for test quality.

Example use cases

  • Refactor a 200-line method into small, well-named helper functions and clear control flow.
  • Review a pull request and flag misleading variable names, hidden side effects, or complex conditionals.
  • Convert ad-hoc data blobs into DTOs and hide implementation behind interfaces for safer evolution.
  • Improve error handling by replacing error codes with typed exceptions and explicit try/catch scopes.
  • Add focused unit tests to cover edge cases uncovered during refactoring and to prevent regressions.

FAQ

Does this skill enforce a specific style guide?

No. It recommends clean-code principles (names, small units, tests) that complement any style guide; it focuses on intent and maintainability rather than formatting rules.

When should I keep a comment instead of rewriting code?

Keep comments that document legal constraints, explain non-obvious algorithmic intent (e.g., regex intent), or clarify external library quirks. Otherwise prefer expressive names and small functions.