是否值得努力使函数返回另一个函数的反函数?

时间:2022-01-21 23:59:13

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

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

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:

在与同事交流期间,我们想出了另外添加另一个基本上就是反过来的函数的想法:也许是HasNoValue,或者IsNothing如果我们最终做到了这样我们会:

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?

但是,我们不确定同时拥有它们或HasValue是否更具可读性。哪个更具可读性/首选?

A:

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

B:

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

7 个解决方案

#1


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

我非常喜欢A到B.“!”是一个应该被所有人理解的编程习语。

#2


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).

如果!HasValue(y)和HasNoValue(y)在y的整个输入范围内保证在逻辑上是等价的,那么我更喜欢!HasValue(y)。

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

我甚至会犹豫,甚至有一个名为HasNoValue(y)的函数,因为不可避免地有人会写!HasNoValue(y)。

#3


I'm voting "A" by far.

到目前为止,我正在投票“A”。

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.

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

#4


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.

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

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.

特别是现在IDE有比以前更好的智能感知,我通常倾向于选择比以前更加冗长的所有命名。 10次​​中有9次,可读性胜过小的性能差异,落地。

#5


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:

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

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

#6


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

我会说选项A更清楚,你确切地知道它意味着什么。

#7


I would stick with option A but thats just me.

我会坚持使用选项A,但那只是我。

#1


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

我非常喜欢A到B.“!”是一个应该被所有人理解的编程习语。

#2


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).

如果!HasValue(y)和HasNoValue(y)在y的整个输入范围内保证在逻辑上是等价的,那么我更喜欢!HasValue(y)。

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

我甚至会犹豫,甚至有一个名为HasNoValue(y)的函数,因为不可避免地有人会写!HasNoValue(y)。

#3


I'm voting "A" by far.

到目前为止,我正在投票“A”。

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.

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

#4


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.

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

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.

特别是现在IDE有比以前更好的智能感知,我通常倾向于选择比以前更加冗长的所有命名。 10次​​中有9次,可读性胜过小的性能差异,落地。

#5


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:

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

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

#6


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

我会说选项A更清楚,你确切地知道它意味着什么。

#7


I would stick with option A but thats just me.

我会坚持使用选项A,但那只是我。