代码大脑“预告片” ——但并非如此

发布于 2024-10-20 03:13:12 字数 313 浏览 5 评论 0原文

我只是很好奇你们对此有何看法。我听到办公室里流传着一堆答案,我想看看你们是否能有更好的答案。

问题:

您有下面概述的两个函数:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

痕至 2024-10-27 03:13:12

并非所有语言都支持这种方法,并且传递函数的语法可能会有所不同,但概念是:

function one()
{
    refactored(B);
}

function two()
{
    refactored(D);
}

function refactored(middleMan)
{
    A();
    middleMan();
    C();
}

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:

function one()
{
    refactored(B);
}

function two()
{
    refactored(D);
}

function refactored(middleMan)
{
    A();
    middleMan();
    C();
}
滥情稳全场 2024-10-27 03:13:12

这里没有重复的代码。看起来不错。

There is no code duplication here. It looks fine.

三生一梦 2024-10-27 03:13:12

调用的每个方法都会更改其他函数需要使用的变量。

我将首先重构整个类以使用正确的 OOP。

Each of the methods called changes variables that the other functions need to use.

I would start by refactoring the entire class to use proper OOP.

北陌 2024-10-27 03:13:12

有多种方法可以重构该代码;我将使用哪个取决于特定的应用程序,因为这可能意味着我需要在更高的级别重新考虑事物,例如重新定义类,或者最坏的情况是审查整个应用程序设计,因为重复意味着我错过了一些关键关系。

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.

久随 2024-10-27 03:13:12

如果您的函数 one() 和 Two() 实际上是三行函数,如示例中所示,我不会重写任何内容。你会失去可读性,并使代码更难被下一个人理解。

如果对 A() 和 C() 的调用实际上是更大的代码块......
- 使用抽象方法 X() 和具体方法定义基类
函数任意()
{
一个();
X();
C();
定义

  • 一个类 One,其中 X() 由 B() 实现
  • 定义一个类 Two,其中 X() 由 D() 实现

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();
}

  • define a class One where X() is implemented by B()
  • define a class Two where X() is implemented by D()
友欢 2024-10-27 03:13:12

这是一种选择。

function (triggerA, triggerB, triggerC, triggerD)
{
     A(triggerA);
     B(triggerB);
     C(triggerC);
     D(triggerD);
}

这样,您只需调用一个函数即可完成所有操作,并跳过您不需要/想要执行的任何操作。

Here's one option.

function (triggerA, triggerB, triggerC, triggerD)
{
     A(triggerA);
     B(triggerB);
     C(triggerC);
     D(triggerD);
}

This way you're only calling one function to do it all, and skips whatever you don't need/want to do.

人事已非 2024-10-27 03:13:12

如果你有闭包、lambda 等可用,你可以写

function one()
{
    three(B)
}

function two()
{
    three(D);
}

function three(middle) 
{
    A();
    middle();
    C();
}

If you have closures, lambdas etc. available, you could write

function one()
{
    three(B)
}

function two()
{
    three(D);
}

function three(middle) 
{
    A();
    middle();
    C();
}
滥情稳全场 2024-10-27 03:13:12

可以(但可能不应该)创建一个类,其中A()是构造函数,C() 是析构函数,并且 one()two() 是调用 B()D( ) 分别。

我说你可能不应该这样做,因为 OOP 应该用于编写有意义的代码,而不是出于晦涩的优化原因。

You could (but probably shouldn't) make a class where A() is the constructor and C() is the destructor, and have one() and two() be methods of the class calling B() and D() respectively.

I said you probably shouldn't because OOP should be used to write code that makes sense and not for obscure optimization reasons.

云之铃。 2024-10-27 03:13:12

在 C++ 中,如果上下文有意义,通常使用 RAII 来完成...这种模式通常是 A() = 某个 init 函数,C() = 某个 de-init 函数。通常还有一个关联的上下文正在被初始化或销毁。

   class bar
    {
        bar() {
            A();
        }

        ~bar() {
            C();
        }
    };

    void one() 
    {
        bar barvar;
        B();
    }

    void two()
    {
        bar barvar;
        D();
    }

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.

   class bar
    {
        bar() {
            A();
        }

        ~bar() {
            C();
        }
    };

    void one() 
    {
        bar barvar;
        B();
    }

    void two()
    {
        bar barvar;
        D();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文