Code Formatting Guidelines for Sustainable Software Development
Establish formatting practices that contribute to the sustainability and maintainability of software projects.
0 likes
200 views
Rule Content
---
description: Enforce consistent code formatting to enhance software maintainability and sustainability
globs: ["**/*.js", "**/*.ts", "**/*.jsx", "**/*.tsx", "**/*.py", "**/*.java", "**/*.cs"]
tags: [code-formatting, maintainability, sustainability]
priority: 1
version: 1.0.0
---
# Code Formatting Guidelines for Sustainable Software Development
## Context
- Applicable to all source code files within the project.
- Aims to standardize code formatting to improve readability and maintainability.
## Requirements
- **Indentation**: Use 2 spaces per indentation level for JavaScript, TypeScript, JSX, and TSX files; use 4 spaces for Python, Java, and C# files.
- **Line Length**: Limit lines to a maximum of 80 characters to enhance readability.
- **Trailing Whitespace**: Remove any trailing whitespace at the end of lines.
- **EOL Characters**: Use Unix-style line endings (`LF`) across all files.
- **File Encoding**: Ensure all files are encoded in UTF-8.
- **Naming Conventions**:
- Use `camelCase` for variable and function names.
- Use `PascalCase` for class names.
- Use `kebab-case` for filenames.
- **Braces**: Place opening braces on the same line as the statement (e.g., `if`, `for`, `function`).
- **Semicolons**: Always terminate statements with a semicolon in JavaScript and TypeScript files.
- **Quotes**: Use single quotes for strings in JavaScript and TypeScript; double quotes in Python, Java, and C#.
- **Imports**: Group and order imports logically:
- Standard library imports first.
- Third-party imports second.
- Local imports last.
- **Comments**: Use `//` for single-line comments and `/* */` for multi-line comments in JavaScript and TypeScript; use `#` for comments in Python; use `//` and `/* */` in Java and C# as appropriate.
## Examples
<example>
// Good example in JavaScript
function calculateTotal(price, taxRate) {
const tax = price * taxRate;
return price + tax;
}
</example>
<example type="invalid">
// Bad example in JavaScript
function calculate_total(price, tax_rate)
{
var tax = price * tax_rate
return price + tax
}
</example>
<example>
# Good example in Python
def calculate_total(price, tax_rate):
tax = price * tax_rate
return price + tax
</example>
<example type="invalid">
# Bad example in Python
def calculate_total(price, tax_rate):
tax = price * tax_rate
return price + tax
</example>