일일 코드 - 6/29 읽기 및 쓰기 분리

public class CartMainTypeSelector {
public static void selectCartMainType(CartContext context,CartOption options){
Params params = context.getParams();
if (params != null && params.isMiniCart()) {
options.cartType = CartType.MINI_CART;
return;
} 
if(context.getShopId() > 0){
options.cartType = CartType.SHOP_TYPE;
} 
}

매개 변수 1에서 값을 읽고 판단과 계산에 따라 매개 변수 2의 값을 수정하는 흔한 방법
CQS(command query seperation의 사상에 따르면 조회를 하거나 대상의 상태를 갱신하는 것이 가장 좋다.이렇게 하면 그렇게 조회하는 방법에 대해 우리는 매우 안심할 수 있다. 왜냐하면 우리는 임의의 순서와 횟수로 그것들을 호출할 수 있고, 그것들의 결과는 대다수 상황에서 예측할 수 있고 변하지 않기 때문이다.
public class CartMainTypeSelector {
public static CartType getCartMainType(CartContext context){
Params params = context.getParams();
if (params != null && params.isMiniCart()) {
return CartType.MINI_CART;
} 
if(context.getShopId() > 0){
return CartType.SHOP_TYPE;
} 
}



다음 두 단락의 코드의 가독성과 안전성을 비교해 보세요(부작용 측면에서 볼 때)
CartOption option = ...;
CartContext context = ...;
option.cartType=getCartMainType(context);


CartOption option = ...;
CartContext context = ...;
selectCartMainType(context,option);

좋은 웹페이지 즐겨찾기