https://github.com/airbnb/javascript
Airbnb 참고 하였습니다.
es-lint 와 prettier 만으론 잘 안되는 부분 위주로 기록하고 있습니다. - updated 2022-07-04
아직 작성중 문서 입니다.
앞으로도 지속적으로 추가 될 예정이며 건의하실 부분 있으시면 언제든 건의 해주세요.
by 김유철 코드넛 기술이사
코드 포맷팅이란 들여쓰기, 공백추가, 줄바꿈 등 주로 스페이스와 엔터를 어떻게 쓰냐의 관점에서 코드의 외형적인 부분을 말합니다.
코드 포맷터(Code Formatter)란 개발자가 작성한 코드를 정해진 코딩 스타일을 따르도록 자동으로 변환해주는 도구를 말합니다. Prettier는 이러한 코드 포맷터 중에서도 최근에 가장 인기를 많이 얻어 거의 표준이 되고 가고 있는 자바스크립트 라이브러리 입니다. 쟁쟁한 오픈 소스 프로젝트들과 수많은 기업들이 Prettier를 정식 코드 포맷터를 채택해서 사용하고 있습니다. (Facebook, React, Jest, Yarn, Babel, Webpack, Dropbox, Storybook, Paypal, MongoDB, Salesforce, …)
Prettier가 많은 개발자들에게서 급속히 사랑받게 된 이유는 기존 코드 포맷터와 달리 설정 여지가 거의 없다는 것입니다. 이 말은 일단 Prettier를 쓰기 시작하면 더 이상 코딩 스타일에 대해서 팀원 간에 왈가왈부할 여지가 없다는 것입니다.
프리티어 안쓰시고 포맷팅을 수동으로 완벽히 하실 자신이 있으시면 그렇게 하셔도 됩니다.
포맷팅과 컨벤션은 조금 다르니 컨벤션 부분은 다음과 같이 기본적인 부분부터 아래에 기술 되어 있습니다.
꼭 쓰시기전 아래 설정파일을 적용하여 주십시요.
{
"singleQuote": true,
"trailingComma": "all",
"semi": false,
"printWidth": 100
}
vscode 프로젝트 루트디렉토리에 위 설정파일을 꼭 셋팅 해주셔야
세미콜론 및 트레일링 컴마 싱글쿼트가 적용 됩니다!
// bad
var a = 1
var b = 2
// good
const a = 1
const b = 2
// bad
const item = {};
// good
const item = {}
const lukeSkywalker = 'Luke Skywalker'
// bad
const obj = {
lukeSkywalker: lukeSkywalker,
}
// good
const obj = {
lukeSkywalker,
}
// bad
const bad = {
'foo': 3,
'bar': 4,
'data-blah': 5,
}
// good
const good = {
foo: 3,
bar: 4,
'data-blah': 5,
}
// bad
const atom = {
value: 1,
addValue: function (value) {
return atom.value + value
},
}
// good
const atom = {
value: 1,
addValue(value) {
return atom.value + value
},
}
// bad
const len = items.length
const itemsCopy = []
let i;
for (i = 0; i < len; i += 1) {
itemsCopy[i] = items[i]
}
// good
const itemsCopy = [...items]
// bad
function foo() {
const self = this
return function () {
console.log(self)
}
}
// good
function bar() {
return () => {
console.log(this)
}
}
// bad
import SmsContainer from './containers/SmsContainer';
// bad
const HttpRequests = [
// ...
]
// good
import SMSContainer from './containers/SMSContainer';
// good
const HTTPRequests = [
// ...
]
// also good
const httpRequests = [
// ...
]
// best
import TextMessageContainer from './containers/TextMessageContainer';
// best
const requests = [
// ...
]
// bad
const errorMessage = 'This is a super long error that was thrown because \
of Batman. When you stop to think about how Batman had anything to do \
with this, you would get nowhere \
fast.'
// bad
const errorMessage = 'This is a super long error that was thrown because ' +
'of Batman. When you stop to think about how Batman had anything to do ' +
'with this, you would get nowhere fast.'
// good
const errorMessage = 'This is a super long error that was thrown because of Batman. When you stop to think about how Batman had anything to do with this, you would get nowhere fast.'
// bad
function sayHi(name) {
return 'How are you, ' + name + '?'
}
// bad
function sayHi(name) {
return ['How are you, ', name, '?'].join()
}
// bad
function sayHi(name) {
return `How are you, ${ name }?`
}
// good
function sayHi(name) {
return `How are you, ${name}?`
}
// bad
const foo = a ? a : b;
const bar = c ? true : false;
const baz = c ? false : true;
// good
const foo = a || b;
const bar = !!c;
const baz = !c;
// bad
LonDon.png
HELLOWORLD.pdf
APP.js
// good
london.png
helloworld.pdf
app.js
패키지의 이름은 lowerCamelCase로 표기한다.
파일의 이름은 default export의 이름과 일치해야한다.
// bad
let 123Number = 123;
let HELLO_WORLD = "Hello World";
// good
let number = 369;
let helloString = "Hello World";
// bad
function MyFunction() {...}
// good
function myFunction() {...}
// bad
function whereIsCamera() { ... }
// good
function findCamera() { ... }
function getFoo() { ... } // getter
function setBar() { ... } // setter
function hasCoo() { ... } // booleans
function makeStyleGuide() {
// ...
}
export default makeStyleGuide;
// bad
function someFunction(SOMEVALUE, SOMEARRAY) { ... }
// good
function someFunction(someValue, someArray) { ... }
// bad
const OBJEcttsssss = {}
const this_is_my_object = {}
function c() {}
// good
const thisIsMyObject = {}
function thisIsMyFunction() {}
const AirbnbStyleGuide = {
es6: {
},
}
export default AirbnbStyleGuide
// bad
function user(options) {
this.name = options.name
}
const bad = new user({
name: 'nope',
})
// good
class User {
constructor(options) {
this.name = options.name
}
}
const good = new User({
name: 'yup',
})
클래스를 export할 때는 PascalCase로 표기한다.
- 카멜식 (Camel case)
- 낙타 모양에서 따온 방법으로 첫글자는 소문자로 시작, 두번쨰 단어부터는 대문자로 표현하는 방식으로 많이 사용합니다.
카멜식 : userLoginLog
- 파스칼식 (Pascal case)
- 카멜식이랑 비슷하지만 차이점은 첫단어를 대문자로 시작합니다.
파스칼식 : UserLoginLog
- 케밥식 (Kebab case)
- 먹는 케밥에 꼬챙이를 낀 모습이며, 모두 소문자로 표현하며, 단어와 단어 사이에는 하이픈(-)를 사용합니다.
케밥식: user-login-log
- 스네이크식(Snake case)
- 케밥식은 하이픈(-)를 사용했다면 스네이크식은 언더바(_)를 사용합니다.
스네이크식 : user_login_log 또는 USER_LOGIN_LOG
사용방법은 간단하니 각 환경에 맞는 네이밍을 사용하시면 됩니다.