If-less 编程(基本上没有条件) [英] If-less programming (basically without conditionals)

查看:14
本文介绍了If-less 编程(基本上没有条件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一位同事告诉我,他曾经在一家公司工作,该公司的政策是在代码中永远不要有条件(if"和switch"语句),并且他们让所有的决定都在代码中使用多态和(我猜)其他一些面向对象原则来完成.

I've had a colleague that told me he once worked for a company that had as a policy to never have conditionals ("if" and "switch" statements) in the code and that they let all the decisions in the code be done using polymorphism and (I'm guessing) some other OO principles.

有点理解这背后的原因,代码更干,更容易更新,但我正在寻找对这个概念更深入的解释.或者它可能是更通用的设计方法的一部分.

I sort of understand the reasoning behind this, of having code that is more DRY and easier to update, but I'm looking for a more in-depth explanation of this concept. Or maybe it's part of a more general design approach.

如果有人对此有任何资源,或者愿意解释甚至有更多与此相关的术语,我可以用它来找到更多答案,我将非常感激.

If anyone has any resources for this or would be willing to explain or even have some more terms related to this I can use to find more answers I'd be much obliged.

我发现一个关于SO的问题,这有点相关但我对 C++ 不熟悉,所以我不太了解那里的答案.

I found one question on SO that was kind of related but I'm unfamiliar with C++ so I don't understand too much of the answers there.

(顺便说一句,我不是面向对象的大师,但我可以做到)

(I'm no OO guru btw but I can manage)

我最精通 PHP,然后是 Python,所以我更喜欢使用这些语言的信息.

I'm most proficient in PHP, and after that Python so I'd prefer info that uses those languages.

更新:我会向我的同事询问更多关于他的确切含义的信息.

Update: I'll ask my colleague for more info on what he meant exactly.

2015 年更新:经过多年的编程经验后,我现在看到此政策的目的可能是防止程序员通过在某些地方添加条件(if 语句)以随意的方式添加功能.扩展软件的更好方法是使用 开放/封闭原则",其中软件是使用继承和多态进行扩展.我强烈怀疑该政策是否对所有条件都非常严格,因为完全没有它们有点困难.

Update 2015: after some more years of experience in programming I see now that the aim of this policy was probably to prevent programmers from adding functionality in a haphazard way by just adding conditionals (if statements) in certain places. A better way to extend software is to use the "Open/Closed principle" where software is extended by using inheritance and polymorphism. I strongly doubt whether the policy was super strict on all conditionals as it's kinda hard to go completely without them.

推荐答案

上有一些资源Anti-IF 运动网站,例如 这篇文章.

我相信这是一个程度的问题.条件并不总是不好,但它们可能(并且经常)被滥用.

I believe it's a matter of degree. Conditionals aren't always bad, but they can be (and frequently are) abused.

其他想法(一天后)

重构:改进现有代码的设计是关于这个主题(以及许多其他主题)的一个很好的参考.它涵盖了用多态替换条件.网站上还有一个新的,用访问者替换条件.

Refactoring: Improving the Design of Existing Code is a good reference on this subject (and many others). It covers Replace Conditional with Polymorphism. There's also a new one, Replace Conditional with Visitor, on the web site.

我重视简单性和 单一责任,而不是删除所有 if 语句.这三个目标经常重合.支持圈复杂度指标的静态分析工具可以快速指出带有嵌套或串行条件的代码.if 语句可能会保留重构后的状态,但可以分解为更小的方法和/或多个类.

I value simplicity and single responsibility over removing all if statements. These three goals often coincide. Static analysis tools that support the cyclomatic complexity metric can quickly point out code with nested or serial conditionals. The if statements may remain post-refactoring, but could be broken out into smaller methods and/or multiple classes.

更新:Michael Feathers 在无条件编程.

Update: Michael Feathers wrote an article on Unconditional Programming.

这是一个热门话题:Phil Haack 在 IF 语句的死亡

This is a popular topic: Phil Haack on Death to the IF statement!

这篇关于If-less 编程(基本上没有条件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