clean-code — Cursor rule: what it does, how to install
This rule sets practical standards for readable, maintainable code. Use it when an agent writes, reviews, or refactors files.
.cursor/rules/clean-code.mdcWhat clean-code does
clean-code guides naming for variables, functions, and classes, replaces magic numbers with named constants, and keeps comments focused on reasons rather than obvious actions. It asks for small functions with one responsibility, reusable logic instead of duplication, and a clear file structure.
The rule also covers ongoing refactoring, early cleanup of technical debt, readable tests for edge cases and errors, plus Git habits such as focused commits, clear messages, and meaningful branch names. Its glob is **/*, while alwaysApply is false, so Cursor does not apply it on every interaction.
Install it by creating .cursor/rules/clean-code.mdc and placing the rule content in that file. Cursor cannot use these guidelines until the file is present.
What it helps with
- replace magic numbers with named constants
- split functions that handle several responsibilities
- remove duplicated logic from a codebase
- clarify names and comments during review
- cover error paths and edge cases with readable tests
Example prompts for the agent
- Review this file with the clean-code rule and suggest focused refactoring.
- Split this function into smaller units, each with one responsibility.
- Remove duplicated logic and replace magic numbers with named constants.
How to install clean-code
Manually: put the files in place
.cursor/rules/clean-code.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.claude/rules/clean-code.mdThe paths frontmatter field limits the files: move globs from the source into paths
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.github/instructions/clean-code.instructions.mdThe applyTo field sets which files it applies to: move globs from the source into applyTo
Manually: put the files in place
.devin/rules/clean-code.mdManually: put the files in place
.clinerules/clean-code.mdStep by step, with commands for each platform: Cursor rules: .mdc files, four modes and what replaced .cursorrules
Questions about clean-code
How do I install clean-code in Cursor?
Create .cursor/rules/clean-code.mdc and put the rule content in that file.
What does clean-code do?
It guides the agent toward clear names, focused functions, reusable logic, readable tests, and consistent Git practices. It also sets expectations for comments and project structure.
Does clean-code apply to every file?
Its scope is **/*, but alwaysApply is false, so Cursor does not apply the rule continuously.
Source text
clean-code.mdc · CC0-1.0
--- description: Guidelines for writing clean, maintainable, and human-readable code. Apply these rules when writing or reviewing code to ensure consistency and quality. globs: ["**/*"] alwaysApply: false --- # Clean Code Guidelines ## Constants Over Magic Numbers - Replace hard-coded values with named constants - Use descriptive constant names that explain the value's purpose - Keep constants at the top of the file or in a dedicated constants file ## Meaningful Names - Variables, functions, and classes should reveal their purpose - Names should explain why something exists and how it's used - Avoid abbreviations unless they're universally understood ## Smart Comments - Don't comment on what the code does - make the code self-documenting - Use comments to explain why something is done a certain way - Document APIs, complex algorithms, and non-obvious side effects ## Single Responsibility - Each function should do exactly one thing - Functions should be small and focused - If a function needs a comment to explain what it does, it should be split ## DRY (Don't Repeat Yourself) - Extract repeated code into reusable functions - Share common logic through proper abstraction - Maintain single sources of truth ## Clean Structure - Keep related code together - Organize code in a logical hierarchy - Use consistent file and folder naming conventions ## Encapsulation - Hide implementation details - Expose clear interfaces - Move nested conditionals into well-named functions ## Code Quality Maintenance - Refactor continuously - Fix technical debt early - Leave code cleaner than you found it ## Testing - Write tests before fixing bugs - Keep tests readable and maintainable - Test edge cases and error conditions ## Version Control - Write clear commit messages - Make small, focused commits - Use meaningful branch names
© PatrickJS · CC0-1.0 · PatrickJS/awesome-cursorrules