labs42io /clean-code-typescript
Clean Code concepts adapted for TypeScript
★ 9,842⑂ 1,168👁 127⑃ 49● 25▤ 0
+0
Daily
9,842
stars
1,168
forks
127
watchers
49
prs
25
issues
0
releases
README
clean-code-typescript
Clean Code concepts adapted for TypeScript.
Inspired from clean-code-javascript.
Table of Contents
1. Introduction
2. Variables
3. Functions
4. Objects and Data Structures
5. Classes
6. SOLID
7. Testing
8. Concurrency
9. Error Handling
10. Formatting
11. Comments
12. Translations
Introduction
!Humorous image of software quality estimation as a count of how many expletives
you shout when reading code
Software engineering principles, from Robert C. Martin's book
Clean Code,
adapted for TypeScript. This is not a style guide. It's a guide to producing
readable, reusable, and refactorable software in TypeScript.
Not every principle herein has to be strictly followed, and even fewer will be
universally agreed upon. These are guidelines and nothing more, but they are
ones codified over many years of collective experience by the authors of
Clean Code.
Our craft of software engineering is just a bit over 50 years old, and we are
still learning a lot. When software architecture is as old as architecture
itself, maybe then we will have harder rules to follow. For now, let these
guidelines serve as a touchstone by which to assess the quality of the
TypeScript code that you and your team produce.
One more thing: knowing these won't immediately make you a better software
developer, and working with them for many years doesn't mean you won't make
mistakes. Every piece of code starts as a first draft, like wet clay getting
shaped into its final form. Finally, we chisel away the imperfections when
we review it with our peers. Don't beat yourself up for first drafts that need
improvement. Beat up the code instead!
⬆ back to top
Variables
Use meaningful variable names
Distinguish names in such a way that the reader knows what the differences offer.
Bad:
ts
function between(a1: T, a2: T, a3: T): boolean {
return a2 (value: T, left: T, right: T): boolean {
return left ;
for (const keyValue of users) {
// iterate through users map
}
Good:
ts
declare const users: Map;
for (const [id, user] of users) {
// iterate through users map
}
⬆ back to top
Avoid Mental Mapping
Explicit is better than implicit.
Clarity is king.
Bad:
ts
const u = getUser();
const s = getSubscription();
const t = charge(u, s);
Good:
ts
const user = getUser();
const subscription = getSubscription();
const transaction = charge(user, subscription);
⬆ back to top
Don't add unneeded context
If your class/type/object name tells you something, don't repeat that in your variable name.
Bad:
ts
type Car = {
carMake: string;
carModel: string;
carColor: string;
}
function print(car: Car): void {
console.log(${car.carMake} ${car.carModel} (${car.carColor}));
}
Good:
ts
type Car = {
make: string;
model: string;
color: string;
}
function print(car: Car): void {
console.log(${car.make} ${car.model} (${car.color}));
}
⬆ back to top
Use default arguments instead of short circuiting or condition