尽管我在很多代码中发现了这种严重降低可读性并往往传达错误意图的坏味道,但这种重构本身还是很容易实现的。这种毁灭性的代码所基于的假设导致了错误的代码编写习惯,并最终导致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. 2015/12/29 eclipse应用 输出三角形

    public class Myfirst { public static void main(String[] args) { System.out.println("hello world ...

  2. Hackrank Candies DP

    题目链接:传送门 题意: n个学生站一行,老师给每个学生发至少一个糖 相邻学生,a[i] > a[i-1] 的话,那么右边学生的糖一定要发得比左边学生的糖多 问你满足条件这个老师总共最少的发多少 ...

  3. [BZOJ2144]国家集训队 跳跳棋

    题目描述 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子. 我们用跳跳棋来做一个简单的游戏:棋盘上有3颗棋子,分别在a,b,c这三个位置.我们要通过最少的跳动把他们的位置移动 ...

  4. UESTC93 King's Sanctuary

    King's Sanctuary Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) ...

  5. 获取WiFi MAC地址总结【转】

    本文转载自:http://blog.csdn.net/crazyman2010/article/details/50464256 今天对MAC地址的获取做了一些学习,目前网上获取MAC地址的方法主要如 ...

  6. ThreadLocal工具类的使用(隔离思想)

    ThreadLocal不是用来解决共享对象的多线程访问问题的, 通过ThreadLocal的set()方法设置到线程的ThreadLocal.ThreadLocalMap里的是是线程自己要存储的对象, ...

  7. Linux网络协议栈(四)——链路层(2)

    2.协议相关2.1.第3层协议的管理在Linux内核中,有两种不同目的的3层协议:(1)    ptype_all管理的协议主要用于分析目的,它接收所有到达第3层协议的数据包.(2)    ptype ...

  8. 通过minicom传送文件的相关配置及使用方法

    写一下我使用串口向开发板传送文件中遇到的问题及解决办法: 使用的软硬件环境: 虚拟机:VMware® Workstation 8.0.1 Linux操作系统:Fedora 9 开发板:mini6410 ...

  9. JavaScript对象模型-执行模型

    数据类型基本数据类型基本数据类型是JS语言最底层的实现.简单数值类型: 有Undefined, Null, Boolean, Number和String.注意,描述中的英文单词在这里仅指数据类型的名称 ...

  10. 摘抄 - JavaScript JSON的总结

    JSON 是用于存储和传输数据的格式,通常用于服务端向网页传递数据 什么是 JSON? JSON英文全称 JavaScript Object Notation(JavaScript 对象表示法),是一 ...