해당 포스팅은 인프런 백기선님의 '리팩토링'을 학습 후 정리한 내용입니다.
함수 이름 변경하기, 메소드 이름 변경하기, 매개변수 추가하기, 매개변수 제거하기, 시그니처 변경하기
package me.whiteship.refactoring._01_smell_mysterious_name._01_before;
import org.kohsuke.github.*;
import org.springframework.beans.factory.annotation.Value;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class StudyDashboard {
@Value("token")
private static String token;
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 = new GitHubBuilder().withOAuthToken(token).build();
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);
}
}
깃헙 whiteship/live-study 레포지토리에서 30 번째 이슈의 작성자와 리뷰를 출력한다.
studyDashboard.studyReviews(issue); //studyReviews 메서드의 이름이 알맞지 않아 //loadReviews 이름으로 수정.
인텔리제이를 사용하여 윈도우 기준 shift + F6 눌러 메서드 이름을 loadReviews 로 수정한다.
package me.whiteship.refactoring._01_smell_mysterious_name._01_change_method_declaration;
import org.kohsuke.github.*;
import org.springframework.beans.factory.annotation.Value;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class StudyDashboard {
@Value("token")
private static String token;
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 = new GitHubBuilder().withOAuthToken(token).build();
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);
}
}
public class StudyDashboard {
@Value("token")
private static String token;
private Set<String> usernames = new HashSet<>();
private Set<String> reviews = new HashSet<>();
private void loadReviews(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 = new GitHubBuilder().withOAuthToken(token).build();
GHRepository repository = gitHub.getRepository("whiteship/live-study");
GHIssue issue = repository.getIssue(30);
StudyDashboard studyDashboard = new StudyDashboard();
studyDashboard.loadReviews(issue);
studyDashboard.getUsernames().forEach(System.out::println);
studyDashboard.getReviews().forEach(System.out::println);
}
}
gitHub, repository, issue 변수를 초기화 하는 코드를 loadReviews 메서드에서 초기화 한다면 main 함수가 더 깔끔해질거 같다!
함수의 매개변수 부분을 수정하기 위해서는 인텔리제이에서 윈도우 기준
ctrl + F6 를 눌러 수정한다.GitHub gitHub = new GitHubBuilder().withOAuthToken(token).build(); GHRepository repository = gitHub.getRepository("whiteship/live-study"); GHIssue issue = repository.getIssue(30);
위 코드를 loadReviews 내부에서 초기화 하여 사용한다.
또한 해당 함수에서 매개변수를 수정하기 위해 인텔리제이에서 윈도우 기준 ctrl + F6 를 눌러 수정할 수 있다.
package me.whiteship.refactoring._01_smell_mysterious_name._02_rename_variable;
import org.kohsuke.github.*;
import org.springframework.beans.factory.annotation.Value;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class StudyDashboard {
@Value("token")
private static String token;
private Set<String> usernames = new HashSet<>();
private Set<String> reviews = new HashSet<>();
/**
* 스터디 리뷰 이슈에 작성되어 있는 리뷰어 목록과 리뷰를 읽어옵니다.
* @throws IOException
*/
private void loadReviews() throws IOException {
GitHub gitHub = new GitHubBuilder().withOAuthToken(token).build();
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());
this.reviews.add(comment.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(name -> System.out.println(name));
studyDashboard.getReviews().forEach(review -> System.out.println(review));
}
}
package me.whiteship.refactoring._01_smell_mysterious_name._02_rename_variable;
import org.kohsuke.github.*;
import org.springframework.beans.factory.annotation.Value;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class StudyDashboard {
@Value("token")
private static String token;
private Set<String> usernames = new HashSet<>();
private Set<String> reviews = new HashSet<>();
/**
* 스터디 리뷰 이슈에 작성되어 있는 리뷰어 목록과 리뷰를 읽어옵니다.
* @throws IOException
*/
private void loadReviews() throws IOException {
GitHub gitHub = new GitHubBuilder().withOAuthToken(token).build();
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());
this.reviews.add(comment.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(name -> System.out.println(name));
studyDashboard.getReviews().forEach(review -> System.out.println(review));
}
}
username, review 필드를 서로 다른 자료구조에서 따로 관리하기 보단 객체화하면 더 깔끔해질 수 있을거 같다.
JDK 14 이상에서 사용할 수 있는 record 를 사용
package me.whiteship.refactoring._01_smell_mysterious_name.practice;
import me.whiteship.refactoring._01_smell_mysterious_name._03_rename_field.StudyReview;
import org.kohsuke.github.*;
import org.springframework.beans.factory.annotation.Value;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class StudyDashboard {
@Value("token")
private static String token;
private Set<StudyReview> studyReviews = new HashSet<>();
private void loadReviews() throws IOException {
GitHub gitHub = new GitHubBuilder().withOAuthToken(token).build();
GHRepository repository = gitHub.getRepository("whiteship/live-study");
GHIssue issue = repository.getIssue(30);
List<GHIssueComment> reviews = issue.getComments();
for (GHIssueComment review : reviews) {
studyReviews.add(new StudyReview(review.getUserName(), review.getBody()));
}
}
public Set<StudyReview> getStudyReviews() {
return studyReviews;
}
public static void main(String[] args) throws IOException {
StudyDashboard studyDashboard = new StudyDashboard();
studyDashboard.loadReviews();
studyDashboard.getStudyReviews().forEach(System.out::println);
}
}
public record StudyReview(String reviewer, String review) { }
record 는 생성자를 작성하지 않아도 되고 toString, equals, hashCode 메소드에 대한 구현을 자동으로 제공