일일 코드 - 6/29 읽기 및 쓰기 분리
2868 단어 읽기 및 쓰기 분리
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);
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
읽기 전용 분리텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.