今天审查年轻人的代码,发现有重复的地方
struct All {
std::unique_ptr<SinkSource> a;
std::unique_ptr<SinkSource> b;
};
All disk;
bool result = Setup("/this is a very long path/003/file", &disk.a);
if (!result) {
LOG<<"This is a fatal error";
return -1;
}
bool result = Setup("/this is a very long path/004/file", &disk.b);
if (!result) {
LOG<<"This is a fatal error";
return -1;
}
Process(std::move(disk.a), std::move(disk.b));
我们以后会逐渐增加SinkSource,一共有10到12个。我就说,不能用给一个循环吗?结果上次那个家伙又说,不用循环没事。最多改进是把整个struct传进去,而不是像现在这样一个一个参数,因为后来会有10到12个。
我就写了大概
enum Disks {
a, b, // c, d, .....
end
};
std::unique_ptr<SinkSource> all[end]; //也可以使用vector
std::map<Disks, std::string> mapping{
{a, "/this is a very long path/003/file"},
{b, "/this is a very long path/004/file"},
};
for(const auto& each: mapping) {
bool result = Setup(each.second, &all[each.first]);
if ...
}
结果这家伙说,用struct会得到好处,编辑器的自动补齐和编译期间的检查,还有用了数组会导致找到那个具体的单元会增加时间。他说他建议是把struct本身传入process,而不是把其中的每一个都传进去。
问题是我也是传整个数组,而不是一个一个啊,enum也是编译期间检查的啊,还有使用的时候,all[a]也是直接可以找到啊,因为a是个常数啊。编译器会那么傻吗?
我这个方法不好吗?为什么这个家伙看不到?
--
FROM 98.42.143.*