Skip to content

Latest commit

ย 

History

History
202 lines (176 loc) ยท 5.19 KB

coding-convention.md

File metadata and controls

202 lines (176 loc) ยท 5.19 KB

๐ŸŒฑ 9์กฐ See-At ์ฝ”๋”ฉ ์ปจ๋ฒค์…˜

์•„๋ž˜๋Š” TOAST UI ์ฝ”๋”ฉ ์ปจ๋ฒค์…˜์—์„œ ๊ธฐ๋ณธ์ ์ธ ๋ถ€๋ถ„์„ ์š”์•ฝํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๊ฐœ๋ฐœ ๋„์ค‘ ๋ชจํ˜ธํ•˜๊ฑฐ๋‚˜ ์•„๋ž˜์— ์—†๋Š” ๋ถ€๋ถ„์ด ์ƒ๊ธธ ์‹œ ์›๋ฌธ ๋‚ด์šฉ์„ ๋”ฐ๋ฆ…๋‹ˆ๋‹ค.

1. ๋ฌธ์žฅ์˜ ์ข…๋ฃŒ

  • ํ•œ ์ค„์— ํ•˜๋‚˜์˜ ๋ฌธ์žฅ๋งŒ ํ—ˆ์šฉ
  • ๋ฌธ์žฅ ์ข…๋ฃŒ ์‹œ ; ์‚ฌ์šฉ

2. ๋ช…๋ช… ๊ทœ์น™

๊ธฐ๋ณธ์ ์œผ๋กœ ์นด๋ฉœ ์ผ€์ด์Šค ์‚ฌ์šฉ

  • ์ƒ์ˆ˜ : ์˜๋ฌธ ๋Œ€๋ฌธ์ž ์Šค๋„ค์ดํฌ ํ‘œ๊ธฐ๋ฒ• ์‚ฌ์šฉ ex) SYMBOLIC_CONSTANTS
  • ์ƒ์„ฑ์ž : ๋Œ€๋ฌธ์ž ์นด๋ฉœ ์ผ€์ด์Šค ์‚ฌ์šฉ ex) class ConstructorName
  • ๋ณ€์ˆ˜, ํ•จ์ˆ˜ : ์นด๋ฉœ ์ผ€์ด์Šค ์‚ฌ์šฉ
let variableName;

// ๋ฐฐ์—ด - ๋ณต์ˆ˜ํ˜• ์ด๋ฆ„
const dogs = [];

// ์ •๊ทœ ํ‘œํ˜„์‹ - 'r'๋กœ ์‹œ์ž‘
const rDesc = /.*/;

function getPropertyName () {
    ...
}

// ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ - 'on'์œผ๋กœ ์‹œ์ž‘
const onClick = () => {};

// ๋ถˆ๋ฆฐ ๋ฐ˜ํ™˜ ํ•จ์ˆ˜ - 'is'๋กœ ์‹œ์ž‘
let isAbailable = false;
  • ๋ฒ”์šฉ์ ์ธ ๋Œ€๋ฌธ์ž ์•ฝ์–ด : URL, HTML ๋“ฑ์€ ๋Œ€๋ฌธ์ž ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉ ex) parseHTML

3. ์„ ์–ธ๊ณผ ํ• ๋‹น

3.1 ๋ณ€์ˆ˜

  1. ๋ณ€์ˆ˜ ์„ ์–ธ ๋ฐฉ์‹
    • var๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š๋„๋ก ํ•จ
    • const : ๊ฐ’์ด ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๋ณ€์ˆ˜
    • let : ๊ฐ’์ด ๋ณ€ํ•˜๋Š” ๋ณ€์ˆ˜
  2. ๋ณ€์ˆ˜ ์„ ์–ธ ์‹œ์ 
    • const๋ฅผ let๋ณด๋‹ค ์œ„์— ์„ ์–ธ
    • const์™€ let์€ ์‚ฌ์šฉ ์‹œ์ ์— ์„ ์–ธ ๋ฐ ํ• ๋‹น

3.2 ๋ชจ๋“ˆ

  • ์™ธ๋ถ€ ๋ชจ๋“ˆ๊ณผ ๋‚ด๋ถ€ ๋ชจ๋“ˆ์„ ๊ณต๋ฐฑ์„ ๋‘์–ด ๊ตฌ๋ถ„ํ•ด์„œ ์‚ฌ์šฉ -> ๊ฐ€๋…์„ฑ ํ–ฅ์ƒ
