0%

Formatting

Formatting

포맷팅은 주관적이다.

포맷팅 체크를 해주는 자동화 도구 가 많이 있기 때문에, 공통적으로 하나를 골라 쓰는 것이 합리적이다. 자동으로 교정되는 서식 외의 것들에 대해서는 다음과 같은 지침을 따르는 것이 좋으나, 무엇보다 일관성 있게 적용하는 것이 중요하다.


  1. 일관된 대소문자를 사용할 것

    • 특히 JS에서는 타입을 명시하지 않기 때문에 변수와 함수의 대소문자를 통해 특성을 파악할 수 있다.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    const DAYS_IN_WEEK = 7;
    // const daysInMonth = 30;
    const DAYS_IN_MONTH = 30;

    /*
    const songs = ['Back In Black', 'Stairway to Heaven', 'Hey Jude'];
    const Artists = ['ACDC', 'Led Zeppelin', 'The Beatles'];
    */
    const SONGS = ["Back In Black", "Stairway to Heaven", "Hey Jude"];
    const ARTISTS = ["ACDC", "Led Zeppelin", "The Beatles"];

    function eraseDatabase() {}
    // function restore_database() {}
    function restoreDatabase() {}

    // class animal {}
    class Animal {}
    class Alpaca {}
  2. 함수 callers와 함수 callees는 가깝게 위치시키자

    • 우리는 코드를 위에서부터 아래로 순차적으로 읽어나가기 때문에, caller와 callee를 수직적으로 가깝게 위치시키는 것이 좋다.
    • 즉, caller 밑에 callee를 놓자.
    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
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    // bad
    class PerformanceReview {
    constructor(employee) {
    this.employee = employee;
    }

    lookupPeers() {
    return db.lookup(this.employee, "peers");
    }

    lookupManager() {
    return db.lookup(this.employee, "manager");
    }

    getPeerReviews() {
    const peers = this.lookupPeers();
    // ...
    }

    perfReview() {
    this.getPeerReviews();
    this.getManagerReview();
    this.getSelfReview();
    }

    getManagerReview() {
    const manager = this.lookupManager();
    }

    getSelfReview() {
    // ...
    }
    }

    const review = new PerformanceReview(employee);
    review.perfReview();

    // good
    class PerformanceReview {
    constructor(employee) {
    this.employee = employee;
    }

    perfReview() {
    this.getPeerReviews(); // 1
    this.getManagerReview(); // 2
    this.getSelfReview(); // 3
    }

    // 1
    getPeerReviews() {
    const peers = this.lookupPeers(); // 1-1
    // ...
    }

    // 1-1
    lookupPeers() {
    return db.lookup(this.employee, "peers");
    }

    // 2
    getManagerReview() {
    const manager = this.lookupManager(); // 2-1
    }

    // 2-1
    lookupManager() {
    return db.lookup(this.employee, "manager");
    }

    // 3
    getSelfReview() {
    // ...
    }
    }

    const review = new PerformanceReview(employee);
    review.perfReview();