原文已经收入到笔记中名称是Anti-If: The missing patterns

Around 10 years ago I encountered the anti-if campaign and found it to be an absurd concept. How on earth would you make a useful program without using an if statement? Preposterous.

大概十年前我遇到if这种模式,并且发现他是毫无价值的想法。
你究竟要怎样才能不用if创造一个十分有用的代码。

But then it gets you thinking. Do you remember that heavily nested code you had to understand last week? That kinda sucked right? If only there was a way to make it simpler.

但是仔细想想,你还记得那些重度嵌套的代码吗?那种正确吗?
是不是有一种相似的方法呢?

The anti-if campaign site is sadly low on practical advice. This post intends to remedy that with a collection of patterns you can adopt when the need arises. But first let’s look at the problem that if statements pose.

if模式是一个非常不好的实践建议,用集合的方式去积极的补救,
看一下if场景。

The problems of if statements

The first problem with if statements is that they often make it easy to modify code in bad ways. Let’s start with the birth of a new if statement:

第一个问题,使用if会导致采用不正确的方式去修改代码。

1
2
3
4
5
6
7
8
9
10
public void theProblem(boolean someCondition) {
// SharedState
if(someCondition) {
// CodeBlockA
} else {
// CodeBlockB
}
}

This isn’t too bad at this point, but we’ve already given us some problems. When I read this code I have to check how CodeBlockA and CodeBlockB are modifying the same SharedState. This can be easy to read at first but can become difficult as the CodeBlocks grow and the coupling becomes more complicated.

这是看起来还不错,但是已经有几个问题了,第一个我可能同时修改A块和B块。
这个可能开始很好阅读。但是最后随着代码块的增长,越来越多的耦合变得更加复杂。

You’ll often see the above CodeBlocks abused with further nested if statements and local returns. Making it hard to see what the business logic is through the routing logic.

你会经常看到这些代码块中会嵌套更多的if和返回值。使得阅读代码逻辑非常困难。

The second problem with if statements is when they are duplicated. This means means a domain concept is missing. It’s all too easy to increase coupling by bringing things together than don’t need to be. Making code harder to read and change.

第二个问题当这些if地方被复制的时候。那就意味着这个领域概念丢失了。
这样容易导致很多并不需要的东西进行耦合。使得代码阅读更加困难。

The third problem with if statements is that you have to simulate execution in your own head. You must beome a mini-computer. That’s taking away from your mental energy, energy that would be better spent thinking about solving the problem, rather than how the intracate code branches weave together.

第三个问题。在这样的状态下你不得不去用你的头脑去模拟执行。
你变成了一个小型电脑,大脑是用来解决问题的而不是用来执行代码的。

I want to get to the point of telling you patterns we can do instead, but first a word of warning.

我想让你知道新模式的重点。但是先警告一下。

Moderation in all things, especially moderation

任何事情都有一个度

If statements usually make your code more complicated. But we don’t want to outright ban them. I’ve seen some pretty heinous code created with the goal of removing all traces of if statements. We want to avoid falling into that trap.

如果这个状态让你的代码更加复杂。我们并不建议你完全这样用。
我曾经看到用这种方式及其错误编码方式。移除所有的if判断。我们都想去避免陷入这种圈套。

For each pattern we’ll read about I’m going to give you a tolerance value for when to use it.

对于每个我们将要读到的模式我都会给你一个有限的建议。

A single if statement which isn’t duplicated anywhere else is probably fine. It’s when you have duplicated if statements that you want your spider sense to be tingling.

一个单独的if语句块并不是拷到任何地方可能都是合适的。一旦拷贝了可能意义就不对了。

At the outside of your code base, where you talk to the dangerous outside world, you are going to want to validate incoming responses and change your beahaviour accordingly. But inside our own codebases, where we behind those trusted gatekeepers, I think we have a great opportunity to use simple, richer and more powerful alternatives.