1. 이해하기 힘든 이름(Mysterious Name)
깔끔한 코드에서 가장 중요한 것 중 하나가 바로 좋은 이름이다.
함수, 변수, 클래스, 모듈의 이름 등 모두 어떤 역할을 하는지 어떻게
쓰이는지 직관적이어야 한다.
사용할 수 있는 리팩토링 기술
2. 함수 선언 변경하기(Change Function Declaration)
2-1) 함수 변경하기-1
함수 이름 변경하기, 메소드 이름 변경하기, 매개변수 추가하기,
매개변수 제거하기, 시그니처 변경하기
2-2) 함수 변경하기-2
좋은 이름을 가진 함수는 함수가 어떻게 구현되었는지 코드를 보지 않아도 이름만
보고도 이해할 수 있다.
좋은 이름을 찾아내는 방법은?
함수 매개변수
2-3) 함수 변경하기 리팩토링
2-3)-1. 리팩토링 전
코드
public class StudyDashboard {
private Set<String> usernames = new HashSet<>(); // 리뷰어
private Set<String> reviews = new HashSet<>(); // 리뷰
// '스터디 리뷰 이슈'에 작성되어 있는'리뷰어'목록과'리뷰'를 읽어온다.
private void studyReviews(GHIssue issue) throws IOException {
List<GHIssueComment> comments = issue.getComments();
for (GHIssueComment comment : comments) {
usernames.add(comment.getUserName());
reviews.add(comment.getBody());
}
}
public Set<String> getUsernames() {return usernames;}
public Set<String> getReviews() {return reviews;}
public static void main(String[] args) throws IOException {
GitHub gitHub = GitHub.connect();
GHRepository repository = gitHub
.getRepository("whiteship/live-study");
GHIssue issue = repository.getIssue(30);
StudyDashboard studyDashboard = new StudyDashboard();
studyDashboard.studyReviews(issue);
studyDashboard.getUsernames().forEach(System.out::println);
studyDashboard.getReviews().forEach(System.out::println);
}
}
이미지
studyReviews라는 메소드명을 대신 직관적인 이름으로 변경할 거임.
studyReviews 메소드의 역할은 ‘리뷰어’와 ‘리뷰'를 읽어오는 역할을 하기 때문에
studyReviews라는 이름은 모호하다.
2-3)-2. 리팩토링 후
코드
public class StudyDashboard {
private Set<String> usernames = new HashSet<>(); // 리뷰어
private Set<String> reviews = new HashSet<>(); // 리뷰
// 1) studyReviews의 이름 변경과 2) 메소드 파라미터 값 없애기
private void loadReviews() throws IOException {
// main메소드에 있는 부분을 loadReviews메소드로 옮겨와
// loadReviews(GHIssue issue)에서 loadReviews()로 변경해보기
GitHub gitHub = GitHub.connect();
GHRepository repository = gitHub
.getRepository("whiteship/live-study");
GHIssue issue = repository.getIssue(30);
List<GHIssueComment> comments = issue.getComments();
for (GHIssueComment comment : comments) {
usernames.add(comment.getUserName());
reviews.add(comment.getBody());
}
}
public Set<String> getUsernames() {return usernames;}
public Set<String> getReviews() {return reviews;}
public static void main(String[] args) throws IOException {
// 2) 메소드의 파라미터 값 없애기
// GitHub gitHub = GitHub.connect();
// GHRepository repository = gitHub
//.getRepository("whiteship/live-study");
// GHIssue issue = repository.getIssue(30);
StudyDashboard studyDashboard = new StudyDashboard();
// studyDashboard.studyReviews(issue);
studyDashboard.loadReviews();
studyDashboard.getUsernames().forEach(System.out::println);
studyDashboard.getReviews().forEach(System.out::println);
}
}
3. 변수 이름 바꾸기(Rename Variable)
3-1) 개념 잡기
3-2) 리팩토링 전
코드
public class StudyDashboard {
private Set<String> usernames = new HashSet<>(); // 리뷰어
private Set<String> reviews = new HashSet<>(); // 리뷰
private void loadReviews() throws IOException {
GitHub gitHub = GitHub.connect();
GHRepository repository = gitHub
.getRepository("whiteship/live-study");
GHIssue issue = repository.getIssue(30);
// 지역변수(바꿀 필요 있음)
List<GHIssueComment> comments = issue.getComments();
for (GHIssueComment comment : comments) {
usernames.add(comment.getUserName());
reviews.add(comment.getBody());
}
}
public Set<String> getUsernames() {return usernames;}
public Set<String> getReviews() {return reviews;}
public static void main(String[] args) throws IOException {
// 지역 변수(OK)
StudyDashboard studyDashboard = new StudyDashboard();
studyDashboard.loadReviews();
// 람다식에서 사용하는 변수(인텔리제이에서 추천하는 메소드 레퍼런스로 바꾸기)
studyDashboard.getUsernames()
.forEach(name -> System.out.println(name));
studyDashboard.getReviews()
.forEach(review -> System.out.println(review));
}
}
이미지
3-3) 리팩토링 후
public class StudyDashboard {
private Set<String> usernames = new HashSet<>();
private Set<String> reviews = new HashSet<>();
private void loadReviews() throws IOException {
GitHub gitHub = GitHub.connect();
GHRepository repository = gitHub
.getRepository("whiteship/live-study");
GHIssue issue = repository.getIssue(30);
// 지역 변수 : Local Variable(comments와 comment를 reviews와 review)
List<GHIssueComment> reviews = issue.getComments();
for (GHIssueComment review : reviews) {
usernames.add(review.getUserName());
reviews.add(review.getBody());
}
}
public Set<String> getUsernames() {return usernames;}
public Set<String> getReviews() {return reviews;}
public static void main(String[] args) throws IOException {
// 지역 변수 : Local Variable(OK)
StudyDashboard studyDashboard = new StudyDashboard();
studyDashboard.loadReviews();
// 람다식에서 사용하는 변수(인텔리제이 레퍼런스 함수)
studyDashboard.getUsernames().forEach(System.out::println);
studyDashboard.getReviews().forEach(System.out::println);
}
}
4. 필드 이름 바꾸기(Rename Field == Global Variable)
4-1) 개념
4-2) 리팩토링 전
코드
public class StudyDashboard {
// 전역변수(Global)
private Set<String> usernames = new HashSet<>(); // 리뷰어
private Set<String> reviews = new HashSet<>(); // 리뷰
private void loadReviews() throws IOException {
GitHub gitHub = GitHub.connect();
GHRepository repository = gitHub
.getRepository("whiteship/live-study");
GHIssue issue = repository.getIssue(30);
List<GHIssueComment> reviews = issue.getComments();
for (GHIssueComment review : reviews) {
usernames.add(review.getUserName());
reviews.add(review.getBody());
}
}
public Set<String> getUsernames() {return usernames;}
public Set<String> getReviews() {return reviews;}
public static void main(String[] args) throws IOException {
StudyDashboard studyDashboard = new StudyDashboard();
studyDashboard.loadReviews();
studyDashboard.getUsernames().forEach(System.out::println);
studyDashboard.getReviews().forEach(System.out::println);
}
}
이미지
4-3) 리팩토링 후
public class StudyDashboard {
// 전역변수(Global)
private Set<String> reviewers = new HashSet<>(); // 리뷰어
private Set<String> reviews = new HashSet<>(); // 리뷰
private void loadReviews() throws IOException {
GitHub gitHub = GitHub.connect();
GHRepository repository = gitHub.getRepository("whiteship/live-study");
GHIssue issue = repository.getIssue(30);
List<GHIssueComment> reviews = issue.getComments();
for (GHIssueComment review : reviews) {
// 전역변수 수정 후
reviewers.add(review.getUserName());
reviews.add(review.getBody());
}
}
// 전역 변수 수정 후
public Set<String> getReviewers() {return reviewers;}
public Set<String> getReviews() {return reviews;}
public static void main(String[] args) throws IOException {
StudyDashboard studyDashboard = new StudyDashboard();
studyDashboard.loadReviews();
studyDashboard.getReviewers().forEach(name -> System.out.println(name));
studyDashboard.getReviewers().forEach(System.out::println);
studyDashboard.getReviews().forEach(System.out::println);
}
}