“空"有简洁的反义词吗? [英] Is there a concise opposite of "empty"?

查看:24
本文介绍了“空"有简洁的反义词吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

字符串类的接口通常具有名为 IsEmpty 的方法(VCL) 或 empty (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...

推荐答案

在大多数情况下,您可以颠倒 ifelse 的顺序来清理代码:

In most cases you can reverse the order of the ifand 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