问题描述
接口串类通常有一个名为方法的IsEmpty
(的 VCL )或空
()。这是绝对合理的,因为它是一个特例,但使用这些方法往往有code否定这个predicate,这导致了光学(甚至心理)的头顶(感叹号不是很明显,尤其是左括号后)。例如,见这(简体)code:
Interfaces to string classes typically have of method named IsEmpty
(VCL) or empty
(STL). That's absolutely reasonable because it's a special case, but the code that uses these methods often has to negate this predicate, which leads to a "optical (and even psychological) overhead" (the exclamation mark is not very obvious, especially after an opening parenthesis). See for instance this (simplified) code:
/// format an optional time specification for output
std::string fmtTime(const std::string& start, const std::string& end)
{
std::string time;
if (!start.empty() || !end.empty()) {
if (!start.empty() && !end.empty()) {
time = "from "+start+" to "+end;
} else {
if (end.empty()) {
time = "since "+start;
} else {
time = "until "+end;
}
}
}
return time;
}
它的四个否定,因为空箱是那些被跳过。界面设计时,我经常看到这样的否定,也和它的不是一个大问题,但它很烦人。我只希望支持写入易懂,易于阅读code。我希望你会明白我的意思。
It has four negations, because the empty cases are those to be skipped. I often observe this kind of negation, also when designing interfaces, and it's not a big problem but it's annoying. I only wish to support writing understandable and easy-to-read code. I hope you'll understand my point.
也许我只能打带有盲目性:你将如何解决上述问题
Maybe I'm only struck with blindness: How would you solve the above problem?
编辑:阅读一些意见后,我认为这是nessessary地说,原来的code使用的类系统:: AnsiString类型
的VCL。这个类提供了一个的IsEmpty
方法,这是非常具有可读性:
After reading some comments, I think it's nessessary to say that the original code uses the class System::AnsiString
of the VCL. This class provides an IsEmpty
method, which is very readable:
if (text.IsEmpty()) { /* ... */ } // read: if text is empty ...
如果不是否定:
if (!text.IsEmpty()) { /* ... */} // read: if not text is empty ...
...代替的如果text不为空的。我觉得文字是
是最好留给读者的幻想也让否定工作做好。好吧,也许不是一个wides $ P $垫的问题...
...instead of if text is not empty. I think the literal is
was better left to the reader's fantasy to let also the negation work well. Ok, maybe not a widespread problem...
推荐答案
在大多数情况下,你可以扭转的如果
和其他顺序
来清理code:
In most cases you can reverse the order of the if
and the else
to clean up the code:
const std::string fmtTime(const std::string& start, const std::string& end)
{
std::string time;
if (start.empty() && end.empty()) {
return time;
}
if (start.empty() || end.empty()) {
if (end.empty()) {
time = "since "+start;
} else {
time = "until "+end;
}
} else {
time = "from "+start+" to "+end;
}
return time;
}
甚至清洁后一些更多的重构:
Or even cleaner after some more refactoring:
std::string fmtTime(const std::string& start, const std::string& end)
{
if (start.empty() && end.empty()) {
return std::string();
}
if (start.empty()) {
return "until "+end;
}
if (end.empty()) {
return "since "+start;
}
return "from "+start+" to "+end;
}
和为最终紧凑(虽然我preFER的previous版本,它的可读性):
And for the ultimate compactness (although I prefer the previous version, for its readability):
std::string fmtTime(const std::string& start, const std::string& end)
{
return start.empty() && end.empty() ? std::string()
: start.empty() ? "until "+end
: end.empty() ? "since "+start
: "from "+start+" to "+end;
}
另一种可能性是创建一个辅助功能:
Another possibility is to create a helper function:
inline bool non_empty(const std::string &str) {
return !str.empty();
}
if (non_empty(start) || non_empty(end)) {
...
}
这篇关于是否有与的一个QUOT对面简洁是空的"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!