inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

워밍업 클럽 2기(클린코드, 테스트코드) 과제 (Day 4)

세뇨르
0

image

Readable Code: 읽기 좋은 코드를 작성하는 사고법(링크)

  1. 코드 리팩토링

public boolean validateOrder(Order order) {
    if (order.getItems().size() == 0) {
        log.info("주문 항목이 없습니다.");
        return false;
    } else {
        if (order.getTotalPrice() > 0) {
            if (!order.hasCustomerInfo()) {
                log.info("사용자 정보가 없습니다.");
                return false;
            } else {
                return true;
            }
        } else if (!(order.getTotalPrice() > 0)) {
            log.info("올바르지 않은 총 가격입니다.");
            return false;
        }
    }
    return true;
}
public boolean validateOrder(Order order) {
    if (order == null) {
        log.info("주문 정보가 없습니다.");
        return false;
    }
    if (order.hasNoItems()) {
        log.info("주문 항목이 없습니다.");
        return false;
    }
    if (order.isInvalidTotalPrice()) {
        log.info("올바르지 않은 총 가격입니다.");
        return false;
    }
    if (order.hasNoCustomerInfo()) {
        log.info("사용자 정보가 없습니다.");
        return false;
    }
    return true;
}
public class Order {
    
    private List<Item> items;
    private double totalPrice;
    private CustomrInfo customerInfo;

    public boolean hasNoItems() {
        return items == null || items.isEmpty();
    }

    public boolean isInvalidTotalPrice() {
        return totalPrice <= 0;
    }

    public boolean hasNoCustomerInfo() {
        return customerInfo == null;
    }
}

  1. SOLID 정리

SOLID

SRP

OCP

LSP

ISP

DIP

백엔드 클린코드 SOLID 리팩토링

답변 0