mirror of
https://github.com/labs42io/clean-code-typescript.git
synced 2024-11-23 05:34:04 +00:00
Added enum example in variables section. (#25)
* Added enum example in variables section. * Rethought the enum motivation and example. * Fixed typo.
This commit is contained in:
parent
cff7d02f87
commit
a00de960c5
53
README.md
53
README.md
|
@ -243,6 +243,59 @@ function loadPages(count: number = 10) {
|
|||
|
||||
**[⬆ back to top](#table-of-contents)**
|
||||
|
||||
### Use enum to document the intent
|
||||
|
||||
Enums can help you document the intent of the code. For example when we are concerned about values being
|
||||
different rather than the exact value of those.
|
||||
|
||||
**Bad:**
|
||||
|
||||
```ts
|
||||
const GENRE = {
|
||||
ROMANTIC: 'romantic',
|
||||
DRAMA: 'drama',
|
||||
COMEDY: 'comedy',
|
||||
DOCUMENTARY: 'documentary',
|
||||
}
|
||||
|
||||
projector.configureFilm(GENRE.COMEDY);
|
||||
|
||||
class Projector {
|
||||
// delactation of Projector
|
||||
configureFilm(genre) {
|
||||
switch (genre) {
|
||||
case GENRE.ROMANTIC:
|
||||
// some logic to be executed
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**Good:**
|
||||
|
||||
```ts
|
||||
enum GENRE {
|
||||
ROMANTIC,
|
||||
DRAMA,
|
||||
COMEDY,
|
||||
DOCUMENTARY,
|
||||
}
|
||||
|
||||
projector.configureFilm(GENRE.COMEDY);
|
||||
|
||||
class Projector {
|
||||
// delactation of Projector
|
||||
configureFilm(genre) {
|
||||
switch (genre) {
|
||||
case GENRE.ROMANTIC:
|
||||
// some logic to be executed
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**[⬆ back to top](#table-of-contents)**
|
||||
|
||||
## Functions
|
||||
|
||||
### Function arguments (2 or fewer ideally)
|
||||
|
|
Loading…
Reference in a new issue