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

问题描述

我有一位同事告诉我,他曾经为一个公司工作,他们在代码中不具有条件(if和switch语句)的政策,并且他们让代码中的所有决定使用多态和(我猜)一些其他OO原则来完成。



这更干脆,更容易更新,但我正在寻找一个更深入的解释这个概念。或者也许它是更通用的设计方法的一部分。



如果任何人有任何资源为此或愿意解释甚至有更多的条款与此相关我可以用于找到更多的答案我很有必要。



我发现,这是一个相关的,但我不熟悉C ++,所以我不明白太多的答案。



(我没有OO大师btw但我可以管理)



我最熟练的PHP,之后Python我更喜欢使用这些语言的信息。



更新:我会问我的同事有关他的意思的更多信息。


更新2015年:经过多年的编程经验,现在我看到这个政策的目的可能是为了防止程序员通过添加条件来以随意的方式添加功能(if语句)在某些地方。扩展软件的更好方法是使用,其中软件通过继承和多态性。我非常怀疑这个政策是否对所有条件都是严格的,因为如果没有他们,这个政策很难完全完成。

解决方案

网站上的资源,例如。



我相信这是一个学位问题条件并不总是坏的,但可能(并经常被滥用)。



其他想法(一天后)



。在网站上还有一个新的。



我重视简单性,并删除所有如果语句。这三个目标经常重合。支持度量的静态分析工具可以使用嵌套或串行条件快速指出代码。 如果语句可能仍然是后重构,但可以分解成较小的方法和/或多个类。



更新: Michael Feathers写了一篇关于。



这是一个热门话题:Phil Haack on !


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.

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)

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.

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.

解决方案

There are some resources on the Anti-IF Campaign site, such as this article.

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

Additional thoughts (one day later)

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.

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.

Update: Michael Feathers wrote an article on Unconditional Programming.

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

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

08-28 20:01