refactoring

hyuk(정윤혁)·2021년 4월 5일

리팩토링(refactoring)

동작하는 것은 그대로 두고 코드 자체를 효율적으로 만들어서
코드의 가독성을 높이고 유지보수를 하기 편리하게 만들고 중복코드를
낮추며 개선하는 것을 리펙토링이라고 함

<input type="button" value="다크모드" onclick="
document.querySelector('body').style.backgroundColor='black';
document.querySelector('body').style.color='white';">
<input type="button" value="일반모드" onclick="
document.querySelector('body').style.backgroundColor='white';
document.querySelector('body').style.color='black';">

위 예시에서 document.querySelector('body')var target을 사용해 리팩토링 했다.

<input id="다크모드on_다크모드off" type="button" value="일반" onclick="
    var target = document.querySelector('body');
    if(this.value === '다크모드on'){
        target.style.backgroundColor='black';
        target.style.color='white';
        this.value='다크모드off';
    } else {
        target.style.backgroundColor='white';
        target.style.color='black';
        this.value='다크모드on';
    }
    ">

이처럼 리팩토링은 코드를 효율적으로 하며 나중에 문제가 생겼을 때 하나하나 바꾸지 않고 최소한의 수정을 통해 코드를 관리할 수 있는 이점이 있기 때문에 주기적으로 리팩토링하는 것이 중요할 것이다.

profile
노션 저장소는 🏠홈버튼 눌러주세요 !

0개의 댓글