해당 포스팅은 인프런 백기선님의 '리팩토링'을 학습 후 정리한 내용입니다.
public class StudyDashboard {
@Value("token")
private static String token;
private void printParticipants(int eventId) throws IOException {
// Get github issue to check homework
GitHub gitHub = new GitHubBuilder().withOAuthToken(token).build();
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);
}
private void printReviewers() throws IOException {
// Get github issue to check homework
GitHub gitHub = new GitHubBuilder().withOAuthToken(token).build();
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);
}
public static void main(String[] args) throws IOException {
StudyDashboard studyDashboard = new StudyDashboard();
studyDashboard.printReviewers();
studyDashboard.printParticipants(15);
}
}
printParticipants, printReviewers 함수에 '의도'가 너무 많아 함수를 추출하겠다.
인텔리제이에서 윈도우 기준 ctrl + alt + m 을 눌러 의도를 표현한 부분을 함수로 추출한다.
package me.whiteship.refactoring._02_duplicated_code.practice._04;
import org.kohsuke.github.GHIssue;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;
import org.springframework.beans.factory.annotation.Value;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
public class StudyDashboard {
@Value("token")
private static String token;
private void printParticipants(int eventId) throws IOException {
// Get github issue to check homework
GHIssue issue = getGhIssue(eventId);
// Get participants
Set<String> participants = getUsernames(issue);
// Print participants
PrintParticipants(participants);
}
private void printReviewers() throws IOException {
// Get github issue to check homework
GHIssue issue = getGhIssue(30);
// Get reviewers
Set<String> reviewers = getUsernames(issue);
// Print reviewers
PrintParticipants(reviewers);
}
private void PrintParticipants(Set<String> participants) {
participants.forEach(System.out::println);
}
private Set<String> getUsernames(GHIssue issue) throws IOException {
Set<String> participants = new HashSet<>();
issue.getComments().forEach(c -> participants.add(c.getUserName()));
return participants;
}
private GHIssue getGhIssue(int eventId) throws IOException {
GitHub gitHub = new GitHubBuilder().withOAuthToken(token).build();
GHRepository repository = gitHub.getRepository("whiteship/live-study");
return repository.getIssue(eventId);
}
public static void main(String[] args) throws IOException {
StudyDashboard studyDashboard = new StudyDashboard();
studyDashboard.printReviewers();
studyDashboard.printParticipants(15);
}
}
package me.whiteship.refactoring._02_duplicated_code._05_slide_statements;
import org.kohsuke.github.GHIssue;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
public class StudyDashboard {
@Value("token")
private static String token;
private void printParticipants(int eventId) throws IOException {
// Get github issue to check homework
Set<String> participants = new HashSet<>();
GitHub gitHub = new GitHubBuilder().withOAuthToken(token).build();
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 = new GitHubBuilder().withOAuthToken(token).build();
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);
}
}
예제코드에서 paricipants, reviewers 변수가 사용되어지는 부분에서 멀리 떨어져 있어 쉽게 이해하기 힘들다.
인텔리제이에서 윈도우 기준 shift + alt + (위, 아래) 를 사용하여 사용되어진 부분에 선언한다.
package me.whiteship.refactoring._02_duplicated_code.practice._05;
import org.kohsuke.github.GHIssue;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;
import org.springframework.beans.factory.annotation.Value;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
public class StudyDashboard {
@Value("token")
private static String token;
private void printParticipants(int eventId) throws IOException {
// Get github issue to check homework
GitHub gitHub = new GitHubBuilder().withOAuthToken(token).build();
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);
}
private void printReviewers() throws IOException {
// Get github issue to check homework
GitHub gitHub = new GitHubBuilder().withOAuthToken(token).build();
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);
}
public static void main(String[] args) throws IOException {
StudyDashboard studyDashboard = new StudyDashboard();
studyDashboard.printReviewers();
studyDashboard.printParticipants(15);
}
}
public class Dashboard {
@Value("token")
protected static String token;
public static void main(String[] args) throws IOException {
ReviewerDashboard reviewerDashboard = new ReviewerDashboard();
reviewerDashboard.printReviewers();
ParticipantDashboard participantDashboard = new ParticipantDashboard();
participantDashboard.printParticipants(15);
}
}
public class ParticipantDashboard extends Dashboard {
public void printParticipants(int eventId) throws IOException {
printUsernames(eventId);
}
protected void printUsernames(int eventId) throws IOException {
// Get github issue to check homework
GitHub gitHub = new GitHubBuilder().withOAuthToken(token).build();
GHRepository repository = gitHub.getRepository("whiteship/live-study");
GHIssue issue = repository.getIssue(eventId);
// Get usernames
Set<String> usernames = new HashSet<>();
issue.getComments().forEach(c -> usernames.add(c.getUserName()));
// Print usernames
usernames.forEach(System.out::println);
}
}
public class ReviewerDashboard extends Dashboard {
public void printReviewers() throws IOException {
printUsernames(30);
}
protected void printUsernames(int eventId) throws IOException {
// Get github issue to check homework
GitHub gitHub = new GitHubBuilder().withOAuthToken(token).build();
GHRepository repository = gitHub.getRepository("whiteship/live-study");
GHIssue issue = repository.getIssue(eventId);
// Get usernames
Set<String> usernames = new HashSet<>();
issue.getComments().forEach(c -> usernames.add(c.getUserName()));
// Print usernames
usernames.forEach(System.out::println);
}
}
각 자식 클래스에서 printUsernames 함수는 비슷한 동작을 수행하는 중복 코드이다.
자식 클래스에서의 반복되는 중복 코드의 수정은 일괄 수정이 어렵다.
이를 해결하기 위해서는 자식 클래스의 중복되는 함수를 부모 클래스로 올리는 Pull Up Method 를 사용한다.
윈도우 기준 인텔리제이에서 메서드 이름 우클릭 -> refactor -> pull members up 을 사용한다.
package me.whiteship.refactoring._02_duplicated_code.practice._06;
import org.kohsuke.github.GHIssue;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;
import org.springframework.beans.factory.annotation.Value;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
public class Dashboard {
@Value("token")
protected static String token;
public static void main(String[] args) throws IOException {
ReviewerDashboard reviewerDashboard = new ReviewerDashboard();
reviewerDashboard.printReviewers();
ParticipantDashboard participantDashboard = new ParticipantDashboard();
participantDashboard.printParticipants(15);
}
protected void printUsernames(int eventId) throws IOException {
// Get github issue to check homework
GHIssue issue = getGhIssue(eventId);
// Get usernames
Set<String> usernames = getUsername(issue);
// Print usernames
usernames.forEach(System.out::println);
}
private Set<String> getUsername(GHIssue issue) throws IOException {
Set<String> usernames = new HashSet<>();
issue.getComments().forEach(c -> usernames.add(c.getUserName()));
return usernames;
}
private GHIssue getGhIssue(int eventId) throws IOException {
GitHub gitHub = new GitHubBuilder().withOAuthToken(token).build();
GHRepository repository = gitHub.getRepository("whiteship/live-study");
GHIssue issue = repository.getIssue(eventId);
return issue;
}
}
package me.whiteship.refactoring._02_duplicated_code.practice._06;
import java.io.IOException;
public class ParticipantDashboard extends Dashboard {
public void printParticipants(int eventId) throws IOException {
super.printUsernames(eventId);
}
}
package me.whiteship.refactoring._02_duplicated_code.practice._06;
import java.io.IOException;
public class ReviewerDashboard extends Dashboard {
public void printReviewers() throws IOException {
super.printUsernames(30);
}
}
부모 클래스에 printUsernames 함수를 선언하여 각 자식 클래스에서 super.printUsernames() 를 통해 접근하여 부모 쪽에서 관리한다.