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 } }
本文探讨了两种不同的条件判断逻辑实现方式,并指出它们之间的差异。通过对比旧方法与新方法,作者详细解释了如何精简代码并保持逻辑清晰,最终提供了一个更简洁且正确的实现方案。
903

被折叠的 条评论
为什么被折叠?



