zoukankan      html  css  js  c++  java
  • 重构:一个简单的IF语句

    private static void OldMethod(BusinessObjectInfo parentBOInfo)
    {
        IList<BusinessObjectsPropertyInfo> bosPropertyInfo = parentBOInfo.BOsPropertyInfos;
        if ((bosPropertyInfo.Count == 1) && (null != parentBOInfo.TreeChildPropertyInfo))
        {
            //action one
        }
        else if (((bosPropertyInfo.Count > 1) && (null != parentBOInfo.TreeChildPropertyInfo))
            || ((bosPropertyInfo.Count > 0) && (null == parentBOInfo.TreeChildPropertyInfo)))
        {
            //action two
        }
    }
    private static void NewMethod(BusinessObjectInfo parentBOInfo)
    {
        IList<BusinessObjectsPropertyInfo> childrenProperties = parentBOInfo.BOsPropertyInfos;
    
        var childrenPropertiesCount = childrenProperties.Count;
        if (childrenPropertiesCount <= 0)
        {
            return;
        }
    
        var hasTreeChild = null != parentBOInfo.TreeChildPropertyInfo;
    
        if (hasTreeChild)
        {
            if (childrenPropertiesCount == 1)
            {
                //action one
            }
        }
        else
        {
            //action two
        }
    }

        以上两种写法并不完全等价(你能找出来吗?),不过就当时的业务逻辑来说,这样写是没错的。

       上面的写法,还是错了,应该是这样:

    private static void NewMethod2(BusinessObjectInfo parentBOInfo)
    {
        IList<BusinessObjectsPropertyInfo> childrenProperties = parentBOInfo.BOsPropertyInfos;
    
        var childrenPropertiesCount = childrenProperties.Count;
        if (childrenPropertiesCount <= 0)
        {
            return;
        }
    
        var hasTreeChild = null != parentBOInfo.TreeChildPropertyInfo;
    
        if (hasTreeChild && childrenPropertiesCount == 1)
        {
            //action one
        }
        else
        {
            //action two
        }
    }
  • 相关阅读:
    轻节点如何验证交易的存在
    梯度爆炸/消失与初始化参数
    归一化能够加速训练的原因
    正则化可以防止过拟合的原因
    关于周志华《机器学习》中假设空间规模大小65的计算
    linux学习0001
    目标检测算法
    opencv安装与卸载
    前端学习02
    前端学习01
  • 原文地址:https://www.cnblogs.com/zgynhqf/p/1624442.html
Copyright © 2011-2022 走看看