From a00de960c5da9f6d2b92fcc44e4be0dd1f4a987f Mon Sep 17 00:00:00 2001 From: Victor Luca Date: Thu, 30 May 2019 14:02:11 +0200 Subject: [PATCH] Added enum example in variables section. (#25) * Added enum example in variables section. * Rethought the enum motivation and example. * Fixed typo. --- README.md | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) 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)