代码大脑“预告片” ——但并非如此
我只是很好奇你们对此有何看法。我听到办公室里流传着一堆答案,我想看看你们是否能有更好的答案。
问题:
您有下面概述的两个函数:
function one()
{
A();
B();
C();
}
function two()
{
A();
D();
C();
}
您将如何重写它(任何重要的东西,您可以创建类、变量、其他方法,任何东西),以减少代码重复?
每个调用的方法都会更改其他函数需要使用的变量。方法 A() B() 和 C() 已经定义。
I'm just curious to see what you guys think about this. I heard a bunch of answers passed around the office and I want to see if you guys can have possibly a better one.
Question:
You have two functions outlined below:
function one()
{
A();
B();
C();
}
function two()
{
A();
D();
C();
}
How would you re-write this (anything counts, you could create classes, variables, other methods, anything), to reduce code duplication?
Each of the methods called changes variables that the other functions need to use. Methods A() B() and C() are already defined.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
并非所有语言都支持这种方法,并且传递函数的语法可能会有所不同,但概念是:
Not all languages will support this approach, and the syntax of passing a function may vary between those that do, but the concept would be:
这里没有重复的代码。看起来不错。
There is no code duplication here. It looks fine.
我将首先重构整个类以使用正确的 OOP。
I would start by refactoring the entire class to use proper OOP.
有多种方法可以重构该代码;我将使用哪个取决于特定的应用程序,因为这可能意味着我需要在更高的级别重新考虑事物,例如重新定义类,或者最坏的情况是审查整个应用程序设计,因为重复意味着我错过了一些关键关系。
There are a number of ways to refactor that code; which I would use depends on the specific application, as it may mean that I need to reconsider things at a higher level, e.g. redefine classes, or at worst review the entire application design because the duplication means I missed some key relationship.
如果您的函数 one() 和 Two() 实际上是三行函数,如示例中所示,我不会重写任何内容。你会失去可读性,并使代码更难被下一个人理解。
如果对 A() 和 C() 的调用实际上是更大的代码块......
- 使用抽象方法 X() 和具体方法定义基类
函数任意()
{
一个();
X();
C();
定义
If your functions one() and two() are really three-liners as in the example, I wouldn't rewrite anything. You would loose readability and make the code much harder to understand for the next guy.
If the calls to A() and C() are actually larger blocks of code...
- define a base class with abstract method X() and a concrete
function any()
{
A();
X();
C();
}
这是一种选择。
这样,您只需调用一个函数即可完成所有操作,并跳过您不需要/想要执行的任何操作。
Here's one option.
This way you're only calling one function to do it all, and skips whatever you don't need/want to do.
如果你有闭包、lambda 等可用,你可以写
If you have closures, lambdas etc. available, you could write
您可以(但可能不应该)创建一个类,其中
A()
是构造函数,C()
是析构函数,并且one()
和two()
是调用B()
和D( )
分别。我说你可能不应该这样做,因为 OOP 应该用于编写有意义的代码,而不是出于晦涩的优化原因。
You could (but probably shouldn't) make a class where
A()
is the constructor andC()
is the destructor, and haveone()
andtwo()
be methods of the class callingB()
andD()
respectively.I said you probably shouldn't because OOP should be used to write code that makes sense and not for obscure optimization reasons.
在 C++ 中,如果上下文有意义,通常使用 RAII 来完成...这种模式通常是 A() = 某个 init 函数,C() = 某个 de-init 函数。通常还有一个关联的上下文正在被初始化或销毁。
In C++ this is usually accomplished with RAII if the context makes sense... this pattern is usually A() = some init function, C() = some de-init function. There's usually also a context associated that's being initialized or destroyed as well.