Skip to main content
rule

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.

Install into Cursor · Manually: put the files in place All methods and platforms ↓
.cursor/rules/clean-code.mdc
Open on GitHub

What 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.mdc

Frontmatter: description, globs, alwaysApply

Step 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

Similar IDE rules

All IDE rules for Code review and refactoring →

From the same set210

code-pair-interviews Sets up Cursor for live coding interviews by enforcing plan outlines, simple initial solutions, clean naming, and edge-case handling. ★ 41,024repo stars code-guidelines-cursorrules-prompt-file A Cursor rule for careful code changes: verify facts, preserve existing structure, and account for tests, security, and version compatibility. ★ 41,024repo stars cloudflare-workers-hono-angular-saas-cursorrules-prompt-file A Cursor rule that pins the stack and conventions for SaaS on Cloudflare Workers: Hono API, Angular, Drizzle, Clerk, Stripe. ★ 41,024repo stars cloudflare-email-telegram-cursorrules-prompt-file Guides Cursor through setting up domain email forwarding to Telegram via Cloudflare Email Routing, Workers, and the mail2tg CLI. ★ 41,024repo stars chrome-extension-dev-js-typescript-cursorrules-pro A Cursor rule that points the agent at one set of conventions for building Chrome extensions in JavaScript and TypeScript. ★ 41,024repo stars blender-python-addon Cursor rule for writing Blender Python add-ons. Enforces clean operator lifecycles, structured panels, and safe Blender API calls. ★ 41,024repo stars beefreesdk This Cursor rule sets out Beefree SDK installation, proxy authentication, UI container setup, configuration, and callbacks for web projects. ★ 41,024repo stars beefreesdk-nocode-content-editor-cursorrules-prompt-file A Cursor rule that guides the agent when embedding Beefree SDK no-code editors for emails, pages and popups into a web app. ★ 41,024repo stars

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor