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:
Victor Luca 2019-05-30 14:02:11 +02:00 committed by Dumitru
parent cff7d02f87
commit a00de960c5

View file

@ -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)