// ์™ธ๋ถ€ ๋ชจ๋“ˆ
const lodash = require('lodash');
const $ = require(jquery);
const handlebars = require('handlebars');
const d3 = require('d3');

// ๋‚ด๋ถ€ ๋ชจ๋“ˆ
const pluginFactory from '../../factories/pluginFactory';
const predicate from '../../helpers/predicate';
const raphaelRenderUtil from '../../plugins/raphaelRenderUtil';

4. ๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด

๋ฐฐ์—ด๊ณผ ๊ฐ์ฒด๋Š” ๋ฆฌํ„ฐ๋Ÿด๋กœ ์„ ์–ธ

// Bad
const emptyArr = new Array();
const arr = new Array(1, 2, 3, 4, 5);
// Good
const emptyArr = [];
const arr = [1, 2, 3, 4, 5];

// Bad
const emptyObj = new Object();
const obj = new Object();
// Good
const emptyObj = {};
const obj = {
  pro1: 'val1', 
  pro2: 'val2'
};

4.1 ๋ฐฐ์—ด

  • ๋ฐฐ์—ด ๋ณต์‚ฌ ์‹œ ์ˆœํ™˜๋ฌธ์ด ์•„๋‹Œ ์ „๊ฐœ ์—ฐ์‚ฐ์ž ์‚ฌ์šฉ
// ๋ฐฐ์—ด itemsCopy์— items๋ฅผ ๋ณต์‚ฌํ•  ๊ฒฝ์šฐ

// Bad
for (i = 0; i < len; i++) {
  itemsCopy[i] = items[i];
}
// Good
const itemsCopy = [...items];
  • ๋ฐฐ์—ด์˜ ๊ด„ํ˜ธ, ์š”์†Œ ์ค‘ ํ•˜๋‚˜๋ผ๋„ ์ค„ ๋ฐ”๊ฟˆ์ด ์žˆ๋‹ค๋ฉด ๋™์ผํ•˜๊ฒŒ ๋ชจ๋‘ ์ค„๋ฐ”๊ฟˆ ์ ์šฉ

4.2 ๊ฐ์ฒด

  • ๊ฐ์ฒด ํ”„๋กœํผํ‹ฐ๊ฐ€ 1๊ฐœ์ผ ๊ฒฝ์šฐ ํ•œ ์ค„ ์ •์˜ ๊ฐ€๋Šฅ, 2๊ฐœ ์ด์ƒ์ผ ๊ฒฝ์šฐ ๊ฐœํ–‰ ๊ฐ•์ œ
// Bad 
const obj = {foo: 'a', bar: 'b'}

// Good
const obj = {foo: 'a'};
const obj = {
  foo: 'a'
};
  • ๋ฉ”์„œ๋“œ ๋ฌธ๋ฒ• ์‚ฌ์šฉ ์‹œ ๋ฉ”์„œ๋“œ ์‚ฌ์ด์— ๊ฐœํ–‰ ์ถ”๊ฐ€
class MyClass {
  foo() {
    //...
  }

  bar() {
    //...
  }
}

5. ํ•จ์ˆ˜

  • ํ•จ์ˆ˜๋Š” ์‚ฌ์šฉ ์ „์— ์„ ์–ธํ•˜๊ณ  ํ•จ์ˆ˜ ์„ ์–ธ๋ฌธ์€ ๋ณ€์ˆ˜ ์„ ์–ธ๋ฌธ ๋‹ค์Œ์— ์˜ค๋„๋ก ํ•จ
  • ํ•จ์ˆ˜๋Š” ์ƒ์„ฑ์ž๊ฐ€ ์•„๋‹Œ ์„ ์–ธ์‹, ํ‘œํ˜„์‹์œผ๋กœ ์„ ์–ธ
// Bad - ํ•จ์ˆ˜ ์ƒ์„ฑ์ž ์‚ฌ์šฉ
const doSomething = new Function('param1', 'param2', 'return param1 + param2;');

// Good - ํ•จ์ˆ˜ ์„ ์–ธ์‹ ์‚ฌ์šฉ
function doSomething(param1, param2) {
  return param1 + param2;
}
// Good - ํ•จ์ˆ˜ ํ‘œํ˜„์‹ ์‚ฌ์šฉ
const doSomething = function(param1, param2) {
  return param1 + param2;
};

5.1 ํ™”์‚ดํ‘œ ํ•จ์ˆ˜

  • ํ•จ์ˆ˜ ํ‘œํ˜„์‹ ๋Œ€์‹  ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ์‚ฌ์šฉ
// Bad - ํ•จ์ˆ˜ ํ‘œํ˜„์‹
[1, 2, 3].map(function (x) {
  const y = x + 1;
  return x * y;
});

