diff --git a/README.md b/README.md index 4a6a76b..4d95d05 100644 --- a/README.md +++ b/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)