1. 중복코드(Duplicated Code)
2. 함수 추출하기(Extract Function)
2-1) 개념
2-2) 리팩토링 전
코드
public class StudyDashboard {
private void printParticipants(int eventId) throws IOException {
// Get github issue to check homework
GitHub gitHub = GitHub.connect();
GHRepository repository = gitHub
.getRepository("whiteship/live-study");
GHIssue issue = repository.getIssue(eventId);
// Get participants
Set<String> participants = new HashSet<>();
issue.getComments().forEach(c -> participants
.add(c.getUserName()));
// Print participants
participants.forEach(System.out::println);
}
public static void main(String[] args) throws IOException {
StudyDashboard studyDashboard = new StudyDashboard();
studyDashboard.printReviewers();
studyDashboard.printParticipants(15);
}
private void printReviewers() throws IOException {
// Get github issue to check homework
GitHub gitHub = GitHub.connect();
GHRepository repository = gitHub
.getRepository("whiteship/live-study");
GHIssue issue = repository.getIssue(30);
// Get reviewers
Set<String> reviewers = new HashSet<>();
issue.getComments().forEach(c -> reviewers
.add(c.getUserName()));
// Print reviewers
reviewers.forEach(System.out::println);
}
}
이미지
2-3) 리팩토링 후
코드
public class StudyDashboard {
private void printParticipants(int eventId) throws IOException {
GHIssue issue = getGhIssue(eventId);
Set<String> participants = getUsernames(issue);
print(participants);
}
private void printReviewers() throws IOException {
GHIssue issue = getGhIssue(30);
Set<String> reviewers = getUsernames(issue);
print(participants);
}
// Get github issue to check homework
private GHIssue getGhIssue(int eventId) throws IOException {
GitHub gitHub = GitHub.connect();
GHRepository repository = gitHub
.getRepository("whiteship/live-study");
GHIssue issue = repository.getIssue(eventId);
return issue;
}
// Get participants
private Set<String> getUsernames(GHIssue issue)
throws IOException {
Set<String> usernames = new HashSet<>();
issue.getComments().forEach(c -> usernames
.add(c.getUserName()));
return usernames;
}
// Print participants
private void print(Set<String> participants) {
participants.forEach(System.out::println);
}
public static void main(String[] args) throws IOException {
StudyDashboard studyDashboard = new StudyDashboard();
studyDashboard.printReviewers();
studyDashboard.printParticipants(15);
}
}
이미지
3. 코드 정리하기(Slide Statements)
3-1) 개념
3-2) 리팩토링 전
코드
public class StudyDashboard {
private void printParticipants(int eventId) throws IOException {
// Get github issue to check homework
Set<String> participants = new HashSet<>();
GitHub gitHub = GitHub.connect();
GHRepository repository = gitHub
.getRepository("whiteship/live-study");
GHIssue issue = repository.getIssue(eventId);
// Get participants
issue.getComments().forEach(c -> participants
.add(c.getUserName()));
// Print participants
participants.forEach(System.out::println);
}
private void printReviewers() throws IOException {
// Get github issue to check homework
Set<String> reviewers = new HashSet<>();
GitHub gitHub = GitHub.connect();
GHRepository repository = gitHub
.getRepository("whiteship/live-study");
GHIssue issue = repository.getIssue(30);
// Get reviewers
issue.getComments().forEach(c -> reviewers
.add(c.getUserName()));
// Print reviewers
reviewers.forEach(System.out::println);
}
public static void main(String[] args) throws IOException {
StudyDashboard studyDashboard = new StudyDashboard();
studyDashboard.printReviewers();
studyDashboard.printParticipants(15);
}
}
이미지
사용할 변수를 상단에 미리 정의한 경우
상단에 모두 정의할 경우 ‘particlipants’, gitHub’, ‘issue’ 변수들이 뒤섞여 있기에
하나의 블럭 단위로 보기가 어려움. 문맥이 섞임.
변수를 사용하는 코드 바로 위에 선언한 경우
4. 메소드 올리기(Pull Up Method)
4-1) 개념
4-2) 리팩토링 전
4-3) 리팩토링 후