尽管我在很多代码中发现了这种严重降低可读性并往往传达错误意图的坏味道,但这种重构本身还是很容易实现的。这种毁灭性的代码所基于的假设导致了错误的代码编写习惯,并最终导致bug。如下例所示:

public class Order {
public void Checkout(List<Product> products, Customer customer) {
if (!customer.getNotFlagged()) {
// the customer account is flagged
// log some errors and return
return;
}
// normal order processing
}
}
public class Customer {
public Double Balance;
public Boolean IsNotFlagged;

public Boolean getNotFlagged() {
return Balance < 30d;
}
public void setNotFlagged(Boolean notFlagged) {
IsNotFlagged = notFlagged;
}
}
如你所见,这里的双重否定十分难以理解,我们不得不找出什么才是双重否定所要表达的肯定状态。修改代码是很容易的。如果我们找不到肯定的判断,可以添加一个处理双重否定的假设,而不要在得到结果之后再去验证。
public class Order {
public void Checkout(List<Product> products, Customer customer) {
if (customer.getFlagged()) {
// the customer account is flagged
// log some errors and return
return;
}
// normal order processing
}
}
public class Customer {
public Double Balance;
public Boolean IsFlagged;

public Boolean getFlagged() {
return Balance < 30d;
}
public void setFlagged(Boolean Flagged) {
IsFlagged = Flagged;
}
}

重构26-Remove Double Negative(去掉双重否定)的更多相关文章

  1. 重构第26天 移除双重否定(Remove Double Negative)

    理解:”移除双重否定”是指把代码中的双重否定语句修改成简单的肯定语句,这样即让代码可读,同时也给维护带来了方便. 详解:避免双重否定重构本身非常容易实现,但我们却在太多的代码中见过因为双重否定降低了代 ...

  2. leetCode练题——26. Remove Duplicates from Sorted Array

    1.题目 26. Remove Duplicates from Sorted Array--Easy Given a sorted array nums, remove the duplicates  ...

  3. [Leetcode][Python]26: Remove Duplicates from Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 26: Remove Duplicates from Sorted Array ...

  4. LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++>

    LeetCode 26 Remove Duplicates from Sorted Array [Array/std::distance/std::unique] <c++> 给出排序好的 ...

  5. 26. Remove Duplicates from Sorted Array【easy】

    26. Remove Duplicates from Sorted Array[easy] Given a sorted array, remove the duplicates in place s ...

  6. [LeetCode] Remove K Digits 去掉K位数字

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  7. 26. Remove Duplicates from Sorted Array

    题目: Given a sorted array, remove the duplicates in place such that each element appear only once and ...

  8. leetcode 26—Remove Duplicates from Sorted Array

    Given a sorted array nums, remove the duplicates in-place such that each element appear only once an ...

  9. C# 写 LeetCode easy #26 Remove Duplicates from Sorted Array

    26.Remove Duplicates from Sorted Array Given a sorted array nums, remove the duplicates in-place suc ...

随机推荐

  1. 浅谈OC中的Category

    OC特有的分类Category,依赖于类.它可以在不改变原来的类内容的基础上,为类增加一些方法.分类的使用注意: (1)分类只能增加方法,不能增加成员变量: (2)在分类方法的实现中可以访问原来类中的 ...

  2. 图片懒加载和UA池,UA代理池

    1,动态数据加载的处理 图片懒加载概念: 图片懒加载是一种页面优化技术.图片作为一种网络资源,在被请求时也与静态资源一样,将占用网络资源,而一次性将整个页面的所有图片加载完,将大大增加页面首屏加载时间 ...

  3. call function

    1 call递归扩展变量 本质上仍然是变量扩展,等价于$(),只不过扩展的时候带了参数,$(call xxx)返回的是xxx扩展之后的值.参数依次赋值给$(1),$(2)......,但是参数要在赋值 ...

  4. Java 负载均衡

    什么是负载均衡 负载均衡,英文 名称为Load Balance,指由多台服务器以对称的方式组成一个服务器集合,每台服务器都具有等价的地位,都可以单独对外提供服务而无须其他服务器的辅助.通过某种 负载分 ...

  5. Entity FramWork Code first 使用心得

    1 最有用的命令 update-database -force -verbose 2 主键如果不是默认的int或者 bigint而是guid 或者 string类型,创建记录的时候要给主键赋值 3 在 ...

  6. linux命令alias永久配置

    需求:清屏的指令是:clear,感觉用着不爽,写这个命令太慢,想自定义命令:cls 解决:命令别名指令:alias 1:查看当前系统中有哪些别名:命令行输入:alias 2:添加我们自己的别名:ali ...

  7. bzoj1465 bzoj1045: [HAOI2008] 糖果传递&&bzoj3293: [Cqoi2011]分金币

    一道神奇的题..看到做法是排序我的心是绝望的.. 首先我们可以先求出每个小朋友应该得到的糖果数,就是平均值,然后ave-a[i]就代表要从其他小朋友那得到多少个糖果(如果是负数就是要送出糖果)然后求前 ...

  8. 调整多个控件的dock的顺序

    https://stackoverflow.com/questions/2607508/how-to-control-docking-order-in-winforms Go to View -> ...

  9. Ubuntu如何定时清理内存

    Ubuntu如何定时清理内存 1.进入su jiqing@Ubuntu:~$ su 密码: root@Ubuntu:/home/jiqing# 2.创建cleanCache.sh #!/bin/bas ...

  10. APP界面常用的五种颜色搭配

    众所周知,每一种颜色带给用户的视觉感受也是不同的.现在人们对手机的依赖程度,就能看到手机中APP的发展前景,那今天就跟大家聊聊如何通过颜色搭配的不同来进行移动端APP界面的布局和排版设计.移动端UI界 ...