Skip to content

Commit

Permalink
move GOOD/BAD into code
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudhead committed May 21, 2011
1 parent da1c941 commit f36f6be
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions JavaScript.md
Expand Up @@ -4,9 +4,9 @@

- **RULE**: Identifiers bound to contructor functions **must** start with a capital.

### GOOD

```javascript
// GOOD

var User = function (name, age) {
this.name = name;
Expand All @@ -16,9 +16,9 @@

```

### BAD

```javascript
// BAD

var user = function (name, age) {
this.name = name;
Expand All @@ -30,17 +30,17 @@

- **RULE**: Identifiers bound to a variable **must** be CamelCased, and start with a lowercase letter.

### GOOD

```javascript
// GOOD

var someKindOfProcess = function () {};

```

### BAD

```javascript
// BAD

var some_kind_of_process = function () {};

Expand All @@ -49,12 +49,12 @@
## Variable declarations

- **RULE**: Always declare variables at the *top* of functions.
- **REASON:**: Variable declarations are moved up to the top of the function scope anyway,
- **REASON**: Variable declarations are moved up to the top of the function scope anyway,
so that's where they belong.

### GOOD

```javascript
// GOOD

function (a, b) {
var k;
Expand All @@ -67,9 +67,9 @@ so that's where they belong.

```

### BAD

```javascript
// BAD

function (a, b) {
if (a == b) {
Expand All @@ -83,19 +83,19 @@ so that's where they belong.
## Functions

- **RULE**: Anonymous functions **should** have a space between the `function` keyword and the left parenthesis.
- **REASON**: To emphasise the lack of the anonimity and differentiate them with named functions.
- **REASON**: To emphasise the lack of identifier and differentiate them with named functions.

### GOOD

```javascript
// GOOD

function (a, b) {}

```

### BAD

```javascript
// BAD

function(a, b) {}

Expand All @@ -104,17 +104,17 @@ so that's where they belong.
- **RULE**: Named functions **should not** have a space between the function name and the left parenthesis.
- **REASON**: See above.

### GOOD

```javascript
// GOOD

function add(a, b) {}

```

### BAD

```javascript
// BAD

function add (a, b) {}

Expand All @@ -125,20 +125,20 @@ so that's where they belong.
- **RULE**: Semicolons `;` **must** be added at the end of every statement, **except** when preceding a closing bracket `}`.
- **REASON**: That's what Jesus would have wanted.

### GOOD

```javascript
// GOOD

var f = function add(a, b) {
if (a == b) { return a * 2 } // No `;` here.
if (a == b) { return a * 2 } // No `;` here.
return a + b;
};

```

### BAD

```javascript
// BAD

var f = function add (a, b) {
if (a == b) { return a * 2; }
Expand All @@ -152,22 +152,23 @@ so that's where they belong.
- **RULE**: Braces **must** be used in all circumstances.
- **REASON**: JavaScript uses braces to structure code.

### GOOD

```javascript
// GOOD

if (x) { return true }

```

### BAD

```javascript
// BAD

if (x) return true;

```
```javascript
// BAD

if (x)
return true;
Expand All @@ -176,27 +177,28 @@ so that's where they belong.
- **RULE**: Braces **must never** be on a line of their own.
- **REASON**: It wastes screen space.

### GOOD

```javascript
// GOOD

if (x) {
return true;
};

```

### BAD

```javascript
// BAD

if (x)
{
return true;
return true;
}

```
```javascript
// BAD

if (x) {
return true; }
Expand Down

0 comments on commit f36f6be

Please sign in to comment.