Skip to content

Commit d19f76d

Browse files
committed
docs: best practices
1 parent e8fae42 commit d19f76d

File tree

1 file changed

+122
-0
lines changed

1 file changed

+122
-0
lines changed

docs/best-practices.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Best Programming Practices for @cap-js/openapi
2+
3+
This guide outlines the best programming practices for the @cap-js/openapi project. These practices are designed to ensure code quality, maintainability, and consistency across the project.
4+
5+
## Table of Contents
6+
1. [Clean Code](#clean-code)
7+
2. [Single Responsibility Principle (SRP)](#single-responsibility-principle)
8+
3. [Immutability](#immutability)
9+
4. [Dependency Injection (DI)](#dependency-injection)
10+
5. [Guidelines for New Code](#guidelines-for-new-code)
11+
12+
## Clean Code
13+
14+
Clean code is self-explanatory and easy to understand without extensive comments. Here are some key principles:
15+
16+
- Use meaningful and pronounceable variable names
17+
- Use meaningful and pronounceable function names
18+
- Avoid using flags as function parameters
19+
- Functions should do one thing
20+
- Use default arguments instead of short-circuiting or conditionals
21+
22+
Example:
23+
```javascript
24+
// Good
25+
function createUser(name, email, isAdmin = false) {
26+
// ...
27+
}
28+
29+
// Avoid
30+
function createUser(name, email, admin) {
31+
const isAdmin = admin || false;
32+
// ...
33+
}
34+
```
35+
36+
## Single Responsibility Principle
37+
38+
Each module or class should have responsibility over a single part of the functionality provided by the software, and that responsibility should be entirely encapsulated by the class.
39+
40+
Example:
41+
```javascript
42+
// Good
43+
class UserAuthentication {
44+
authenticate(user, password) {
45+
// ...
46+
}
47+
}
48+
49+
class UserRepository {
50+
save(user) {
51+
// ...
52+
}
53+
}
54+
55+
// Avoid
56+
class User {
57+
authenticate(password) {
58+
// ...
59+
}
60+
61+
save() {
62+
// ...
63+
}
64+
}
65+
```
66+
67+
## Immutability
68+
69+
Prefer immutable data structures and pure functions. This helps prevent unintended side effects and makes the code easier to reason about.
70+
71+
Example:
72+
```javascript
73+
// Good
74+
const addItem = (list, item) => [...list, item];
75+
76+
// Avoid
77+
const addItem = (list, item) => {
78+
list.push(item);
79+
return list;
80+
};
81+
```
82+
83+
## Dependency Injection
84+
85+
Use dependency injection to decouple the construction of objects from their usage. This makes the code more modular and easier to test.
86+
87+
Example:
88+
```javascript
89+
// Good
90+
class UserService {
91+
constructor(userRepository) {
92+
this.userRepository = userRepository;
93+
}
94+
95+
getUser(id) {
96+
return this.userRepository.findById(id);
97+
}
98+
}
99+
100+
// Avoid
101+
class UserService {
102+
constructor() {
103+
this.userRepository = new UserRepository();
104+
}
105+
106+
getUser(id) {
107+
return this.userRepository.findById(id);
108+
}
109+
}
110+
```
111+
112+
## Guidelines for New Code
113+
114+
When adding new code to the project, follow these guidelines:
115+
116+
1. Ensure your code adheres to the principles outlined in this document
117+
2. Write unit tests for new functionality
118+
3. Update documentation if you're changing existing APIs or adding new features
119+
4. Use ESLint to catch potential issues early
120+
5. Perform a self-review before submitting your code for review
121+
122+
Remember, the goal is to write code that is easy to understand, maintain, and extend. When in doubt, prioritize readability and simplicity over cleverness.

0 commit comments

Comments
 (0)