Skip to content

Commit

Permalink
Merge pull request #318 from petetnt/patch-1
Browse files Browse the repository at this point in the history
Update Tip #3 Improve Nested Conditionals with a refactoring example
  • Loading branch information
zenopopovici committed Mar 5, 2016
2 parents cda8b2b + bd8b679 commit 077154d
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion _posts/en/2016-01-03-improve-nested-conditionals.md
Expand Up @@ -73,7 +73,17 @@ switch(true) {
}
```

But we must always avoid having several checks in every condition and avoid using `switch` as much as possible. We also must take into account that the most efficient way to do this is through an `object`.
If refactoring is an option, we can try to simplify the functions themselves. For example instead of having a function for each background color we could have an function that takes the color as an argument.

```javascript
function printBackground(color) {
if (!color || typeof color !== 'string') {
return; // Invalid color, return immediately
}
}
```

But if refactoring is not an option, we must always avoid having several checks in every condition and avoid using `switch` as much as possible. We also must take into account that the most efficient way to do this is through an `object`.

```javascript
var colorObj = {
Expand Down

0 comments on commit 077154d

Please sign in to comment.