조건문 분해하기(Decompose Conditional)
- 여러 조건에 따라 달라지는 코드를 작성하다보면 종종 긴 함수가 만들어지는 것을 목격할 수 있다.
- “조건” 과 “액션” 모두 “의도”를 표현해야한다
- 기술적으로는 “함수 추출하기”와 동일한 리팩토링이지만 의도만 다를 뿐이다.
리팩토링 (전)
private Participant findParticipant(String username, List<Participant> participants) {
Participant participant = null;
if (participants.stream().noneMatch(p -> p.username().equals(uesrname))) {
participant = new Participant(uesrname);
participants.add(participant);
} else {
participant = participants.steam().filter(p -> p.username().equals(uesrname)).findFirst().orElseThrow();
}
return participant;
}
위에 함수는 participants를 사용한 코드인데 눈에 잘 안들어온다. 무엇을 하는코드인지도 모르겠다.
리팩토링 (후)
private Participant findParticipant(String username, List<Participant> participants) {
Participant participant = null;
if (isNewParticipant(username, participants)) {
participant = createNewParticipnat(username, participants);
} else {
participnat = findExistingParticipant(username, participants);
}
return participant;
}
코드를 리팩토링 하니 이 함수가 어떤 함수인지 알수가 있게 되었다. 조건문에 의도를 알게 되었고 이름도 누가봐도 어떤 의미인지 알수가 있다.
isNewParticipant(username, participants) → 새로운 참가자 인가
createNewParticipnat(username, participants) → 새로운 참가자이다
findExistingParticipant(username, participants) → 새로운 참가자가 아니다
여기까지만 해도 잘한 리팩토링이지만 완벽하지는 않다. 한번더 리팩토링을 해보겠다
리팩토링(마지막)
private Participant findParticipant(String username, List<Participant> participants) {
return isNewParticipant(username, participants) ?
createNewParticipant(username, participants) :
findExistingParticipnat(username, participants);
}
삼항연산자를 이용하여 리팩토링을 하였다. 만약 첫번째 코드를 보자마자 삼항연산자를 떠올렸으면 상당한 내공이 있을것이다. 처음부터 하기는 힘들다. 일단 조건무에 의도를 알수있게 Decompose를 해주고 그 다음 삼항 연산자로 바꾸는 연습을 하자.
반복문 쪼개기 ( Split Loop ) 이란?
- 하나의 반복문에서 여러 다른 작업을 하는 코드를 쉽게 찾아볼 수 있다
- 해당 반복문을 수정할 때 여러 작업을 모두 고려하며 코딩을 해야한다.
- 반복문을 여러개로 쪼개면 보다 쉽게 이해하고 수정할 수 있다
- 성능 문제를 야기할 수 있지만, “리팩토링”은 “성능 최적화”와 별개의 작업이다. 리팩토링을 마친 이후에 성능 최적화 시도할 수 있다.
코드 하나의 반복문이 길면 코드를 수정할때 그 안에있는 코드들을 고려하면서 수정해야되는 단점이 있다. 반복문을 쪼깨고 성능 최적화를 시도하는것이 좋다
리팩토링 (전)
public class StudyDashboard {
public static void main(String[] args) throws IOException, InterruptedException {
StudyDashboard studyDashboard = new StudyDashboard();
studyDashboard.print();
}
private void print() throws IOException, InterruptedException {
GitHub gitHub = GitHub.connect();
GHRepository repository = gitHub.getRepository("whiteship/live-study");
List<Participant> participants = new CopyOnWriteArrayList<>();
int totalNumberOfEvents = 15;
ExecutorService service = Executors.newFixedThreadPool(8);
CountDownLatch latch = new CountDownLatch(totalNumberOfEvents);
for (int index = 1 ; index <= totalNumberOfEvents ; index++) {
int eventId = index;
service.execute(new Runnable() {
@Override
public void run() {
try {
GHIssue issue = repository.getIssue(eventId);
List<GHIssueComment> comments = issue.getComments();
for (GHIssueComment comment : comments) {
String username = comment.getUserName();
boolean isNewUser = participants.stream().noneMatch(p -> p.username().equals(username));
Participant participant = null;
if (isNewUser) {
participant = new Participant(username);
participants.add(participant);
} else {
participant = participants.stream().filter(p -> p.username().equals(username)).findFirst().orElseThrow();
}
participant.setHomeworkDone(eventId);
}
latch.countDown();
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
});
}
latch.await();
service.shutdown();
try (FileWriter fileWriter = new FileWriter("participants.md");
PrintWriter writer = new PrintWriter(fileWriter)) {
participants.sort(Comparator.comparing(Participant::username));
writer.print(header(totalNumberOfEvents, participants.size()));
participants.forEach(p -> {
long count = p.homework().values().stream()
.filter(v -> v == true)
.count();
double rate = count * 100 / totalNumberOfEvents;
String markdownForHomework = String.format("| %s %s | %.2f%% |\\n", p.username(), checkMark(p, totalNumberOfEvents), rate);
writer.print(markdownForHomework);
});
}
}
쓰레드 run()함수 내부 for문에서 과제를 끝낸 참가자를 찾는 로직과, 첫번쨰로 끝낸 참가자를 찾는다.
하나의 반복문에서 여러 다른 작업을 해야한다. 반복문을 여러개로 쪼개 보다 쉽게 이해하게 한다.
리팩토링 (후)
public class StudyDashboard {
private final int totalNumberOfEvents;
private final List<Participant> participants;
private final Participant[] firstParticipantsForEachEvent;
public StudyDashboard(int totalNumberOfEvents) {
this.totalNumberOfEvents = totalNumberOfEvents;
participants = new CopyOnWriteArrayList<>();
firstParticipantsForEachEvent = new Participant[this.totalNumberOfEvents];
}
public static void main(String[] args) throws IOException, InterruptedException {
StudyDashboard studyDashboard = new StudyDashboard(15);
studyDashboard.print();
}
private void print() throws IOException, InterruptedException {
GHRepository ghRepository = getGhRepository();
ExecutorService service = Executors.newFixedThreadPool(8);
CountDownLatch latch = new CountDownLatch(totalNumberOfEvents);
for (int index = 1 ; index <= totalNumberOfEvents ; index++) {
int eventId = index;
service.execute(new Runnable() {
@Override
public void run() {
try {
GHIssue issue = ghRepository.getIssue(eventId);
List<GHIssueComment> comments = issue.getComments();
Date firstCreatedAt = null;
Participant first = null;
for (GHIssueComment comment : comments) {
Participant participant = findParticipant(comment.getUserName(), participants);
participant.setHomeworkDone(eventId);
if (firstCreatedAt == null || comment.getCreatedAt().before(firstCreatedAt)) {
firstCreatedAt = comment.getCreatedAt();
first = participant;
}
}
firstParticipantsForEachEvent[eventId - 1] = first;
latch.countDown();
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
}
});
}
latch.await();
service.shutdown();
new StudyPrinter(this.totalNumberOfEvents, this.participants).execute();
printFirstParticipants();
}
private void printFirstParticipants() {
Arrays.stream(this.firstParticipantsForEachEvent).forEach(p -> System.out.println(p.username()));
}
private GHRepository getGhRepository() throws IOException {
GitHub gitHub = GitHub.connect();
GHRepository repository = gitHub.getRepository("whiteship/live-study");
return repository;
}
private Participant findParticipant(String username, List<Participant> participants) {
return isNewParticipant(username, participants) ?
createNewParticipant(username, participants) :
findExistingParticipant(username, participants);
}
private Participant findExistingParticipant(String username, List<Participant> participants) {
Participant participant;
participant = participants.stream().filter(p -> p.username().equals(username)).findFirst().orElseThrow();
return participant;
}
private Participant createNewParticipant(String username, List<Participant> participants) {
Participant participant;
participant = new Participant(username);
participants.add(participant);
return participant;
}
private boolean isNewParticipant(String username, List<Participant> participants) {
return participants.stream().noneMatch(p -> p.username().equals(username));
}
}
반복문을 하나 더 만들어 분리하였으며, 반복문을 함수로 추출하였다.
조건문을 다형성으로 바꾸기(Replace Conditional with Polymorphism)
- 여러 타입에 따라 각기 다른 로직으로 처리해야 하는 경우에 다형성을 적용해서 조건문을 보다 명확하게 분리할 수 있다. 반복되는 switch문을 각기 다른 클래스를 만들어 제거할 수 있다
- 공통으로 사용되는 로직은 상위클래스에 두고 달라지는 부분만 하위클래스에 둠으로써, 달라지는 부분만 강조할 수 있다.
- 모든 조건문을 다형성으로 바꿔야 하는 것은 아니다.
리팩토링 (전)
public class StudyPrinter {
private int totalNumberOfEvents;
private List<Participant> participants;
private PrinterMode printerMode;
public StudyPrinter(int totalNumberOfEvents, List<Participant> participants, PrinterMode printerMode) {
this.totalNumberOfEvents = totalNumberOfEvents;
this.participants = participants;
this.participants.sort(Comparator.comparing(Participant::username));
this.printerMode = printerMode;
}
public void execute() throws IOException {
switch (printerMode) {
case CVS -> {
try (FileWriter fileWriter = new FileWriter("participants.cvs");
PrintWriter writer = new PrintWriter(fileWriter)) {
writer.println(cvsHeader(this.participants.size()));
this.participants.forEach(p -> {
writer.println(getCvsForParticipant(p));
});
}
}
case CONSOLE -> {
this.participants.forEach(p -> {
System.out.printf("%s %s:%s\\n", p.username(), checkMark(p), p.getRate(this.totalNumberOfEvents));
});
}
case MARKDOWN -> {
try (FileWriter fileWriter = new FileWriter("participants.md");
PrintWriter writer = new PrintWriter(fileWriter)) {
writer.print(header(this.participants.size()));
this.participants.forEach(p -> {
String markdownForHomework = getMarkdownForParticipant(p);
writer.print(markdownForHomework);
});
}
}
}
}
private String getCvsForParticipant(Participant participant) {
StringBuilder line = new StringBuilder();
line.append(participant.username());
for (int i = 1 ; i <= this.totalNumberOfEvents ; i++) {
if(participant.homework().containsKey(i) && participant.homework().get(i)) {
line.append(",O");
} else {
line.append(",X");
}
}
line.append(",").append(participant.getRate(this.totalNumberOfEvents));
return line.toString();
}
private String cvsHeader(int totalNumberOfParticipants) {
StringBuilder header = new StringBuilder(String.format("참여자 (%d),", totalNumberOfParticipants));
for (int index = 1; index <= this.totalNumberOfEvents; index++) {
header.append(String.format("%d주차,", index));
}
header.append("참석율");
return header.toString();
}
private String getMarkdownForParticipant(Participant p) {
return String.format("| %s %s | %.2f%% |\\n", p.username(), checkMark(p),
p.getRate(this.totalNumberOfEvents));
}
/**
* | 참여자 (420) | 1주차 | 2주차 | 3주차 | 참석율 |
* | --- | --- | --- | --- | --- |
*/
private String header(int totalNumberOfParticipants) {
StringBuilder header = new StringBuilder(String.format("| 참여자 (%d) |", totalNumberOfParticipants));
for (int index = 1; index <= this.totalNumberOfEvents; index++) {
header.append(String.format(" %d주차 |", index));
}
header.append(" 참석율 |\\n");
header.append("| --- ".repeat(Math.max(0, this.totalNumberOfEvents + 2)));
header.append("|\\n");
return header.toString();
}
/**
* |:white_check_mark:|:white_check_mark:|:white_check_mark:|:x:|
*/
private String checkMark(Participant p) {
StringBuilder line = new StringBuilder();
for (int i = 1 ; i <= this.totalNumberOfEvents ; i++) {
if(p.homework().containsKey(i) && p.homework().get(i)) {
line.append("|:white_check_mark:");
} else {
line.append("|:x:");
}
}
return line.toString();
}
}
이 코드에서는 CVS, CONSOLE, MARKDOWN 3가지 옵션으로 출력하고있다.
public class StudyDashboard {
private final int totalNumberOfEvents;
private final List<Participant> participants;
public StudyDashboard(int totalNumberOfEvents) {
this.totalNumberOfEvents = totalNumberOfEvents;
participants = new CopyOnWriteArrayList<>();
}
public static void main(String[] args) throws IOException, InterruptedException {
StudyDashboard studyDashboard = new StudyDashboard(15);
studyDashboard.print();
}
private void print() throws IOException, InterruptedException {
checkGithubIssues(getGhRepository());
new StudyPrinter(this.totalNumberOfEvents, this.participants, PrinterMode.CONSOLE).execute();
}
private GHRepository getGhRepository() throws IOException {
GitHub gitHub = GitHub.connect();
return gitHub.getRepository("whiteship/live-study");
}
private void checkGithubIssues(GHRepository repository) throws InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(8);
CountDownLatch latch = new CountDownLatch(totalNumberOfEvents);
for (int index = 1 ; index <= totalNumberOfEvents ; index++) {
int eventId = index;
service.execute(() -> {
try {
GHIssue issue = repository.getIssue(eventId);
checkHomework(issue.getComments(), participants, eventId);
latch.countDown();
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
});
}
latch.await();
service.shutdown();
}
private void checkHomework(List<GHIssueComment> comments, List<Participant> participants, int eventId) {
for (GHIssueComment comment : comments) {
Participant participant = findParticipant(comment.getUserName(), participants);
participant.setHomeworkDone(eventId);
}
}
private Participant findParticipant(String username, List<Participant> participants) {
return isNewParticipant(username, participants) ?
createNewParticipant(username, participants) :
findExistingParticipant(username, participants);
}
private Participant findExistingParticipant(String username, List<Participant> participants) {
Participant participant;
participant = participants.stream().filter(p -> p.username().equals(username)).findFirst().orElseThrow();
return participant;
}
private Participant createNewParticipant(String username, List<Participant> participants) {
Participant participant;
participant = new Participant(username);
participants.add(participant);
return participant;
}
private boolean isNewParticipant(String username, List<Participant> participants) {
return participants.stream().noneMatch(p -> p.username().equals(username));
}
}
- StudyDashboard Class에서는 print 함수에 PrinterMode 에 자신이 출력하고싶은 옵션을 넣어주어 출력이 가능하다.
public enum PrinterMode {
CONSOLE, CVS, MARKDOWN
}
public record Participant(String username, Map<Integer, Boolean> homework) {
public Participant(String username) {
this(username, new HashMap<>());
}
public double getRate(double total) {
long count = this.homework.values().stream()
.filter(v -> v == true)
.count();
return count * 100 / total;
}
public void setHomeworkDone(int index) {
this.homework.put(index, true);
}
}
리팩토링 (후)
StudyPrinterClass를 남두고 StudyPrinter를 상속받는 Class를 만들겠다.
ConsolePrinter, CvsPrinter, MarkdownPrinter
만들고 나서 StudyPrinterClass 에서는 더이상 PrinterMode가 필요가 없어지므로 삭제한다.
ConsolePrinter에 StudyPrinter를 상속 받고 생성자와 오버라이딩을 해주고 오버라이딩에는 StudyPrinter에서 Console 코드를 가져와 준다.
그리고 StudyPrinter에서 공유해줘야할 필드들을 protected로 변경해준다.
이후 CvsPrinter, MarkdownPrinter도 ConsolePrinter처럼 해준다.
public abstract class StudyPrinter {
protected int totalNumberOfEvents;
protected List<Participant> participants;
public StudyPrinter(int totalNumberOfEvents, List<Participant> participants) {
this.totalNumberOfEvents = totalNumberOfEvents;
this.participants = participants;
this.participants.sort(Comparator.comparing(Participant::username));
}
public abstract void execute() throws IOException;
/**
* |:white_check_mark:|:white_check_mark:|:white_check_mark:|:x:|
*/
protected String checkMark(Participant p) {
StringBuilder line = new StringBuilder();
for (int i = 1 ; i <= this.totalNumberOfEvents ; i++) {
if(p.homework().containsKey(i) && p.homework().get(i)) {
line.append("|:white_check_mark:");
} else {
line.append("|:x:");
}
}
return line.toString();
}
}
public class ConsolePrinter extends StudyPrinter {
public ConsolePrinter(int totalNumberOfEvents, List<Participant> participants) {
super ( totalNumberOfEvents, participants );
}
@Override
public void execute() throws IOException {
this.participants.forEach(p -> {
System.out.printf("%s %s:%s\\n", p.username(), checkMark(p), p.getRate(this.totalNumberOfEvents));
});
}
}
public class CvsPrinter extends StudyPrinter{
public CvsPrinter(int totalNumberOfEvents, List<Participant> participants) {
super ( totalNumberOfEvents, participants );
}
@Override
public void execute() throws IOException {
try (FileWriter fileWriter = new FileWriter("participants.cvs");
PrintWriter writer = new PrintWriter(fileWriter)) {
writer.println(cvsHeader(this.participants.size()));
this.participants.forEach(p -> {
writer.println(getCvsForParticipant(p));
});
}
}
private String getCvsForParticipant(Participant participant) {
StringBuilder line = new StringBuilder();
line.append(participant.username());
for (int i = 1 ; i <= this.totalNumberOfEvents ; i++) {
if(participant.homework().containsKey(i) && participant.homework().get(i)) {
line.append(",O");
} else {
line.append(",X");
}
}
line.append(",").append(participant.getRate(this.totalNumberOfEvents));
return line.toString();
}
private String cvsHeader(int totalNumberOfParticipants) {
StringBuilder header = new StringBuilder(String.format("참여자 (%d),", totalNumberOfParticipants));
for (int index = 1; index <= this.totalNumberOfEvents; index++) {
header.append(String.format("%d주차,", index));
}
header.append("참석율");
return header.toString();
}
}
public class MarkdownPrinter extends StudyPrinter{
public MarkdownPrinter(int totalNumberOfEvents, List<Participant> participants) {
super ( totalNumberOfEvents, participants );
}
@Override
public void execute() throws IOException {
try (FileWriter fileWriter = new FileWriter("participants.md");
PrintWriter writer = new PrintWriter(fileWriter)) {
writer.print(header(this.participants.size()));
this.participants.forEach(p -> {
String markdownForHomework = getMarkdownForParticipant(p);
writer.print(markdownForHomework);
});
}
}
private String getMarkdownForParticipant(Participant p) {
return String.format("| %s %s | %.2f%% |\\n", p.username(), checkMark(p),
p.getRate(this.totalNumberOfEvents));
}
/**
* |참여자(420) | 1주차| 2주차| 3주차|참석율|
* | --- | --- | --- | --- | --- |
*/
private String header(int totalNumberOfParticipants) {
StringBuilder header = new StringBuilder(String.format("| 참여자 (%d) |", totalNumberOfParticipants));
for (int index = 1; index <= this.totalNumberOfEvents; index++) {
header.append(String.format(" %d주차 |", index));
}
header.append(" 참석율 |\\n");
header.append("| --- ".repeat(Math.max(0, this.totalNumberOfEvents + 2)));
header.append("|\\n");
return header.toString();
}
}
public class StudyDashboard {
private final int totalNumberOfEvents;
private final List<Participant> participants;
public StudyDashboard(int totalNumberOfEvents) {
this.totalNumberOfEvents = totalNumberOfEvents;
participants = new CopyOnWriteArrayList<>();
}
public static void main(String[] args) throws IOException, InterruptedException {
StudyDashboard studyDashboard = new StudyDashboard(15);
studyDashboard.print();
}
private void print() throws IOException, InterruptedException {
checkGithubIssues(getGhRepository());
new MarkdownPrinter(this.totalNumberOfEvents, this.participants).execute();
}
private GHRepository getGhRepository() throws IOException {
GitHub gitHub = GitHub.connect();
return gitHub.getRepository("whiteship/live-study");
}
private void checkGithubIssues(GHRepository repository) throws InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(8);
CountDownLatch latch = new CountDownLatch(totalNumberOfEvents);
for (int index = 1 ; index <= totalNumberOfEvents ; index++) {
int eventId = index;
service.execute(() -> {
try {
GHIssue issue = repository.getIssue(eventId);
checkHomework(issue.getComments(), participants, eventId);
latch.countDown();
} catch (IOException e) {
throw new IllegalArgumentException(e);
}
});
}
latch.await();
service.shutdown();
}
private void checkHomework(List<GHIssueComment> comments, List<Participant> participants, int eventId) {
for (GHIssueComment comment : comments) {
Participant participant = findParticipant(comment.getUserName(), participants);
participant.setHomeworkDone(eventId);
}
}
private Participant findParticipant(String username, List<Participant> participants) {
return isNewParticipant(username, participants) ?
createNewParticipant(username, participants) :
findExistingParticipant(username, participants);
}
private Participant findExistingParticipant(String username, List<Participant> participants) {
Participant participant;
participant = participants.stream().filter(p -> p.username().equals(username)).findFirst().orElseThrow();
return participant;
}
private Participant createNewParticipant(String username, List<Participant> participants) {
Participant participant;
participant = new Participant(username);
participants.add(participant);
return participant;
}
private boolean isNewParticipant(String username, List<Participant> participants) {
return participants.stream().noneMatch(p -> p.username().equals(username));
}
}
더이상 PrinterMode eun은 필요없으니 삭제하자.
- switch문이 포함된 함수가 여러개 보일 경우, case별로 클래스를 하나씩 만들어 고통 switch 로직의 중복을 없앨 수 있다.
- 다른 방법으로는 로직을 슈퍼클래스에 넣어 변형 동작에 신경쓰지 않게 만들고, 변형 동작을 뜻하는 case들을 각각의 서브 클래스로 만든다.
'Language > Java' 카테고리의 다른 글
엔티티의 생명주기 (0) | 2022.10.10 |
---|---|
긴 매개변수 목록 (0) | 2022.08.15 |
[리팩토링] 긴 함수 (0) | 2022.08.06 |
Optional (0) | 2022.07.02 |
Java8 Stream (0) | 2022.07.02 |