Creating Consistency in Object-Oriented JavaScript Practices

Develop clear protocols for object-oriented programming in JavaScript to streamline collaboration and maintain code quality.

0 likes
35 views

Rule Content

# Creating Consistency in Object-Oriented JavaScript Practices

## Description

Develop clear protocols for object-oriented programming in JavaScript to streamline collaboration and maintain code quality.

## Category

JavaScript Cursor Rules

## Rules

### General Principles

- **Use ES6+ Class Syntax**:
  - Define classes using the `class` keyword introduced in ECMAScript 6 (ES6).
  - Ensure compatibility with ECMAScript 2024 (ES2024) standards.

- **Encapsulation**:
  - Utilize private fields and methods using the `#` prefix to encapsulate internal implementation details.
  - Provide public getters and setters for controlled access to private properties.

- **Inheritance**:
  - Use the `extends` keyword to create subclasses.
  - Call the parent class constructor using `super()` within the subclass constructor.

- **Composition Over Inheritance**:
  - Favor composition to promote code reuse and flexibility.
  - Use dependency injection to compose behaviors.

- **Static Methods**:
  - Define utility functions as static methods within classes when they do not depend on instance properties.

- **Method Binding**:
  - Use arrow functions or explicit binding to ensure correct `this` context in callbacks.

- **Avoid Overuse of Classes**:
  - Use classes when they provide clear benefits in terms of structure and maintainability.
  - For simple data structures or utility functions, consider using modules or factory functions.

### Naming Conventions

- **Class Names**:
  - Use PascalCase for class names (e.g., `UserAccount`).

- **Method and Property Names**:
  - Use camelCase for method and property names (e.g., `calculateTotal`).

- **Private Fields and Methods**:
  - Prefix private fields and methods with `#` (e.g., `#internalData`).

### Documentation

- **JSDoc Comments**:
  - Document all classes, methods, and properties using JSDoc comments.
  - Include descriptions, parameter types, and return types.

### Testing

- **Unit Tests**:
  - Write unit tests for all classes and methods to ensure functionality and prevent regressions.

### Code Style

- **Linting**:
  - Use ESLint with a configuration that enforces object-oriented best practices.

- **Formatting**:
  - Use Prettier to maintain consistent code formatting.

### Performance

- **Memory Management**:
  - Be mindful of memory usage, especially when creating multiple instances of classes.
  - Implement proper cleanup and avoid memory leaks.

### Security

- **Data Validation**:
  - Validate input data within class methods to prevent security vulnerabilities.

- **Error Handling**:
  - Implement robust error handling within classes to manage exceptions gracefully.

### Version Control

- **Commit Messages**:
  - Use clear and descriptive commit messages that reflect changes related to object-oriented structures.

### Continuous Integration

- **Automated Testing**:
  - Integrate automated tests for object-oriented components into the CI/CD pipeline.

### Code Reviews

- **Review Focus**:
  - During code reviews, pay special attention to the correct implementation of object-oriented principles.

### Learning and Improvement

- **Stay Updated**:
  - Keep abreast of the latest developments in JavaScript and object-oriented programming practices.

By adhering to these rules, teams can ensure consistent and high-quality object-oriented JavaScript code, facilitating better collaboration and maintainability.