Back to resources

$ Cursor Rules for Code Style

A comprehensive guide to consistent formatting, naming conventions, and syntax for your projects.

Background and Context

The importance of code style lies in improving readability, maintainability, and collaboration, especially in team settings. Consistent formatting, naming conventions, and syntax reduce cognitive load and prevent errors, making these rules critical for large-scale projects.

The rules set out here are general, with notes on language-specific adjustments, ensuring they are practical for implementation in the .cursorrules file.

Detailed Rules for Code Style

Below is a table summarizing the proposed cursor rules for code style, categorized by formatting, naming conventions, and syntax, with examples and notes for clarity.

Category Rule Example Notes
Indentation Use 4 spaces for indentation. Avoid using tabs. if (x > 0) {
    return true;
}
Common in Python (PEP 8) and many projects; JavaScript may use 2 spaces.
Line Length Keep lines to a maximum of 120 characters. Break longer lines appropriately. long_variable_name = very_long_function_call(param1, param2) Python often uses 79 (PEP 8); 120 is common in modern projects for flexibility.
Naming Conventions - Variables and functions: snake_case
- Classes: CamelCase
- Constants: UPPER_CASE
user_name, UserClass, MAX_VALUE Python uses snake_case; JavaScript uses camelCase for variables (userName).
Spacing - Single space after commas.
- Spaces around operators.
- No space before colons or semicolons.
x = y + z, function(x, y) Ensures readability; varies slightly by language (e.g., Python uses colons for control structures).
Control Structures For languages with braces, place opening brace on the same line as the control statement; closing brace on its own line. if (x > 0) {
  return true;
}
Applies to C, Java, JavaScript; Python uses indentation, so adjust accordingly.
Comments Use language's standard comment syntax for single-line and multi-line comments. Python: # Comment, """Multi-line"""
JavaScript: // Comment, /* Multi-line */
Ensures clarity; adapt to language (e.g., Python uses #, not //).
Strings Use single quotes for string literals. 'Hello, World!' Common in Python and JavaScript; double quotes may be used if string contains single quotes.
Semicolons Always use semicolons at the end of statements where required by the language. let x = 5; (JavaScript) Not needed in Python; critical in JavaScript and C++ for statement termination.
Trailing Commas Avoid trailing commas in lists, arrays, or function calls. [1, 2, 3] (not [1, 2, 3, ]) Improves readability; some modern tools allow trailing commas, but consistency is key.

Language-Specific Considerations

The analysis revealed significant variation in code style across languages, necessitating adaptation. For instance:

  • Python: Follows PEP 8, with 4 spaces indentation, 79-character line length, and snake_case for functions/variables. Control structures rely on indentation, not braces, so rule 5 (control structures) would be irrelevant (PEP 8 -- Style Guide for Python Code).
  • JavaScript: Often uses 2 spaces (Airbnb style), 100-character line length, and camelCase for variables/functions. Semicolons are mandatory, and brace placement follows K&R style (Airbnb JavaScript Style Guide).
  • C++: Google's C++ Style Guide recommends 2 spaces, camelCase for variables, and braces on the same line, with a 80-character line limit, differing from our 120-character suggestion.

Implementation in Cursor AI

The .cursorrules file, typically a markdown file, should list these instructions clearly for the AI to follow. For example:

To maintain a consistent code style across the project, please follow these guidelines:

  1. Use 4 spaces for indentation. Avoid using tabs.
  2. Keep all lines to a maximum of 120 characters. Break longer lines appropriately.
  3. Use snake_case for variable and function names, CamelCase for classes, and UPPER_CASE for constants.
  4. Place a single space after commas and around operators, but no space before colons or semicolons.
  5. For control structures, place the opening brace on the same line as the control statement and the closing brace on its own line.
  6. Use # for single-line comments (or equivalent in your language) and multi-line comment syntax for longer explanations.
  7. Use single quotes for string literals.
  8. Always use semicolons at the end of statements where required by the language.
  9. Avoid trailing commas in lists, arrays, or function calls.

Key Citations