인프런 워밍업 클럽 스터디 4기 - Clean Code & Test <Day 4. 미션>

자신만의 언어로 클린 코딩 하기

as-is

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;
}

to-be

validateOrder.java

public boolean validateOrder(Order order) {
    if (order.getSizeOfItems == 0) {
      log.info(""주문 항목이 없습니다."");
      return false;
    }
    if (order.getTotalPrice() < 0) {
      log.info(""올바르지 않은 총 가격입니다."");
      return false;
    }
    if (order.isCustomerInfoEmpty()) {
      log.info(""사용자 정보가 없습니다."");
      return false;
    }
    return true;
 }

Order.java

public class Order {
  private String[] items;
  private int[] sizes;
  private int[] prices;
  private Object customerInfo;
  public Order() {

  } 

  public int getSizeOfItems() {
    int totalSize = 0;
    for (int size : sizes) {
      totalSize += size;
    }
    return totalSize;
  }

  public int getTotalPrice() {
    int totalPrice = 0;
    for (int i = 0; i < sizes.length; i++) {
      totalPrice += sizes[i] * prices[i];
    }
    return totalPrice;
  }

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

댓글을 작성해보세요.

채널톡 아이콘