// Good - ํ™”์‚ดํ‘œ ํ•จ์ˆ˜
[1, 2, 3].map(x => {
  const y = x + 1;
  return x * y;
});
  • ์•”์‹œ์  ๋ฐ˜ํ™˜ ์ตœ๋Œ€ํ•œ ํ™œ์šฉ -> ํ•จ์ˆ˜์˜ ๋ณธ์ฒด๊ฐ€ ํ•˜๋‚˜์˜ ํ‘œํ˜„์‹์ผ ๋•Œ ์ค‘๊ด„ํ˜ธ ์ƒ๋žต
// Bad
[1, 2, 3].map(number => {
  const nextNumber = number + 1;
  `A string containing the ${nextNumber}.`;
});

// Good - ์•”์‹œ์  return ์‚ฌ์šฉ -> ์•”์‹œ์  ๋ฐ˜ํ™˜ ์‹œ ํ•จ์ˆ˜ ๋ณธ๋ฌธ ์ „ ๊ฐœํ–‰์„ ํ•˜์ง€ ์•Š์Œ
[1, 2, 3].map(number => `A string containing the ${number + 1}.`);

6. ํ…œํ”Œ๋ฆฟ ๋ฌธ์ž์—ด

  • ๋ณ€์ˆ˜ ๋“ฑ์„ ์กฐํ•ฉํ•ด์„œ ๋ฌธ์ž์—ด ์ƒ์„ฑ ์‹œ ํ…œํ”Œ๋ฆฟ ๋ฌธ์ž์—ด ์ด์šฉ -> ๋ฌธ์ž์—ด์„ ์‰ฝ๊ณ  ๋ช…ํ™•ํ•˜๊ฒŒ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•จ
function sayHi(name) {
  return `How are you, ${name}?`;
}

7. ๋ธ”๋ก ๊ตฌ๋ฌธ

  • ํ•œ ์ค„์งœ๋ฆฌ ๋ธ”๋ก์ผ ๊ฒฝ์šฐ๋ผ๋„ {}๋ฅผ ์ƒ๋žตํ•˜์ง€ ์•Š๊ณ , ์ค„๋ฐ”๊ฟˆ ์ถ”์ฒœ
  • ํ‚ค์›Œ๋“œ์™€ ์กฐ๊ฑด๋ฌธ ์‚ฌ์ด ๋นˆ์นธ ์‚ฌ์šฉ ex) a < 1
  • switch-case ์‚ฌ์šฉ ์‹œ ์ฒซ ๋ฒˆ์งธ case๋ฌธ์„ ์ œ์™ธํ•œ case๋ฌธ ์‚ฌ์šฉ ์ด์ „์— ๊ฐœํ–‰
switch (value) {
  case 1:
    doSomething1();
    break;

  case 2:
    doSomething2();
    break;

  default:
    throw new Error('This shouldn\'t happen.');
}

8. ์ฃผ์„

  • ์ฃผ์„ ์‚ฌ์šฉ ์‹œ ์ฝ”๋“œ์™€ ๋งž์ถฐ์„œ ๋“ค์—ฌ์“ฐ๊ธฐ ํ•จ
  • ์—ฌ๋Ÿฌ ์ค„ ์ฃผ์„ ์‚ฌ์šฉ ์‹œ *์˜ ๋“ค์—ฌ์“ฐ๊ธฐ๋ฅผ ๋งž์ถค

9. ๊ณต๋ฐฑ

  • ํ‚ค์›Œ๋“œ, ์—ฐ์‚ฐ์ž์™€ ๋‹ค๋ฅธ ์ฝ”๋“œ ์‚ฌ์ด์— ๊ณต๋ฐฑ์ด ์žˆ์–ด์•ผ ํ•จ ex) if (typeof str === 'string')
  • ์ฝค๋งˆ ๋‹ค์Œ์— ๊ฐ’์ด ์˜ฌ ๊ฒฝ์šฐ ๊ณต๋ฐฑ์ด ์žˆ์–ด์•ผ ํ•จ
// Bad - ์ฝค๋งˆ ๋’ค ๊ณต๋ฐฑ
const arr = [1,2,3,4];

// Good
const arr = [1, 2, 3, 4];

10. ๊ทธ ์™ธ

  • ์กฐ๊ฑด ํ™•์ธ ์‹œ ์‚ผ์ค‘ ๋“ฑํ˜ธ ์—ฐ์‚ฐ์ž ===, !== ์‚ฌ์šฉ