Skip to content

Commit

Permalink
Fix Conditional Blocks on if statements after discussion with Kevin Marx
Browse files Browse the repository at this point in the history
  • Loading branch information
mplewis committed Jan 2, 2015
1 parent ea453f3 commit 77eefda
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions javascript.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,26 +72,37 @@ var pointX = 2, pointY = 4, pointZ = 0

### Conditional Blocks

Conditional blocks should be formatted with an *'uncuddled'* `else` in the case of larger blocks of code:
Conditional blocks should be formatted with a "cuddled" `else`:

**Preferred:**

```javascript
if (x == y) {
doSomthing(x)
doSomething(x)
callMethod()
x += y
y = 0
doSomethingElse(y)
}
else {
} else {
callMethod2()
x++
someMethod()
}
```

In the case of smaller blocks containing only one or two lines, use a *'cuddled'* `else`:
It's OK to write single line if statements with or without braces.

**Preferred:**

```javascript
if (a == b) return c
```

```javascript
if (a == b) { return c }
```

**Do not** write multi line if statements without braces. [Read more on why here](objective-c.html#conditionals).

**Preferred:**

Expand All @@ -103,14 +114,6 @@ if (x == y) {
}
```

**Do not** use if statements without braces. [Read more on why here](/objective-c.html#conditionals).

The SparkNotes edition of that page:

* Conditional bodies should always use braces even when a conditional body could be written without braces.
* One-liner if statements without braces are **dangerous.**
* One-liner if statements with braces are acceptable.

**Not Preferred:**

```javascript
Expand Down

0 comments on commit 77eefda

Please sign in to comment.