Implementing Consistent Code Indentation Practices
Establish and enforce uniform indentation rules to maintain code clarity and prevent errors.
0 likes
173 views
Rule Content
---
description: Enforce consistent code indentation to enhance readability and maintainability
globs: ['**/*.js', '**/*.ts', '**/*.jsx', '**/*.tsx', '**/*.py', '**/*.java', '**/*.php', '**/*.rb']
tags: [code-formatting, indentation, readability]
priority: 3
version: 1.0.0
---
# Implementing Consistent Code Indentation Practices
## Context
- Applicable to all source code files within the project.
- Ensures uniform indentation across different programming languages to maintain code clarity and prevent errors.
## Requirements
- **JavaScript/TypeScript/React (JSX/TSX):** Use 2 spaces for indentation.
- **Python:** Use 4 spaces for indentation.
- **Java:** Use 4 spaces for indentation.
- **PHP:** Use 4 spaces for indentation.
- **Ruby:** Use 2 spaces for indentation.
## Examples
// Good: JavaScript with 2-space indentation
function calculateTotal(price, quantity) {
const total = price * quantity;
return total;
}
// Bad: JavaScript with inconsistent indentation
function calculateTotal(price, quantity) {
const total = price * quantity;
return total;
}
# Good: Python with 4-space indentation
def calculate_total(price, quantity):
total = price * quantity
return total
# Bad: Python with inconsistent indentation
def calculate_total(price, quantity):
total = price * quantity
return total
// Good: Java with 4-space indentation
public class Calculator {
public int calculateTotal(int price, int quantity) {
int total = price * quantity;
return total;
}
}
// Bad: Java with inconsistent indentation
public class Calculator {
public int calculateTotal(int price, int quantity) {
int total = price * quantity;
return total;
}
}
// Good: PHP with 4-space indentation
function calculateTotal($price, $quantity) {
$total = $price * $quantity;
return $total;
}
// Bad: PHP with inconsistent indentation
function calculateTotal($price, $quantity) {
$total = $price * $quantity;
return $total;
}
# Good: Ruby with 2-space indentation
def calculate_total(price, quantity)
total = price * quantity
total
end
# Bad: Ruby with inconsistent indentation
def calculate_total(price, quantity)
total = price * quantity
total
end