• 重构:一个简单的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
        }
    }
  • 相关阅读:
    工单系统的设计与实现(4)
    java_tcp_简单示例
    java_udp编程
    mysql 锁问题 (相同索引键值或同一行或间隙锁的冲突)
    行锁与表锁详解
    BTree和B+Tree详解
    深入浅出java常量池
    MySQL三大范式和反范式
    java多线程 栅栏CyclicBarrier
    SpringBoot初始教程之Servlet、Filter、Listener配置
  • 原文地址:https://www.cnblogs.com/zgynhqf/p/1624442.html
Copyright © 2020-2023  润新知