C++ 概括这些代码行?

C++ 概括这些代码行?,c++,c++11,c++14,redundancy,C++,C++11,C++14,Redundancy,编程时,我碰到了一堵墙,里面有一些代码。看起来是这样的: 这就是问题所在。我拍了一张漂亮的截图来减轻我的内疚感。漂亮的颜色并不能弥补可维护性的不足。我几乎不知道如何像这样概括代码 我试过什么? 嗯,考虑第三和第六个参数的周期性。它也和其他论点的周期性相一致。如果我们使用数组,类似这样的东西将允许我们将代码转换成一个有9行的循环。这是一个进步,因为我们下降了66%。然而,这还不够好。最好将其更改为具有1行。这至少会使它更易于维护 这真的是个问题吗? 好吧,让我们这样说吧。上面的代码可能是错误的。

编程时,我碰到了一堵墙,里面有一些代码。看起来是这样的:

这就是问题所在。我拍了一张漂亮的截图来减轻我的内疚感。漂亮的颜色并不能弥补可维护性的不足。我几乎不知道如何像这样概括代码

我试过什么?

嗯,考虑第三和第六个参数的周期性。它也和其他论点的周期性相一致。如果我们使用数组,类似这样的东西将允许我们将代码转换成一个有9行的循环。这是一个进步,因为我们下降了66%。然而,这还不够好。最好将其更改为具有1行。这至少会使它更易于维护

这真的是个问题吗?
好吧,让我们这样说吧。上面的代码可能是错误的。

嗯,分析模式花了一些时间

当然,首先我从截图中获取文本。然后我开始匹配高亮显示以发现模式

  • 您可以看到
    make_cube
    有效地使用了两个
    (x,y,z)
    元组
  • 有三个组或行以相同的
    z
    值结尾
  • 这三个组由以相同的
    (y,z)
    元组结尾的三个子组组成
  • x、y和z值为每个组枚举相同的值对
这使其成为生成循环的“明显”材料。经过大约20分钟的重构后,我的目标是

for (auto&& zs : { tie(rmin_z, imin_z), tie(imin_z, imax_z), tie(imax_z, rmax_z) })
    for (auto&& ys : { tie(rmin_y, imin_y), tie(imin_y, imax_y), tie(imax_y, rmax_y) })
        for (auto&& xs : { tie(rmin_x, imin_x), tie(imin_x, imax_x), tie(imax_x, rmax_x) })
{
    *out++ = make_cube(get<0>(xs), get<0>(ys), get<0>(zs), get<1>(xs), get<1>(ys), get<1>(zs));
}
我们选择连续对:
(rmin,imin),(imin,imax),(imax,rmax)

#include <iostream>
#include <algorithm>
#include <vector>
#include <array>

int main() {
#ifdef NDEBUG
    typedef double T;
    struct coord { T x,y,z; };

    coord rmin { 0,  1,  2 },
          imin { 3,  4,  5 },
          imax { 6,  7,  8 },
          rmax { 9, 10, 11 };
#else
    typedef const char* T;
    struct coord { T x,y,z; };

    coord rmin { "rmin_x", "rmin_y", "rmin_z" },
          imin { "imin_x", "imin_y", "imin_z" },
          imax { "imax_x", "imax_y", "imax_z" },
          rmax { "rmax_x", "rmax_y", "rmax_z" };
#endif
    using namespace std;

    // the source sequence
    coord const sequence[] = { rmin, imin, imax, rmax };

    // we take all consecutive pairs (warning: ignoring the `(rmax, rmin)` closing pair here)
    vector<pair<coord, coord>> pairs;
    transform(begin(sequence), prev(end(sequence)), back_inserter(pairs), [](coord const& it) { return std::make_pair(*(&it+0), *(&it+1)); });

    // Now we build cubes. The `make_cube` interface implied it requires two
    // coordinates to be constructed:
    struct Cube { coord p1, p2; };
    std::array<Cube, 3*3*3> cubes;

    // generate!
    auto out = cubes.begin();
    for (auto zs : pairs) for (auto ys : pairs) for (auto xs : pairs)
        *out++ = Cube { { xs.first.x, ys.first.y, zs.first.z }, { xs.second.x, ys.second.y, zs.second.z } }; 

    // debug print
    for(auto const& c : cubes)
        std::cout << "make_cube(" << c.p1.x << ", " << c.p1.y << ", " << c.p1.z << ", " << c.p2.x << ", " << c.p2.y << ", " << c.p2.z << ")\n";
}

