拥有一个返回另一个函数的反函数的函数值得付出努力吗?

发布于 2024-07-18 01:50:58 字数 630 浏览 10 评论 0原文

我最近在我们的内部 javascript 库中添加了一个 HasValue 函数:

function HasValue(item) {
    return (item !== undefined && item !== null);
}

在与同事交谈期间,我们提出了添加另一个函数的想法,该函数基本上只是相反:也许 HasNoValue 或 IsNothing 如果我们最终这样做,我们会:

function HasNoValue(item) {
    return (item === undefined || item === null);
}
function HasValue(item) {
    return !HasNoValue(item);
}

但是,我们不确定同时拥有两者或 HasValue 是否更具可读性。 哪个更具可读性/更受欢迎?

答:

if (HasValue(x) && !HasValue(y))

乙:

if (HasValue(x) && HasNoValue(y))

I have recently added a HasValue function to our internal javascript library:

function HasValue(item) {
    return (item !== undefined && item !== null);
}

A during a convorsation with a coworker, we came up with the idea of also adding another function that would basically just be the inverse: perhaps HasNoValue, or IsNothing
If we ended up doing that we would have:

function HasNoValue(item) {
    return (item === undefined || item === null);
}
function HasValue(item) {
    return !HasNoValue(item);
}

However, we're not sure whether it is more readable to have both, or HasValue.
Which is more readable/preferred?

A:

if (HasValue(x) && !HasValue(y))

B:

if (HasValue(x) && HasNoValue(y))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

故事和酒 2024-07-25 01:50:58

比起B,我更喜欢A。“!” 是所有人都应该理解的编程习惯。

I vastly prefer A to B. "!" is a programming idiom that should be understood by all.

千纸鹤 2024-07-25 01:50:58

如果 !HasValue(y)HasNoValue(y) 保证在 y 的整个输入范围内逻辑上等效,那么我会极大地更喜欢 !HasValue(y)

我什至会犹豫是否要拥有一个名为HasNoValue(y)的函数,因为不可避免地会有人写!HasNoValue(y)

If !HasValue(y) and HasNoValue(y) are guaranteed to be logically equivalent over the entire input range of y, then I would greatly prefer !HasValue(y).

I would hesitate to even have a function named HasNoValue(y) because inevitably someone will write !HasNoValue(y).

如梦 2024-07-25 01:50:58

到目前为止我投的是“A”票。

与易于理解和可读的“!”相比,为每个布尔返回函数执行此操作的额外维护负担是不值得的,事实上,我相信“B”实际上不太可读,因为很容易错过名字中间的“否”。

I'm voting "A" by far.

The additional maintenance burden of doing this for each and any boolean return function isn't worth it versus the well-understood and quite readable "!", and in fact I believe "B" is actually less readable, since it's so easy to miss the "No" in the middle of the name.

紧拥背影 2024-07-25 01:50:58

我知道我会很孤独地持有这种观点,如果我在一个合作项目中面临这种选择,我肯定会选择 A,因为很明显这是正确的选择。是的,但我不得不说,我确实欣赏选项 B 的冗长。文字比符号更容易阅读和理解,即使它是像我们心爱的感叹号一样平凡的东西。

特别是现在 IDE 的智能感知比以前好得多,我通常倾向于选择比以前更加冗长的命名方式。 十分之九,可读性胜过微小的性能差异,毫无疑问。

I know I'll be quite alone with this opinion and if I'd be faced with this choise in a collaborative project, I'd surely go with A, since it's quite obvious it's the right thing to do, but I have to say I do appreciate the verbosity of option B. Words are just infinitely easier to read and understand than symbolics, even if it's something as mundane as our beloved ol' exclamation point.

Especially now that IDE's have so much better intellisense than before, I usually tend to opt for far more verbosity than before with all naming. 9 times out of 10, readability trumps small performance differences, hands down.

简单爱 2024-07-25 01:50:58

只是为了减少代码行并且因为你的函数返回一个布尔值,我会说使用方法 A。如果你必须担心可读性,你可以随时尝试:

if ( HasValue(x) && !(HasValue(y)) )

Just for the sake of having less lines of code and because your function returns a Boolean, I'd say to go with method A. If you have to worry about readability, you can always try:

if ( HasValue(x) && !(HasValue(y)) )
未蓝澄海的烟 2024-07-25 01:50:58

我想说选项A更清楚,你知道它的意思。

I would say option A is clearer, you know exactly what it means.

恬淡成诗 2024-07-25 01:50:58

我会坚持选择 A,但那就是我。

I would stick with option A but thats just me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文