前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >重构:一个简单的IF语句

重构:一个简单的IF语句

作者头像
用户1172223
发布2018-01-26 15:23:33
5580
发布2018-01-26 15:23:33
举报
文章被收录于专栏:哲学驱动设计哲学驱动设计
代码语言:javascript
复制
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
    }
}

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

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

代码语言:javascript
复制
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
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2009-12-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档