Goodbye, Clean Code

댄은 늦은 저녁 동료의 중복이 많고 반복되는 아래의 코드를 보았고

let Rectangle = {
  resizeTopLeft(position, size, preserveAspect, dx, dy) {
    // 10 repetitive lines of math
  },
  resizeTopRight(position, size, preserveAspect, dx, dy) {
    // 10 repetitive lines of math
  },
  resizeBottomLeft(position, size, preserveAspect, dx, dy) {
    // 10 repetitive lines of math
  },
  resizeBottomRight(position, size, preserveAspect, dx, dy) {
    // 10 repetitive lines of math
  },
};

let Oval = {
  resizeLeft(position, size, preserveAspect, dx, dy) {
    // 10 repetitive lines of math
  },
  resizeRight(position, size, preserveAspect, dx, dy) {
    // 10 repetitive lines of math
  },
  resizeTop(position, size, preserveAspect, dx, dy) {
    // 10 repetitive lines of math
  },
  resizeBottom(position, size, preserveAspect, dx, dy) {
    // 10 repetitive lines of math
  },
};

let Header = {
  resizeLeft(position, size, preserveAspect, dx, dy) {
    // 10 repetitive lines of math
  },
  resizeRight(position, size, preserveAspect, dx, dy) {
    // 10 repetitive lines of math
  },  
}

let TextBlock = {
  resizeTopLeft(position, size, preserveAspect, dx, dy) {
    // 10 repetitive lines of math
  },
  resizeTopRight(position, size, preserveAspect, dx, dy) {
    // 10 repetitive lines of math
  },
  resizeBottomLeft(position, size, preserveAspect, dx, dy) {
    // 10 repetitive lines of math
  },
  resizeBottomRight(position, size, preserveAspect, dx, dy) {
    // 10 repetitive lines of math
  },
};

대략 다음과 같이 수정하였다.

let Directions = {
  top(...) {
    // 5 unique lines of math
  },
  left(...) {
    // 5 unique lines of math
  },
  bottom(...) {
    // 5 unique lines of math
  },
  right(...) {
    // 5 unique lines of math
  },
};

let Shapes = {
  Oval(...) {
    // 5 unique lines of math
  },
  Rectangle(...) {
    // 5 unique lines of math
  },
}

코드 량을 반으로 줄였고 중복을 줄였으며 깔끔해 졌다. 수정 또한 한 군데서 하면 된다.

댄은 동료의 지저분한 코드를 정리한 것에 대해 자랑스러웠고 늦은 밤 이었기 때문에 곧 잠이 들었다.

다음 날

보스가 댄을 1:1 챗으로 초대하여 정중하게 (politely) 댄의 변경 사항을 revert 해줄 수 있냐 물었다.

그리고 댄은 경악했지만 그렇게 했다. 그리고 그들이 옳았다는 것을 알게 되기까지 몇 년이 걸렸다.

It's a Phase

클린 코드에 집착하고 중복을 제거 하는 것은 많은 사람들이 겪는 단계이다.

우리가 우리의 코드에 대한 자신감을 느끼지 못할 때, 측정될 수 있는 어떤 것에 우리의 자긍심과 전문적인 자부심을 부여하는 것은 우리에게 유혹 적으로 느껴진다.

엄격한 린트 규칙, 네이밍 컨벤션, 파일 구조, 중복 제거