Enhancing Code Readability with Adaptive Formatting Techniques

Implement adaptive formatting methods that adjust to improve code readability based on context.

0 likes
13 views

Rule Content

---
description: Implement adaptive formatting methods that adjust to improve code readability based on context.
globs: "**/*.{js,jsx,ts,tsx,py,java,cs,cpp,rb,go,php}"
tags: [code-formatting, readability, adaptive-formatting]
priority: 2
version: 1.0.0
---

# Enhancing Code Readability with Adaptive Formatting Techniques

## Context
- Applicable to all source code files in the project.
- Aims to maintain consistent and context-aware code formatting to enhance readability and maintainability.

## Requirements
- **Indentation**: Use 4 spaces per indentation level for Python, Java, C#, C++, PHP; use 2 spaces for JavaScript, TypeScript, Ruby, Go.
- **Line Length**: Limit lines to 80 characters for Python; 100 characters for Java, C#, C++, PHP; 120 characters for JavaScript, TypeScript, Ruby, Go.
- **Spacing**:
  - Ensure a single space after commas, colons, and semicolons.
  - Maintain a single blank line between functions and class definitions.
  - Use blank lines to separate logical sections within functions.
- **Braces and Brackets**:
  - For languages like C++, Java, C#, JavaScript, TypeScript, and Go, place opening braces on the same line as the statement.
  - For Python, use indentation to define code blocks without braces.
- **Naming Conventions**:
  - Use `camelCase` for variables and functions in JavaScript, TypeScript, Java, C#, Go.
  - Use `snake_case` for variables and functions in Python, Ruby, PHP.
  - Use `PascalCase` for class names across all languages.
- **Comments**:
  - Write comments in English.
  - Use single-line comments (`//` or `#`) for brief explanations.
  - Use multi-line comments (`/* */` or `""" """`) for detailed descriptions.
- **Adaptive Formatting**:
  - Adjust formatting based on the specific language's conventions and the project's established style guide.
  - Utilize language-specific linters and formatters (e.g., ESLint for JavaScript, Pylint for Python, gofmt for Go) to enforce formatting rules.

## Examples

// JavaScript: Good example
function calculateTotal(price, taxRate) {
  const tax = price * taxRate;
  return price + tax;
}
// JavaScript: Bad example
function calculate_total(price,taxRate){
const tax=price*taxRate;return price+tax;}
# Python: Good example
def calculate_total(price, tax_rate):
    tax = price * tax_rate
    return price + tax
# Python: Bad example
def calculateTotal(price,taxRate): tax=price*taxRate; return price+tax
// Java: Good example
public class Calculator {
    public double calculateTotal(double price, double taxRate) {
        double tax = price * taxRate;
        return price + tax;
    }
}
// Java: Bad example
public class calculator{
public double calculate_total(double price,double taxRate){double tax=price*taxRate;return price+tax;}}