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?
Edit: 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 not negated:
if (!text.IsEmpty()) { /* ... */} // read: if not text is empty ...
...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...
!
character do you want it? I would suggest the use of local variables to simplify reading the negated expressions.bool hasStart = !start.empty();
then the logic becomes easier to read:if (hasStart || hasEnd) { ...
if
's to be on positiveempty
tests, and still eliminate the outermost one.!foo.empty()
then you have a much bigger problem. Look around, its used everywhere and everyone understands it well.not
is clearly harder to overlook...and
,not
andor
operators provided by C++, instead of just using the more outstanding and equally understandable operators&&
,||
and!
. See it like this: When ppl use the lingual forms, the whole boolean code gets less structured, because there are only words and no more punctuaction; and then, anot
is less outstanding. Just like long phrases without any punctuation are hard to read to many people out there in the world and probably space creatures and also it probably has historical reasons that punctuati...