是否有与“空"相对的简明含义? [英] Is there a concise opposite of "empty"?
问题描述
字符串类的接口通常具有名为 IsEmpty
的方法( STL ).这是绝对合理的,因为这是特例,但是使用这些方法的代码通常必须否定该谓词,这会导致光学(甚至是心理上)开销" (感叹号不是很明显,尤其是在圆括号后面).例如,查看以下(简化的)代码:
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;
}
它具有四个否定,因为空白的情况就是要跳过的情况.在设计界面时,我经常观察到这种否定,这不是一个大问题,但很烦人.我只希望支持编写易于理解的代码.希望您能理解我的观点.
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?
阅读一些评论后,我认为有必要说一下原始代码使用了VCL的 System :: AnsiString
类.此类提供了一个 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 ...
...如果文本不为空,则代替 .我认为字面的 is
最好留给读者幻想,以使否定也能很好地发挥作用.好的,也许不是一个普遍的问题...
...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...
推荐答案
在大多数情况下,您可以颠倒 if
和 else
的顺序以清理代码:
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;
}
为了最终的紧凑性(尽管我更喜欢以前的版本,因为它可读性强):
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)) {
...
}
这篇关于是否有与“空"相对的简明含义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!