Skip to content

Commit 23923fe

Browse files
committed
First commit
0 parents  commit 23923fe

File tree

2 files changed

+145
-0
lines changed

2 files changed

+145
-0
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Ryan McDermott
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE

README.md

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# clean-code-javascript
2+
Software engineering principles, from Robert C. Martin's wonderful book *Clean Code*, adapted for JavaScript.
3+
4+
## **Variables**
5+
### Use meaningful and pronounceable variable names
6+
7+
**Bad:**
8+
```javascript
9+
var yyyymmdstr = moment().format('YYYY/MM/DD');
10+
```
11+
12+
**Good**:
13+
```javascript
14+
var yearMonthDay = moment().format('YYYY/MM/DD');
15+
```
16+
17+
### Use the same vocabulary for the same type of variable
18+
19+
**Bad:**
20+
```javascript
21+
getUserInfo();
22+
getClientData();
23+
getCustomerRecord();
24+
```
25+
26+
**Good**:
27+
```javascript
28+
getUser();
29+
```
30+
31+
## **Functions**
32+
### Limit the amount of function parameters (2 or less)
33+
Use an object if you are finding yourself needing a lot of parameters
34+
35+
**Bad:**
36+
```javascript
37+
function createMenu(title, body, buttonText, cancellable) {
38+
...
39+
}
40+
```
41+
42+
**Good**:
43+
```javascript
44+
var menuConfig = {
45+
title: 'Foo',
46+
body: 'Bar',
47+
buttonText: 'Baz'
48+
cancellable: true
49+
}
50+
51+
function createMenu(config) {
52+
...
53+
}
54+
55+
```
56+
57+
### Don't use flags as function parameters
58+
Flags tell your user that this function does more than one thing. Functions should do one thing. Split out your functions if they are following different code paths based on a boolean.
59+
60+
**Bad:**
61+
```javascript
62+
function createFile(name, temp) {
63+
if (temp) {
64+
fs.create('./temp/' + name);
65+
} else {
66+
fs.create(name);
67+
}
68+
}
69+
70+
```
71+
72+
**Good**:
73+
```javascript
74+
function createTempFile(name) {
75+
fs.create('./temp/' + name);
76+
}
77+
78+
function createFile(name) {
79+
fs.create(name);
80+
}
81+
```
82+
83+
## **Comments**
84+
### Only comment things that have business logic complexity.
85+
Comments are an apology, not a requirement. Good code *mostly* documents itself.
86+
87+
**Bad:**
88+
```javascript
89+
function hashIt(data) {
90+
// The hash
91+
var hash = 0;
92+
93+
// Length of string
94+
var length = data.length;
95+
96+
// Loop through every character in data
97+
for (var i = 0; i < length; i++) {
98+
// Get character code.
99+
var char = i.charCodeAt(i);
100+
// Make the hash
101+
hash = ((hash << 5) - hash) + char;
102+
// Convert to 32-bit integer
103+
hash = hash & hash;
104+
}
105+
}
106+
```
107+
108+
**Good**:
109+
```javascript
110+
111+
function hashIt(data) {
112+
var hash = 0;
113+
var length = data.length;
114+
115+
for (var i = 0; i < length; i++) {
116+
var char = i.charCodeAt(i);
117+
hash = ((hash << 5) - hash) + char;
118+
119+
// Convert to 32-bit integer
120+
hash = hash & hash;
121+
}
122+
}
123+
124+
```

0 commit comments

Comments
 (0)