Skip to content

Latest commit

 

History

History
69 lines (51 loc) · 3.79 KB

coding-rules.md

File metadata and controls

69 lines (51 loc) · 3.79 KB

Coding Rules and Guidelines

This document outlines the established coding standards and best practices for contributing to the Binary Coffee API project. Adherence to these rules ensures code consistency, maintainability, and a high quality codebase across all modules.

📜 Project Scope and Language Mandate

Language Requirement: Effective immediately, this entire project must exclusively use TypeScript (TS). All new files, modifications, and additions must adhere to TypeScript syntax, types, interfaces, and modern Node.js module patterns. Please ensure appropriate type definitions are used throughout the codebase.

Code Scope Limitation: For development, testing, and feature implementation, focus your efforts primarily within these directories:

  • src/: Contains all core application source code (controllers, services, utility functions).
  • tests/: Dedicated directory for unit and integration tests.
  • config/: Holds project configuration files (e.g., environment variables handling, module settings).

Changes outside these primary directories should be minimized or explicitly documented if necessary.

🏗️ General Coding Practices

1. Structure and Organization

  • Module Design: Services and controllers within src/ must be designed to handle single responsibilities (Single Responsibility Principle - SRP). Avoid placing large blocks of logic in one file; break down complexity into smaller, dedicated utility modules.
  • Imports: Use absolute imports where possible (@/utils/filename or similar alias) instead of relative paths (../../../).
  • Naming Conventions:
    • Components/Modules: PascalCase (e.g., UserService, DatabaseClient).
    • Functions/Methods: camelCase (e.g., fetchUserData, processData).
    • Variables/Constants: camelCase or SCREAMING_SNAKE_CASE for constants (MAX_RETRIES).

2. TypeScript Implementation Details

  • Type Safety is Mandatory: Every function, class, and module must be strongly typed. Avoid using any type unless absolutely unavoidable and clearly documented with a comment explaining the rationale.
  • Interfaces vs Types: Use TypeScript Interfaces (interface) for object shapes that describe public contracts (like data transfer objects - DTOs). Use Types (type) for unions, intersections, or primitive aliases.

3. Testing Standards (tests/ directory)

  • Coverage Goal: Aim for high unit test coverage (>80%) across all business logic in src/.
  • Test Structure: Tests should use TypeScript and the .spec.ts suffix (for example, tests/integration/user/search-users.spec.ts); this is the naming pattern discovered by the Mocha configuration. Unit tests should live alongside the code they are testing (e.g., src/services/UserService.ts should have a corresponding tests/user.service.spec.ts).
  • Async Handling: All asynchronous operations must be properly mocked and tested using async/await or appropriate Promise handling.

4. Endline

  • Always add a newline at the end of each file to comply with POSIX standards and avoid unnecessary diffs in version control.

♻️ Development Workflow

  1. Feature Implementation: Always start by defining the necessary types (interfaces) first to model the data flow.
  2. Review: Before committing, run linting (npm run lint), formatting checks (npm run format:check), and type-checking (npm run build / tsc --noEmit). Use npm run lint:fix or npm run format to apply automatic fixes, then review the changes.
  3. Contribution Guide: For detailed steps on setting up environments or complex architectural decisions, please refer to the project's main CONTRIBUTING.md guide.

This document was generated based on current best practices and is subject to revision.