Thinking in States

翻译 2015年07月11日 07:24:14

Thinking in States

Niclas Nilsson
This morning, I stopped by the local store to prepare for another day of con- verting caffeine to code. Since my favorite way of doing that is by drinking lattes, and I couldn’t find any milk, I asked the clerk.
“Sorry, we’re super-duper, mega–out of milk.”
To a programmer, that’s an odd statement. You’re either out of milk, or you’re not. There is no scale when it comes to being out of milk. Perhaps she was try- ing to tell me that they’d be out of milk for a week, but the outcome was the same—espresso day for me.
In most real-world situations, people’s relaxed attitude toward state is not an issue. Unfortunately, however, many programmers are quite vague about state, too—and that is a problem.
Consider a simple webshop that only accepts credit cards and does not invoice customers, with an Order class containing this method:
public boolean isComplete() {
return isPaid() && hasShipped();
Reasonable, right? Well, even if the expression is nicely extracted into a method instead of copy ’n’ pasted everywhere, the expression shouldn’t exist at all. The fact that it does highlights a problem. Why? Because an order can’t be shipped before it’s paid. Thereby, hasShipped can’t be true unless isPaid is true, which makes part of the expression redundant. You may still want isComplete for clarity in the code, but then it should look like this:

    public boolean isComplete() {
        return hasShipped();

97 Things Every Programmer Should Know

In my work, I see both missing checks and redundant checks all the time. This example is tiny, but when you add cancellation and repayment, it’ll become more complex, and the need for good state handling increases. In this case, an order can only be in one of three distinct states:
• In progress: Can add or remove items. Can’t ship.
• Paid: Can’t add or remove items. Can be shipped.
• Shipped: Done. No more changes accepted.
These states are important, and you need to check that you’re in the expected state before doing operations, and that you only move to a legal state from where you are. In short, you have to protect your objects carefully, in the right places.
But how do you begin thinking in states? Extracting expressions to meaningful methods is a very good start, but it is just a start. The foundation is to under- stand state machines. I know you may have bad memories from CS class, but leave them behind. State machines are not particularly hard. Visualize them to make them simple to understand and easy to talk about. Test-drive your code to unravel valid and invalid states and transitions and to keep them correct. Study the State pattern. When you feel comfortable, read up on Design by Contract. It helps you ensure a valid state by validating incoming data and the object itself on entry and exit of each public method.
If your state is incorrect, there’s a bug, and you risk trashing data if you don’t abort. If you find the state checks to be noise, learn how to use a tool, code generation, weaving, or aspects to hide them. Regardless of which approach you pick, thinking in states will make your code simpler and more robust.

Thinking In C++中文版

----------------------- Page 1----------------------- 下载                       第1章  对象的演化     计算机革命起...
  • liu_hang_yu
  • liu_hang_yu
  • 2011年05月17日 07:11
  • 4064


QML StatesStates ElementsStatePropertyChangesStateGroupStateChangeScriptParentChangeAnchorChangesMan...
  • xiaojun111111
  • xiaojun111111
  • 2016年01月28日 16:51
  • 684

《Thinking in Java》读后总结与感想

作为Java界的四大名著之一《Thinking in Java》一直备受推崇,我很早就知道了这本书,当时只是初涉Java,粗略的翻了翻之后觉得看起来这本书讲的比较深就没有再去碰它,后来Java基础学完...
  • qq_28899635
  • qq_28899635
  • 2017年01月16日 09:47
  • 803

Thinking In Java 第四版读书笔记

原文地址:[译者改后代码下载][翻译]在 2....
  • webwalker
  • webwalker
  • 2007年05月22日 09:30
  • 2850

从《thinking in C++》被迫转到《C++ Primer》第一天

今天啃《thinking in C++》有种不能继续下去的感觉,一方面我一看书就停不下来,难懂+没有休息容易困,所以看的速度并不快。另外感觉里边有很多内容在建立在你了解其使用的基础上的理论提升,所以我...
  • lihb018
  • lihb018
  • 2014年09月21日 22:36
  • 3836

读《大象-Thinking in UML》的一些感悟归纳。

  • michelsn
  • michelsn
  • 2010年08月08日 21:52
  • 1720

Thinking in Java (Java 编程思想)

Thinking in Java (Java 编程思想)本书来自网络,全科-不安分的码农整理成电子书,支持PDF,ePub,Mobi格式,方便大家下载阅读。...
  • axi295309066
  • axi295309066
  • 2017年04月30日 15:16
  • 4084


  • liuhongwei123888
  • liuhongwei123888
  • 2010年12月09日 02:49
  • 2760

Thinking in java:对比C++和java

附录B 对比C++和java1.java比C慢约20倍。2.所有东西都必须置入一个类中,要用全局函数,可将static方法和数据成员置入一个类。3.类定义结尾没有分号。没有类声明,只有类定义。4.主类...
  • GE1988
  • GE1988
  • 2011年03月04日 12:01
  • 302

CPU 电源状态( C-States )

CPU 电源状态( C-States )一般用户很少注意到这个状态,通常只会在使用 CPU-Z 来监控时钟频率和电压时才会留意到它。移动处理器的 C 状态比台式机的多。例如, Core 2 Duo 处...
  • foreverdengwei
  • foreverdengwei
  • 2011年03月22日 17:07
  • 2655
您举报文章:Thinking in States