์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
- branch ํ์ธ
- Git๋ช ๋ น์ด
- branch ์ญ์
- ๋ธ๋์น ์์ฑ
- ์ฝ๋๋ธ๋ญ
- ๋ธ๋์น ํ์ธ
- markdown
- ๋ธ๋์น ์ญ์
- ์ฝ๋๋ธ๋ก
- ๋งํฌ๋ค์ด
- branch ์์ฑ
- ![rejected]
- Today
- Total
DevLog
์์๋ณด๊ธฐ ์ฌ์ด ์ฝ๋ ์์ฑ๋ฒ ๋ณธ๋ฌธ
์์๋ณด๊ธฐ ์ฌ์ด ์ฝ๋ ์์ฑ๋ฒ
Seungjae Lee 2021. 6. 16. 23:55โถ Indentation – ๋ค์ฌ์ฐ๊ธฐ
Good:
if (condition) {
action();
}
// ์ฝ๋์ ๋ก์ง์ด ๋ช
ํํ๊ฒ ์ข
์์ ์ผ๋ก ๋๋๋ ์ฝ๋๋ฅผ ์ธ ๋,
// ์ข
์๋ code block๋ ์ฃผ์ธ code block๋ณด๋ค ๋ ์นธ ๋ค์ฌ์ฐ๊ธฐ ํฉ๋๋ค.
Bad:
if (condition) {
action();
}
๋ค์ฌ์ฐ๊ธฐ๋ฅผ ํ ๋ ํญ์ด ์๋ ์คํ์ด์ค๋ฅผ ๊ถ์ฅํฉ๋๋ค.
๋ค์ฌ์ฐ๊ธฐ์ ๊ด๋ จ๋ ํญ๊ณผ ์คํ์ด์ค ์ฌ์ด์ ๋ ผ์์ ํ๋ก๊ทธ๋๋ฐ ์ธ๊ณ์์๋ ์์ฃผ ์ค๋๋ ๋ ผ์์ ๋๋ค.
๊ทธ๋ ๊ธฐ์ ์ทจํฅ์ ์ฐจ์ด์ผ ๋ฟ ์ด๊ฒ์ด ๋ง๋ค ํ๋ฆฌ๋ค์ ๋ฌธ์ ๋ ์๋์ง๋ง,
๋ง์ JavaScript ํ๋ก์ ํธ์์ ๋๋ถ๋ถ์ ํ๋ก์ ํธ๊ฐ 2๊ฐ์ ์คํ์ด์ค๋ฅผ ์ฐ๊ณ ์๊ณ ,
์ ์ฐจ ๋ค์ฌ์ฐ๊ธฐ ๋ ผ์์ ์น๋ฆฌ์๊ฐ ๋์์ต๋๋ค.
๋ค์์ ์คํ์์ค ํ๋ก์ ํธ๊ฐ ์งํ์ค์ธ GitHub์์๋,
Star(์ผ์ข ์ '์ข์์')๋ฅผ ๋ฐ์ ํ๋ก์ ํธ์ 85% ์ด์์ JavaScript ํ๋ก์ ํธ๊ฐ ์คํ์ด์ค ๋ค์ฌ์ฐ๊ธฐ๋ฅผ ์ฌ์ฉํ๊ณ ์์ต๋๋ค.์ฌ๋ฌ๋ถ์ด ์ ๋๋ก ํผํด์ผ ํ ๋จ ํ๊ฐ์ง๋, ๋ฐ๋ก ์คํ์ด์ค์ ํญ์ ํผ์ฉํด์ ์ฐ๋ ๊ฒ์ ๋๋ค!
Good:
if (condition) {
action();
}
// code block์ ๋ง์ง๋ง ์ค์ ์ธ ๋ ๋ง์ง๋ง ์ค์ ์์์, ์์ํ ๋ ์ค์ ์์๊ณผ ๋์ผํ ๊ณณ์์ ํด์ฃผ์ธ์.
Bad:
if (condition) {
action();
}
// ์ข
์๋ code block์ ์์์ ๋ง์ถ๋ฉด ์๋ฉ๋๋ค.
Bad:
transmogrify({
a: {
b: function(){
}
}});
// code block์ด ๋ฐ๋๊ณ ํด๋น code block์ ๋ง์ถฐ ์ค์ ๋ฐ๊ฟ ๋ 2์นธ ๋ค์ฌ์ฐ๊ธฐ ๊ท์น์ ์ง์ผ์ผ ํฉ๋๋ค.
// ์์ฑ a ์์ ์์ฑ b๊ฐ ์๋๋ฐ, ๋ง์น ๊ฐ์ ๋ฒ์ฃผ์ ์๋ ์์ฑ์ผ๋ก ๋ณด์
๋๋ค. ๋ํ ํจ์์ ๊ฐ์ฒด์ ๊ตฌ๋ถ์ด ๋ช
ํํ์ง ์์ต๋๋ค.
โถ Naming – ์ด๋ฆ ์ง๊ธฐ
๋ณ์๋ช ์ ๊ฐ์ ๋ณธ์ง์ ์ธ ์๋ฏธ๋ฅผ ๊ฐ์ง๊ณ ์์ด์ผ ํฉ๋๋ค.
๋ณ์์ ์ด๋ฆ์ ํ ๋จ์ด(Descriptive word)๋ก ํํํ๋ ๊ฒ์ด ๊ฐ์ฅ ์ข์ต๋๋ค. ๊ฐ๋ฐ ๋ถ์ผ(domain)์ ํต์ฌ์ ์ ๋ฌ์ฌํด์ฃผ๋ ๋จ์ด์ผ์๋ก ์ข์ต๋๋ค. ์๋ฅผ ๋ค์ด ๊ธ์ต ๊ด๋ จ ๊ฐ๋ฐ์ ํ๋ ๊ฒฝ์ฐ, ๊ทธ ์ฐ์ ๋ถ์ผ์์ ์ฌ์ฉํ๋ ์ฉ์ด๋ฅผ ๊ฐ๋ฅํ๋ฉด ๊ทธ๋๋ก ์ฐ๋ ๊ฒ์ด ์ข์ต๋๋ค.
Good:
let interestRate = 0.2;
// ์ด์์จ์์ ์ฝ๊ฒ ์ ์ ์์ต๋๋ค. ์ด๋ค ๊ธ์ต์ํ์ ์ด์์จ์ธ์ง ํ๊ธฐํ ์ ์์ผ๋ฉด ๋ ์ข๊ฒ ๋ค์.
Bad:
let numA = 0.2;
// ๋จ์ง ์ซ์๋ผ๋ ๊ฒ ์๋ฏธ ์ธ ๋ค๋ฅธ ์๋ฏธ๊ฐ ์์ด ๋ฌด์จ ๋ณ์์ธ์ง ์ถ์ ํด์ผ ํฉ๋๋ค.
๋ณ์์ ํ ๋น๋๋ ๊ฐ์ ํ์์ด ์๋, ๊ทธ ๊ฐ์ ์๋ฏธ๊ฐ ๋ณ์ ์ด๋ฆ์ ๋ฐ์๋์ด์ผ ํฉ๋๋ค.
Good:
let animals = ['cat', 'dog', 'fish']; // ๋ณ์๊ฐ ๊ฐ์ง๋ ์๋ฏธ๋ฅผ ๋ช
ํํ๊ฒ ํํํ๊ณ ์์ต๋๋ค.
Bad:
let targetInputs = ['cat', 'dog', 'fish'];
// ๋ณ์๊ฐ ๊ฐ์ง๋ ๊ตฌ์กฐ๋ฅผ ํํํ๊ณ ์๋ ๋ณ์๋ช
์ผ๋ก, animals๊ฐ ๋ ๋ซ์ต๋๋ค.
Very bad:
let array = ['cat', 'dog', 'fish'];
// targetinputs ๋ณด๋ค ๋์ฑ ์๋ฏธ๊ฐ ๋ถ๋ช
ํํฉ๋๋ค.
๋ฐ์ดํฐ์ ๋ชจ์(collection)์ด ํ ๋น๋ ๋ณ์์ ์ด๋ฆ์ ๋ณต์ ๋ช ์ฌ๊ฐ ์ข์ต๋๋ค.
Good:
let animals = ['cat', 'dog', 'fish'];
// ํ๋์ ๋๋ฌผ์ด ์๋๋ผ, ์ฌ๋ฌ ๋ง๋ฆฌ์ ๋๋ฌผ์ด ์๋ ๋ฐ์ดํฐ์์ด ๋ช
์์ ์ผ๋ก ํํ๋์ด ์์ต๋๋ค.
Bad:
let animalList = ['cat', 'dog', 'fish'];
// animals๋ก ํํ๋๋ ๊ฒ์ด ์๋ฏธ๋ฅผ ์กฐ๊ธ ๋ ์ ํํํฉ๋๋ค.
Very Bad:
let animal = ['cat', 'dog', 'fish'];
// ํ๋์ ๋๋ฌผ์ธ์ง, ์ฌ๋ฌ ๋ง๋ฆฌ์ ๋๋ฌผ ๋ฆฌ์คํธ์ธ์ง ์ ์ ์์ต๋๋ค.
boolean์ด ํ ๋น๋ ๋ณ์๋ is ํน์ are ์ ๋ถ์ฌ์ ์ฐธ ํน์ ๊ฑฐ์ง์์ ๋ถ๋ช ํ ํํํฉ๋๋ค.
๋ง์ ๊ฐ๋ฐ์๋ค์ด ์ด๋ฏธ boolean์ด ํ ๋น๋ ๋ณ์ ์ด๋ฆ์ is ํน์ are์ ๋ถ์ฌ์์ต๋๋ค. ์ ๊ทธ๋ด๊น์?
Carry is a dog
"์ผ๋ฆฌ๋ ๊ฐ์์ง์ ๋๋ค"๋ผ๋ ์ฌ์ค์ ์ฝ๋๋ก ๊ฐ์ฅ ์ฝ๊ฒ ํํํ๋ ๋ฐฉ๋ฒ์ด๊ธฐ ๋๋ฌธ์ ๋๋ค. "๊ฐ์์ง์ ๋๋ค"๊ฐ ์ฐธ(true)์ธ์ง ๊ฑฐ์ง(false)์ธ์ง ๋ช ํํ๊ฒ ๋ณด์ฌ์ฃผ๊ณ ์์ฃ .
let isDog = true;
let ๊ฐ์์ง์
๋๋ค = true;
Good:
let areEqual = true;
Bad:
let pass = true;
ํจ์์ ์ด๋ฆ์ ๋์ฌ๋ก ์์ํ๋๊ฒ ์ข์ต๋๋ค.
ํจ์์ ๊ธฐ๋ฅ์ ๋ด์ ๋์ฌ๋ฅผ ํจ์๋ช ์ ํฌํจํ๋ฉด ์ญํ ์ ์ฝ๊ฒ ํ์ ํ ์ ์์ต๋๋ค. ๋ํ, ํจ์์ ์ ๋ ฅ๊ฐ๊ณผ ์ถ๋ ฅ๊ฐ, ๊ทธ๋ฆฌ๊ณ ๋ ์ฌ์ด์ ๋ณํ ๊ณผ์ ์ ํ์ ํ๊ธฐ๊ฐ ์ฝ์ต๋๋ค.
Good:
let countBlocks = function() {
// ๋ฒฝ๋์ ๊ฐ์๋ฅผ ์ธ๋ ํจ์์์ ์ฝ๊ฒ ์ ์ ์์ต๋๋ค.
}
Good:
let countWaterBlocksBetweenTowers = function() {
// ํ์ ์ฌ์ด์ ๋ฒฝ๋์ ๊ฐ์๋ฅผ ์ธ๋ ํจ์์์ ์ฝ๊ฒ ์ ์ ์์ต๋๋ค.
}
Bad:
let waterBlocks = function() {
// ๋ณ์๋ช
์ผ๋ก๋ ์ญํ ์ด ํ์
์ด ๋์ง ์๊ธฐ ๋๋ฌธ์, ์๋์ ์ฃผ์์ด ์์ผ๋ฉด ๋ฌด์จ ์ญํ ์ ํ๋์ง ํ์
ํ๊ธฐ ์ด๋ ต์ต๋๋ค.
// ์ด ํจ์๋ ๋ฒฝ๋์ ๊ฐ์๋ฅผ ์ธ๋ ํจ์์
๋๋ค. ํ์ ์ฌ์ด์ ๋ฒฝ๋์ ์ธ๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค.
}
๋ณ์ ํ ๋น ๊ฐ์ด Class์ธ ๊ฒฝ์ฐ์ ์ฃผ๋ก ๋ณ์์ ์ฒซ ๊ธ์๋ฅผ ๋๋ฌธ์๋ก ์ฌ์ฉํฉ๋๋ค.
class Animal(){
// ES6 ๋ถํฐ ์ฌ์ฉ ๊ฐ๋ฅํ class๊ฐ ํ ๋น๋๋ ๋ณ์๋ ์ฒซ ๊ธ์๋ฅผ ๋๋ฌธ์๋ก ์ ์ต๋๋ค.
}
- new ํค์๋๋ฅผ ์ฌ์ฉํ ํจ์์ ํํด์ ๋๋ฌธ์๋ฅผ ์ฐ๊ธฐ๋ ํฉ๋๋ค. class ์ญํ ์ ํ๊ธฐ ๋๋ฌธ์ด์ฃ .
function Animal() {
// class ์์ฑ์ ํจ์๊ฐ ๋ ํจ์์์ผ๋ก ๋๋ฌธ์๋ก ์ ์ต๋๋ค.
}
์์๋ ๋ชจ๋ ๋๋ฌธ์๋ก ์ ์ต๋๋ค.
const MAX_ITEMS_IN_QUEUE = 100;
โถ ๊ธฐํธ ๋ฐ ๊ตฌ๋์ (punctuation)
๋ฌธ๋ฒ์ ์ผ๋ก ์๋ต ๊ฐ๋ฅํ ๋์๋ ์ค๊ดํธ๋ ์๋ตํ์ง ์์ต๋๋ค.
Good:
for (key in object) {
alert(key);
}
// for๋ฌธ์ ์๋ ๋ฒ์๋ฅผ ๋์ผ๋ก ํ์ธ ๊ฐ๋ฅํฉ๋๋ค.
Bad:
for (key in object)
alert(key);
// for๋ฌธ์ ์๋ ๋ฒ์๋ฅผ ๋์ผ๋ก ํ์ธํ ์ ์์ต๋๋ค.
JavaScript์ ๋ฌธ์์ด ํ์๋ฅผ ์ํด์ ์์ ๋ฐ์ดํ๋ฅผ ๊ถ์ฅํฉ๋๋ค.
HTML์์ ์ฌ์ฉํ๋ ํฐ ๋ฐ์ดํ์ ๊ตฌ๋ถํ๊ธฐ ์ํด์ ์๋ฐ์คํฌ๋ฆฝํธ์์ ๋ฌธ์์ด ํ๊ธฐ ์ ์์ ๋ฐ์ดํ๋ฅผ ๊ถ์ฅํฉ๋๋ค.
Good:
let dog = 'dog';
let cat = 'cat';
Acceptable:
let dog = "dog";
let cat = "cat";
// ํฐ ๋ฐ์ดํ๋ ๊ด์ฐฎ์ต๋๋ค.
Bad:
let dog = 'dog';
let cat = "cat";
// ๋์ด์ฐ๊ธฐ์ tab๊ณผ ๊ฐ์ด ํผ์ฉ์ ์ต์
์ ์ฝ๋ ์คํ์ผ์
๋๋ค.
์ค ๋ฐ๊ฟ์ด ํ์ํ ๋ฌธ์์ด์ ์ ์ํ ๋๋ ` (๋ฐฑํฑ, backtick) ์ฌ์ฉ์ ๊ถ์ฅํฉ๋๋ค.
Good:
let multilineText = `this is line one
this is line two
this is line three`;
// ์ํฐ๊ฐ ๋ช
ํํ๊ฒ ๋ณด์
๋๋ค.
Bad:
let multilineText = 'this is line one' + '\n' + 'this is line two' + '\n' + 'this is line three';
// ์ํฐ๊ฐ ๋ช
ํํ๊ฒ ๋ณด์ด์ง ์์ต๋๋ค.
์ฝ๋ ์คํ์ ๊ฐ์ฅ ์์ ๋จ์์ธ Statement(๋ฌธ)์ ๋์ ์ธ๋ฏธ์ฝ๋ก ์ ์ฌ์ฉํฉ๋๋ค.
Good:
alert('hi');
Bad:
alert('hi')
if , for , while ๋ฌธ์ ๋์๋ ์ธ๋ฏธ์ฝ๋ก ์ ์ฌ์ฉํ์ง ์์์ผ ํฉ๋๋ค.
์ค๊ดํธ๋ก ๋๋๋ Statement๋ ์ด๋ฏธ ์ข ๋ฃ๊ฐ ์์๋์ด ์๊ธฐ ๋๋ฌธ์, ์ธ๋ฏธ์ฝ๋ก ์ ์ฌ์ฉํ์ง ์์ต๋๋ค.
Good:
if (condition) {
response();
}
Bad:
if (condition) {
response();
};
ํจ์ ํํ์์ ๋์๋ ์ธ๋ฏธ์ฝ๋ก ์ ์ฌ์ฉํฉ๋๋ค.
if , for , while ๊ตฌ๋ฌธ์ ๋์ฒ๋ผ ๋ณด์ผ์ง๋ผ๋ ์ฝ๋ ๋์ ์ธ๋ฏธ์ฝ๋ก ์ ์จ์ผํฉ๋๋ค.
Good:
let greet = function () {
alert('hi');
};
Bad:
let greet = function () {
alert('hi');
}
์ธ๋ฏธ์ฝ๋ก ์ ์ฐ์ง ์์๋ ๋๋ค๊ณ ์ฃผ์ฅํ๋ ์ฌ๋๋ค๋ ๋๋ฌ ์์ผ๋ฉฐ, ESLint Standard Style์์๋ ์ธ๋ฏธ์ฝ๋ก ์ ์ธ ํ์๊ฐ ์๋ค๊ณ ์ด์ผ๊ธฐํ๊ณ ์์ต๋๋ค. ํ์ง๋ง ์ฌ์ ํ ๋ง์ ํ๋ก์ ํธ์์๋ ์ธ๋ฏธ์ฝ๋ก ์ ์ ํธํฉ๋๋ค. ๋คํํ๋ ์ต๊ทผ์ ์๋ฐ์คํฌ๋ฆฝํธ๋ ๋ค๋ฅธ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ ๋ฌ๋ฆฌ ์ธ๋ฏธ์ฝ๋ก ์ ์๋ฐํ๊ฒ ์ ์ง ์์๋ ์๋ฌ๊ฐ ๋๋ ๊ฒฝ์ฐ๋ ๊ฑฐ์ ์์ต๋๋ค. ์ธ๋ฏธ์ฝ๋ก ์๋ ์ฝ์ ๊ธฐ๋ฅ์ธ ASI(automatic semicolon insertion)์ด ๋๋ถ๋ถ์ ์๋ฐ์คํฌ๋ฆฝํธ ์์ง์์ ์๋ํ๊ธฐ ๋๋ฌธ์ ๋๋ค. ๊ทธ๋๋ ์ธ๋ฏธ์ฝ๋ก ์ ์ ์์ฑํ๋์ง ์๊ณ ์์ฑํ์ง ์๋ ๊ฒ๊ณผ, ๊ทธ๋ ์ง ์์ ๊ฒ์๋ ์ดํ ๋๋ฒ๊น ๋ฅ๋ ฅ ํฅ์์ ํฐ ์ฐจ์ด๊ฐ ์๊ธฐ๊ฒ ๋ฉ๋๋ค. "์ด๊ฑด ์ค๋ฅ๊ฐ ์๋๋ค."๋ผ๊ณ ํ์ ์ด ๋์ด์ผ, ๋ฌด์๋ฏธํ ์ฝ์ง์ ํ์ง ์์ต๋๋ค.
โถ ์ฐ์ฐ์์ ํค์๋
์๊ฒฉํ ๋น๊ต ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ธ์
์๊ฒฉํ์ง ์์ ๋์น ์ฐ์ฐ(loose equality, ==, !=)๋ ์๋ฐํ์ง ์๊ธฐ ๋๋ฌธ์ ๋ฐ๋์ ์๊ฒฉํ ๋์น ์ฐ์ฐ(strict equality, ===, !==)์ ์ฌ์ฉํ์ธ์.
Good:
if (0 === '') {
alert('looks like they\'re equal');
}
// ์๊ฒฉํ ๋์น ์ฐ์ฐ์ `0`๊ณผ `''`์ ๋ค๋ฅด๋ค๊ณ ํ๊ฐํฉ๋๋ค.
Bad:
if (0 == '') {
alert('looks like they\'re equal');
}
// ์๊ฒฉํ์ง ์์ ๋์น ์ฐ์ฐ์ `0`๊ณผ `''`์ ๊ฐ๋ค๊ณ ํ๊ฐํฉ๋๋ค.
3ํญ ์ฐ์ฐ์( ? )๋ ๊ฐ๊ฒฐํ๊ณ ๊ฐ๋ ์ฑ์ด ์ข์ ๊ฒฝ์ฐ๋ง ์ฌ์ฉํฉ๋๋ค.
3ํญ ์ฐ์ฐ์๋ ์ฝ๋๋ฅผ ๊ฐ๊ฒฐํ๊ฒ ๋ง๋ญ๋๋ค. ๊ทธ๋ฌ๋ ์ฅํฉํ ๊ฒฝ์ฐ ๊ฐ๋ ์ฑ์ด ๋จ์ด์ง๋๋ค.
Good:
if (actual !== expected) {
console.log('FAILED ' + testName + ': Expected ' + expected + ', but got ' + actual);
} else {
console.log('passed');
}
Bad:
return (actual === expected) ? 'passed' : 'FAILED ['+ testName + '] Expected "'+expected+'",but got '+'"'+actual+'"';
not ์ฐ์ฐ์(!)๋ ๋ฐ๋ก ์์ ๋ถ์ฌ์ ์ฌ์ฉํฉ๋๋ค.
Good:
if (!isEqual) {
Bad:
if (! isEqual) {
โถ ์งง๊ฒ ์ฐ๊ธฐ
์ฝ๋๋ ๋ป์ด ๋ถ๋ช ํ๊ณ ์คํ ๋๋ ํ, ๋๋๋ก ์งง๊ฒ ์ฐ์ธ์.
Good:
function square(n) {
return n * n;
}
Not as good:
function square(n) {
let squaredN = n * n;
return squaredN;
}
// ๋ณ์๋ฅผ ์ ์ธํ์ง ์์๋ ๋ฉ๋๋ค.
๋ถ์ ์ ์๋ฏธ๊ฐ ๋ช ํํ ๊ณณ์๋ง NOT ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํฉ๋๋ค.
Good:
if(equalSizes && equalValues) {
// ์ฌ์ด์ฆ๊ฐ ๊ฐ๊ณ , ๊ฐ์ด ๊ฐ์ ๊ฒฝ์ฐ
} else {
}
Bad:
if(!equalSizes || !equalValues) {
// ์ฌ์ด์ฆ๊ฐ ๊ฐ์ง ์๊ฑฐ๋ ๊ฐ์ด ๊ฐ์ง ์์ ๊ฒฝ์ฐ
} else {
}
// ์ด ์กฐ๊ฑด๋ฌธ์ ๋ถํ์ํ๊ฒ ์กฐ๊ฑด์ ๋ํด ๊ณ ๋ฏผ์ ํด์ผ ํฉ๋๋ค.
Boolean์ผ๋ก ํ๊ฐ๋๋ ํํ๋ฌธ์ ๋ฐ๋ก return ํ์ธ์
Good:
return charSet.size > text.length;
Bad:
if(charSet.size < text.length) {
return false;
}
return true;
// ๋น๊ต๋ฌธ์ ๋ฌด์กฐ๊ฑด true ํน์ false๋ก ํ๊ฐ๋๊ธฐ ๋๋ฌธ์ ์ฅํฉํ๊ฒ ์กฐ๊ฑด๋ฌธ์ ์์ฑํ์ง ์์ต๋๋ค.
โถ ์ฝ๋ ๋ฌธ์ฅ๊ณผ ๊ตฌ๋ฌธ ์ฌ์ด ๊ณต๊ฐ
ํ ๋ฒ์ ๋ ๋ง์ ์ฝ๋๋ฅผ ์ฝ๊ธฐ ์ํด์, ์ค ๋ฐ๊ฟ์ ์ต์๋ก ์ฌ์ฉํด์ผ ํฉ๋๋ค.
Good:
function square(n) {
return n * n;
}
function assertEqual(actual, expected, testName) {
// compare actual and expected
}
Bad:
function square(n) {
return n * n;
}
function assertEqual(actual, expected, testName) {
// compare actual and expected
}
์ค์ ๋ฐ๊ฟ์ผํ ๋๋ ์๊ณ , ์ค ๋ฐ๊ฟ์ ์์ ํด์ผ ํ ๋๋ ์์ต๋๋ค.
ํ๋จ ๊ธฐ์ค์ ์ด๋ป๊ฒ ์์ฑํด์ผ ์ดํดํ๊ธฐ ์ฌ์ด๊ฐ? ์ ๋๋ค.
๋ค์ฌ์ฐ๊ธฐ๋ ์ผ๊ด์ฑ์๊ฒ, ์ต์ํํ์ฌ ์ฌ์ฉํฉ๋๋ค.
Good:alert('I chose to put no visual padding around this string'); // ๊ดํธ์ ๋ฐ์ดํ ์ฌ์ด ๊ณต๋ฐฑ์ด ์์ต๋๋ค.Good:alert( 'I chose to put visual padding around this string' ); // ๊ดํธ์ ๋ฐ์ดํ ์ฌ์ด ๊ณต๋ฐฑ์ด ์ผ๊ด๋๊ฒ ์์ต๋๋ค.Bad:alert( 'I only put visual padding on one side of this string'); // ํ ์ชฝ์๋ ๊ณต๋ฐฑ์ด ์๊ณ , ํ ์ชฝ์๋ ๊ณต๋ฐฑ์ด ์์ต๋๋ค.
๊ฐ์ ๋ผ์ธ์ ๊ฐ์ ๋ณด๊ธฐ ์ํด ํ๋ ๋ค์ฌ์ฐ๊ธฐ๋ ์ง์ํฉ๋๋ค.
Good:
let firstItem = getFirst();
let secondItem = getSecond();
let thirteenthItem = getThirteenth();
Bad:
let firstItem = getFirst();
let secondItem = getSecond();
let thirteenthItem = getThirteenth();
// ๋ถํ์ํ ๋ค์ฌ์ฐ๊ธฐ๊ฐ ๋๋ฌด ๋ง์์ก์ต๋๋ค.
์ฝค๋ง(,) ์ฌ์ด๋ ํ ์นธ ๋์ด์๋๋ค.
Good:
assertEqual(Math.pow(3, 2), 9, 'Math.pow squares properly');
Bad:
assertEqual(Math.pow(3,2),9,'Math.pow squares properly');
์ฐ์ฐ์ ์ฌ์ด๋ ํ ์นธ ๋์ด์๋๋ค.
Good:
'Failed [' + testName + ']'...
Good:
if(actual === expected){
// action
} else {
// alternate action
}
Bad:
'Failed ['+testName+']'...
Bad:
if(actual===expected){
// action
}else{
// alternate action
}
์ฃผ์
- ์ฃผ์์ ๊ผญ ํ์ํ ๊ฒฝ์ฐ์๋ง ์์ฑํฉ๋๋ค.
- ์ฃผ์์ ์ ๊ธฐ ์ ์, ๋ณ์ ์ด๋ฆ๊ณผ ๊ตฌ์กฐ๋ก ์ฝ๋์ ๋ชฉ์ ์ด ๋ช ํํ ํํ๋๋ฉด ์ฃผ์์ ์ ์ ํ์๊ฐ ์์ต๋๋ค.
- ์ฃผ์์ผ๋ก ์ฝ๋์ ์๋์ ์ค๋ช ํ๋ ๊ฒ์ ์ง์ํฉ๋๋ค. ์ฝ๋๋ฅผ ๋ณด๊ณ ๋ฐ๋ก ์ดํดํ ์ ์์ ๋งํผ ๊ฐ๋ ์ฑ์ด ์ข๊ฒ ์ฝ๋๋ฅผ ์์ฑํด์ผ ํฉ๋๋ค.
๊ณต๋ถ ๋ชฉ์ ์ผ๋ก ์ฃผ์์ ์ ์๋๋ฐ์, ์ด๊ฒ๋ ํ๋ฉด ์๋๋์?
- ์ฃผ์์ ๊ณต๋ถ ๋ชฉ์ ์ผ๋ก ํ์ฉํ๋ ๊ฒ์ ์ข์ผ๋, ์๋ฌ๋์ด ์ดํดํ ์ ์๋ ์ฝ๋์ ์ฃผ์์ ๋ค๋ ๊ฒ์ ๊ฐ๋ ์ฑ์ ๋จ์ด๋จ๋ฆฝ๋๋ค. ์ ์ฐจ์ ์ผ๋ก ์ค์ฌ๋๊ฐ์๋ค.
โถ camelCase vs. snake_case
JavaScript์์๋ ๋ณ์์ ์ด๋ฆ์ ์ง์ ํ ๋ 'Camel Casing'์ผ๋ก ์ง์ ํฉ๋๋ค. ์ด๊ฒ์ Ruby ๊ฐ์ ๋ค๋ฅธ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์์ ์ฌ์ฉํ๋ 'Snake Casing'๊ณผ ๋ค๋ฆ ๋๋ค.
Good:
let camelCased = 'Used in javascript';
Bad:
let snake_cased = 'Used in other languages';
JavaScript์์ '๋ฑ ๋ชจ์'์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๋, ์์ ์ด๋ฆ์ ์ง์ ๋์ ๋๋ค.
const MAX_ITEMS_IN_QUEUE = 100;
'๐ง๐ปโ๐ป ๊ฐ๋ฐ๊ฐ๋ฐ > ๊ธฐํ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋งํฌ๋ค์ด(Markdown) ์์ฑ๋ฒ (0) | 2021.06.27 |
---|---|
๋งํฌ๋ค์ด(Markdown)๋ฌธ๋ฒ์ผ๋ก ์ฝ๋(Code) ์ฝ์ ๋ฒ (0) | 2021.06.26 |
Google์๋ณด๋ค ํจ์จ์ ์ผ๋ก ์ฌ์ฉํ๊ธฐ์ํ 20 ๊ฐ์ง Google ๊ฒ์ ํ (0) | 2021.06.14 |
DOM : innerHTML๊ณผ textContent์ ์ฐจ์ด (0) | 2021.06.02 |
Primitive type & Reference type (0) | 2021.05.28 |