Skip to content
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

add object literals section #403

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
92 changes: 90 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -12,8 +12,9 @@
8. [Concurrency](#concurrency)
9. [Error Handling](#error-handling)
10. [Formatting](#formatting)
11. [Comments](#comments)
12. [Translation](#translation)
11. [Object Literals](#object-literals)
12. [Comments](#comments)
13. [Translation](#translation)

## Introduction

@@ -2218,6 +2219,93 @@ review.perfReview();

**[⬆ back to top](#table-of-contents)**

## **object-literals**

### Replace switch and else if statements with Object literals

**Bad:**

```javascript
const getSiteTheme = () => {
const theme = localStorage.getItem('site.theme');

if (theme === 'default') {
return {
palette: {...},
shadows: {...},
}
} else if (theme === 'light') {
return {
palette: {...},
shadows: {...},
}
} else {
return {
palette: {...},
shadows: {...},
}
}
}
```

**Also Bad:**

```javascript
const getSiteTheme = () => {
const theme = localStorage.getItem('site.theme');

switch(theme) {
case 'default':
return {
palette: {...},
shadows: {...},
};
case 'light':
return {
palette: {...},
shadows: {...},
};
case 'dark':
return {
palette: {...},
shadows: {...},
};
}
}
```

**Good:**

```javascript
const SITE_THEME_LS_KEY = 'site.theme';

const themes = {
default: {
palette: {...},
shadows: {...},
},
lightnes: {
palette: {...},
shadows: {...},
},
dark: {
palette: {...},
shadows: {...},
},
};

const getSiteTheme = () => {
const theme = localStorage.getItem(SITE_THEME_LS_KEY);
const result = themes[theme];

if (!theme) throw new Error('Theme can only be default, light or dark.')

return theme
}
```

**[⬆ back to top](#table-of-contents)**

## **Comments**

### Only comment things that have business logic complexity.