20190422 TIL

summary : async todo app 구현, 코드 리뷰

DONE

async 활용해서 todo app 만들기

  • 함수를 작게 나눠서 프로그래밍 -> 객체 간 책임과 역할 나눠서 구현
  • Util 객체를 따로 만들어 계속 활용되는 function은 미니 라이브러리 개념으로 구성
  • foreach/map/filter/reduce 메서드 적극 사용
  • async 구현 시 Promise 객체를 반환하는 delay fuction 만들어 재사용

REVIEW

  • 함수 이름에서 구체적인 구현 방식을 적어줄 필요는 없다. 추상화시켜 적자.

// 리뷰 전
const getCommandArrayByRegexp = (command) => {...}

// 리뷰 후 : 추상화시켜 네이밍 적용
const getCmdList = (command) => {...}

  • 늘 원하는 값이 오지 않는다. 예외를 늘 신경쓰자.

// 리뷰 전
const executeCommand = (commandArr) => {...}

// 리뷰 후 : arguments 개수 판별
const executeCommand = (commandArr) => {
    if (arguments.length != 1) return;    
}

  • 함수는 재사용성이 전제되어야 한다. 상수 대신 인자를 받아 재사용성을 높이자.
// 리뷰 전
const idGenerator = () => Math.floor(Math.random() * (99999)) + 1;	

// 리뷰 후
const idGenerator = (max, min) => Math.floor(Math.random() * (max - min)) + 1;

  • 용도가 한정되어 있더라도 전역 변수에 접근하지 말고, 인자로 받아 컨트롤할 수 있도록 하자

// 리뷰 전
const isValidCommand = (command) => {	
    let result = false;	    
    if (Object.keys(todos).includes(command)) result = true;
    return result;	
}

// 리뷰 후
const isValidCommand = (command, obj) => {
    let result = false;
    if (Object.keys(obj).includes(command)) result = true;
    return result;
}

 Date: 
 Tags:  til