From 59c8085f22bd8447af097ed2a32e387e7e83e6b6 Mon Sep 17 00:00:00 2001 From: Vitiok Date: Thu, 9 May 2019 21:27:50 +0300 Subject: [PATCH 1/3] Added enum example in variables section. --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index f9e232d..ea28db7 100644 --- a/README.md +++ b/README.md @@ -243,6 +243,38 @@ function loadPages(count: number = 10) { **[⬆ back to top](#table-of-contents)** +### Use enum for same type constants + +Enum helps you create a set of distinct cases or to define a set of named constants. + +**Bad:** + +```ts +const GENRE = { + ROMANTIC: 'Romantic', + DRAMA: 'Drama', + COMEDY: 'Comedy', + DOCUMENTARY: 'Documentary', +} + +const comedy = async requestFilm(GENRE.COMEDY); +``` + +**Good:** + +```ts +enum GENRE { + ROMANTIC = 'Romantic', + DRAMA = 'Drama', + COMEDY = 'Comedy', + DOCUMENTARY = 'Documentary', +} + +const comedy = async requestFilm(GENRE.COMEDY); +``` + +**[⬆ back to top](#table-of-contents)** + ## Functions ### Function arguments (2 or fewer ideally) From aeb7c16270f671517ec61003d34af8dd07b9f21a Mon Sep 17 00:00:00 2001 From: Vitiok Date: Thu, 30 May 2019 12:33:39 +0200 Subject: [PATCH 2/3] Rethought the enum motivation and example. --- README.md | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index ea28db7..df9fae3 100644 --- a/README.md +++ b/README.md @@ -243,34 +243,55 @@ function loadPages(count: number = 10) { **[⬆ back to top](#table-of-contents)** -### Use enum for same type constants +### Use enum to document the intent -Enum helps you create a set of distinct cases or to define a set of named constants. +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', + ROMANTIC: 'romantic', + DRAMA: 'drama', + COMEDY: 'comedy', + DOCUMENTARY: 'documentary', } -const comedy = async requestFilm(GENRE.COMEDY); +projector.configureFilm(GENRE.COMEDY); + +class Projector { + // delactation of Projector + configureFilm(genre) { + switch (genre) { + case GENRE.ROMANTIC: + // some logit to be executed + } + } +} ``` **Good:** ```ts enum GENRE { - ROMANTIC = 'Romantic', - DRAMA = 'Drama', - COMEDY = 'Comedy', - DOCUMENTARY = 'Documentary', + ROMANTIC, + DRAMA, + COMEDY, + DOCUMENTARY, } -const comedy = async requestFilm(GENRE.COMEDY); +projector.configureFilm(GENRE.COMEDY); + +class Projector { + // delactation of Projector + configureFilm(genre) { + switch (genre) { + case GENRE.ROMANTIC: + // some logit to be executed + } + } +} ``` **[⬆ back to top](#table-of-contents)** From 101785bf377baa2348fad187e37accbbd2b6e5b1 Mon Sep 17 00:00:00 2001 From: Vitiok Date: Thu, 30 May 2019 13:57:51 +0200 Subject: [PATCH 3/3] Fixed typo. --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index df9fae3..eb3f920 100644 --- a/README.md +++ b/README.md @@ -265,7 +265,7 @@ class Projector { configureFilm(genre) { switch (genre) { case GENRE.ROMANTIC: - // some logit to be executed + // some logic to be executed } } } @@ -288,7 +288,7 @@ class Projector { configureFilm(genre) { switch (genre) { case GENRE.ROMANTIC: - // some logit to be executed + // some logic to be executed } } }