只要等到你开始实现密码学:)这类东西的页面。你已经发现,把它归结到一行将是复杂和不明显的。这样做极不可能值得,也不可能产生“可维护”的代码。我建议坚持这九行。根据给定的代码,我可能会同意@LightnessRacesinOrbit的说法。但是,我不希望这样写(除非生成)。至少在分析和颠倒了这些组合背后的逻辑之后,我总是觉得有义务确保未来的读者不再需要这样做,或者检查它的正确性。当然,您可以将“复杂”生成器放在单元测试中,而不是验证表。十分钟的重构?这个问题三分钟前才发布@Lightness Race Sinorbit你从那以后就没来过休息室7pm@sehe我爱你,瑟赫!谢谢@LightnessRacesinOrbit只是计算了一下,不包括到达的时间。我花了更多的时间(
// we take all consecutive pairs (warning: ignoring the `(rmax, rmin)` closing pair here)
vector<pair<coord, coord>> pairs;
transform(begin(sequence), prev(end(sequence)), back_inserter(pairs), [](coord const& it) { return std::make_pair(*(&it+0), *(&it+1)); });
for (auto zs : pairs) for (auto ys : pairs) for (auto xs : pairs)
    *out++ = Cube { { xs.first.x, ys.first.y, zs.first.z }, { xs.second.x, ys.second.y, zs.second.z } }; 
#include <iostream>
#include <algorithm>
#include <vector>
#include <array>

int main() {
#ifdef NDEBUG
    typedef double T;
    struct coord { T x,y,z; };

    coord rmin { 0,  1,  2 },
          imin { 3,  4,  5 },
          imax { 6,  7,  8 },
          rmax { 9, 10, 11 };
#else
    typedef const char* T;
    struct coord { T x,y,z; };

    coord rmin { "rmin_x", "rmin_y", "rmin_z" },
          imin { "imin_x", "imin_y", "imin_z" },
          imax { "imax_x", "imax_y", "imax_z" },
          rmax { "rmax_x", "rmax_y", "rmax_z" };
#endif
    using namespace std;

    // the source sequence
    coord const sequence[] = { rmin, imin, imax, rmax };

    // we take all consecutive pairs (warning: ignoring the `(rmax, rmin)` closing pair here)
    vector<pair<coord, coord>> pairs;
    transform(begin(sequence), prev(end(sequence)), back_inserter(pairs), [](coord const& it) { return std::make_pair(*(&it+0), *(&it+1)); });

    // Now we build cubes. The `make_cube` interface implied it requires two
    // coordinates to be constructed:
    struct Cube { coord p1, p2; };
    std::array<Cube, 3*3*3> cubes;

    // generate!
    auto out = cubes.begin();
    for (auto zs : pairs) for (auto ys : pairs) for (auto xs : pairs)
        *out++ = Cube { { xs.first.x, ys.first.y, zs.first.z }, { xs.second.x, ys.second.y, zs.second.z } }; 

    // debug print
    for(auto const& c : cubes)
        std::cout << "make_cube(" << c.p1.x << ", " << c.p1.y << ", " << c.p1.z << ", " << c.p2.x << ", " << c.p2.y << ", " << c.p2.z << ")\n";
}
// we take all consecutive pairs (warning: ignoring the `(rmax, rmin)` closing pair here)