refactoring : duplicated code - extract function

This commit is contained in:
haerong22
2022-02-16 01:11:55 +09:00
parent edead5a675
commit 400ce9cf19
2 changed files with 97 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
package com.example.refactoring._02_duplicated_code._00_before;
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 {
private void printParticipants(int eventId) throws IOException {
// Get github issue to check homework
GitHub gitHub = GitHub.connect();
GHRepository repository = gitHub.getRepository("haerong22/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 = GitHub.connect();
GHRepository repository = gitHub.getRepository("haerong22/Study");
GHIssue issue = repository.getIssue(1);
// 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(1);
}
}

View File

@@ -0,0 +1,49 @@
package com.example.refactoring._02_duplicated_code._04_extract_function;
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 {
private void printParticipants(int eventId) throws IOException {
// 메소드를 추출하여 해당 코드의 의도를 명확하게 전달 할 수 있다.(구현은 메소드에서)
GHIssue issue = getGhIssue(eventId);
Set<String> participants = getUsernames(issue);
print(participants);
}
private void print(Set<String> participants) {
participants.forEach(System.out::println);
}
private Set<String> getUsernames(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 = GitHub.connect();
GHRepository repository = gitHub.getRepository("haerong22/Study");
GHIssue issue = repository.getIssue(eventId);
return issue;
}
private void printReviewers() throws IOException {
GHIssue issue = getGhIssue(1);
Set<String> reviewers = getUsernames(issue);
print(reviewers);
}
public static void main(String[] args) throws IOException {
StudyDashboard studyDashboard = new StudyDashboard();
studyDashboard.printReviewers();
studyDashboard.printParticipants(1);
}
}