Code Quality Guideline
Ensuring code quality and maintainability in Node.js and Fastify applications requires following best practices enforced by ESLint rules.
This guide outlines coding standards with explanations, incorrect and correct examples, and why each rule is important.
Table of Contents
- 1. Consistent Code Style
- 2. Use Arrow Functions for Callbacks
- 3. Avoid Callback Hell (Use Async/Await)
- 4. Use Descriptive Route Names
- 5. Avoid Mutating Request Objects
- 6. Use process.env for Configuration Variables
- 7. Avoid Using setTimeout for Async Operations
- 8. Enforce Strict Equality (===)
- 9. Use .map() Instead of forEach() for Transformations
- 10. Always Return a Response
- 11. Avoid Console Logs in Production
- 12. Use Named Functions for Middleware
- 13. Enforce Object Property Shorthand
- 14. Use const for Immutable Variables
- 15. Always Use Braces for Conditionals
- 16. Ensure Proper Spacing
- 17. Avoid Hardcoded Status Codes
- 18. Avoid Deeply Nested Code
- 19. Limit Maximum Line Length
- 20. Use .catch() for Promises
- 21. Avoid Using eval()
- 22. Use req.query, req.params, and req.body Correctly
- 23. Avoid Modifying Function Parameters
- 24. Use Promise.all() for Parallel Async Operations
- 25. Avoid Deeply Nested Ternary Expressions
- 26. Use Proper Error Messages
- 27. Always Use Meaningful Variable Names
- 28. Enforce Comments for Complex Logic
- 29. Avoid Using Deprecated APIs
- 30. Validate Input Data
- 31. Always Close Database Connections
- 32. Avoid Hardcoded Paths
- 33. Limit Function Complexity
- 34. Use Rate Limiting to Prevent Abuse
- 35. Use Helmet for Security Headers
- 36. Avoid Insecure JWT Secrets
- 37. Sanitize User Input
- 38. Enforce API Versioning
[1] Consistent Code Style
Reason
Ensures consistency in code formatting, improving readability and maintainability.
🔴 Incorrect Usage:
🟢 Correct Usage:
[2] Use Arrow Functions for Callbacks
Reason
Arrow functions are more concise, avoid this binding issues, and ensure consistency.
🔴 Incorrect Usage:
🟢 Correct Usage:
[3] Avoid Callback Hell Use Async and Await
Reason
Improves readability and maintainability by avoiding deeply nested callbacks.
🔴 Incorrect Usage:
🟢 Correct Usage:
[4] Use Descriptive Route Names
Reason
Improves API clarity and maintainability.
🔴 Incorrect Usage:
🟢 Correct Usage:
[5] Avoid Mutating Request Objects
Reason
Prevents unexpected side effects that can cause bugs.
🔴 Incorrect Usage:
🟢 Correct Usage:
[6] Use process.env for Configuration Variables
Reason
Prevents hardcoded secrets in code, enhancing security and maintainability.
🔴 Incorrect Usage:
🟢 Correct Usage:
[7] Avoid Using setTimeout for Async Operations
Reason
Avoids magic numbers and makes the code more configurable.
🔴 Incorrect Usage:
🟢 Correct Usage:
[8] Enforce Strict Equality ===
Reason
Prevents unexpected type coercion, ensuring accurate comparisons.
🔴 Incorrect Usage:
🟢 Correct Usage:
[9] Use .map Instead of forEach for Transformations
Reason
.map() is more functional and avoids unnecessary mutation.
🔴 Incorrect Usage:
🟢 Correct Usage:
[10] Always Return a Response
Reason
Prevents hanging requests and ensures consistent API behavior.
🔴 Incorrect Usage:
🟢 Correct Usage:
[11] Avoid Console Logs in Production
Reason
console.log is useful for debugging but should not be used in production. Use a proper logger.
🔴 Incorrect Usage:
🟢 Correct Usage:
[12] Use Named Functions for Middleware
Reason
Named functions make debugging easier and improve stack traces.
🔴 Incorrect Usage:
🟢 Correct Usage:
[13] Enforce Object Property Shorthand
Reason
Reduces redundancy in object creation.
🔴 Incorrect Usage:
🟢 Correct Usage:
[14] Use const for Immutable Variables
Reason
Prevents unintended variable reassignment.
🔴 Incorrect Usage:
🟢 Correct Usage:
[15] Always Use Braces for Conditionals
Reason
Improves code clarity and prevents unintended behavior.
🔴 Incorrect Usage:
🟢 Correct Usage:
[16] Ensure Proper Spacing
Reason
Improves code readability.
🔴 Incorrect Usage:
🟢 Correct Usage:
[17] Avoid Hardcoded Status Codes
Reason
Magic numbers reduce code readability. Use constants for clarity.
🔴 Incorrect Usage:
🟢 Correct Usage:
[18] Avoid Deeply Nested Code
Reason
Reduces complexity and improves readability.
🔴 Incorrect Usage:
🟢 Correct Usage:
[19] Limit Maximum Line Length
Reason
Prevents horizontal scrolling and improves readability.
🔴 Incorrect Usage:
🟢 Correct Usage:
[20] Use .catch for Promises
Reason
Ensures proper error handling in asynchronous operations.
🔴 Incorrect Usage:
🟢 Correct Usage:
[21] Avoid Using eval
Reason
eval() is a security risk as it can execute arbitrary code, leading to vulnerabilities.
🔴 Incorrect Usage:
🟢 Correct Usage:
[22] Use req.query, req.params, and req.body Correctly
Reason
Prevents improper data handling and ensures requests retrieve values from the correct locations.
🔴 Incorrect Usage:
🟢 Correct Usage:
[23] Avoid Modifying Function Parameters
Reason
Prevents unintended side effects when modifying function parameters.
🔴 Incorrect Usage:
🟢 Correct Usage:
[24] Use Promise.all for Parallel Async Operations
Reason
Ensures non-blocking execution and improves performance by running asynchronous operations in parallel.
🔴 Incorrect Usage:
🟢 Correct Usage:
[25] Avoid Deeply Nested Ternary Expressions
Reason
Enhances readability and avoids confusion.
🔴 Incorrect Usage:
🟢 Correct Usage:
[26] Use Proper Error Messages
Reason
Ensures meaningful error messages for debugging.
🔴 Incorrect Usage:
🟢 Correct Usage:
[27] Always Use Meaningful Variable Names
Reason
Improves code readability and maintainability.
🔴 Incorrect Usage:
🟢 Correct Usage:
[28] Enforce Comments for Complex Logic
Reason
Helps in documentation and understanding of complex code.
🔴 Incorrect Usage:
🟢 Correct Usage:
[29] Avoid Using Deprecated APIs
Reason
Prevents usage of outdated or removed Node.js APIs.
🔴 Incorrect Usage:
🟢 Correct Usage:
[30] Validate Input Data
Reason
Prevents Regular Expression Denial of Service (ReDoS) attacks.
🔴 Incorrect Usage:
🟢 Correct Usage:
[31] Always Close Database Connections
Reason
Prevents memory leaks and database connection exhaustion.
🔴 Incorrect Usage:
🟢 Correct Usage:
[32] Avoid Hardcoded Paths
Reason
Prevents errors in different environments and improves security.
🔴 Incorrect Usage:
🟢 Correct Usage:
[33] Limit Function Complexity
Reason
Reduces cognitive load and improves maintainability.
🔴 Incorrect Usage:
🟢 Correct Usage:
[34] Use Rate Limiting to Prevent Abuse
Reason
Protects APIs from excessive requests and DoS attacks.
🔴 Incorrect Usage:
🟢 Correct Usage:
[35] Use Helmet for Security Headers
Reason
Prevents common security vulnerabilities.
🔴 Incorrect Usage:
🟢 Correct Usage:
[36] Avoid Insecure JWT Secrets
Reason
Prevents unauthorized access due to exposed secrets.
🔴 Incorrect Usage:
🟢 Correct Usage:
[37] Sanitize User Input
Reason
Prevents injection attacks (SQL, XSS, etc.).
🔴 Incorrect Usage:
🟢 Correct Usage:
[38] Enforce API Versioning
Reason
Allows backward compatibility for future API changes.
🔴 Incorrect Usage:
🟢 Correct Usage:
Reference Websites
- ESLint Rules Documentation: https://eslint.org/docs/latest/rules/
- Node.js Style Guide (Airbnb): https://github.com/airbnb/javascript
- OWASP Node.js Security Cheat Sheet: https://cheatsheetseries.owasp.org/cheatsheets/Nodejs_Security_Cheat_Sheet.html
- Fastify Helmet Plugin for Security Headers: https://github.com/fastify/fastify-helmet