Skip to content

Added enum example in variables section. #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 30, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the motivation behind this enum being string-based?
It is then arguable that it has advantage over bad example with just constants.

Would it be better maybe to use constant member, like:

enum Genre {
  Romantic,
  Drama,
  ...
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rethought the intent and motivation of enum.

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)
Expand Down