Encapsulate Variable - Refactoring skills(6)

Kerem Song·2021년 3월 23일
0

Refactoring - Study

목록 보기
8/22

6. Encapsulate Variable(변수 캡슐화하기)

Encapsulate a reference to some data structure
(데이터 구조를 캡슐화)

let defaultOwner = {firstName: "Martin", lastName: "Fowler"}

to

// defaultOwner.js
let defaultOwnerData = {firstName: "Martin", lastName: "Fowler"}
export function defaultOwner() { return defaultOwnerData }
export function setDefaultOwner(arg) { defaultOwnerData = arg }

Motivation

  1. Provide a clear point to monitor changes and use of the data, like validation.
    데이터의 사용이나 변화를 모니터링하는데 있어서 확실한 포인트를 제공

Procedure

  1. Make encapsulate functions that are responsiboe for accessing and renewal to variables
    변수로의 접근과 갱신을 전담하는 캡슐화 함수들을 만든다.

  2. Do static analysis(with ESLint, Prettier)
    정적 검사를 수행한다.

  3. Replace all parts of the variable that were referenced directly with the appropriate encapsulation function call. Test each time you change one by one.
    변수를 직접 참조하던 부분을 모두 적절한 캡슐화 함수 호출로 바꾼다. 하나씩 바꿀 때마다 테스트한다.

  4. Destrict the ranges of variables
    변수의 접근 범위를 제한한다.

  5. Test it.
    테스트한다.

  6. If value of variables is record, Think about record encapsulation
    변수 값이 레코드라면 레코드 캡슐화하기를 적용할지 고려해본다.

profile
Tea and Dessert

0개의 댓글