简体   繁体   English

布尔值,英语否定式,完全摆脱布尔值

[英]boolean, english negation and simply getting rid of the boolean

Very often when you code you have a flag indicating two possibilities, like this: 很多时候,当您编写代码时,会有一个标志指示两种可能性,如下所示:

CACHING_AUTHORIZED
CACHING_UNAUTHORIZED

or 要么

CACHING_ON
CACHING_OFF

If you then use a boolean to store this, very often it just "reads weird": 如果您随后使用布尔值来存储它,通常它只是“读取异常”:

CACHING_ON = false;
CACHING_OFF = true;

or 要么

CACHING_ON = true;
CACHING_OFF = false;

One of the possibility is to write: 一种可能是编写:

CACHING

And then simply code using: 然后只需使用以下代码即可:

if ( CACHING )

or 要么

if ( !CACHING )

but sometimes you really do want both the 'ON' and 'OFF' forms. 但是有时候您确实希望同时使用“ ON”和“ OFF”两种形式。

The thing is: it doesn't change much which one you pick, it simply doesn't make much sense, no matter how you turn it. 问题是:无论您选择哪一个,它并没有太大变化,无论您如何转动它都没有多大意义。

It is not entirely dissimilar to the "getter" problem for boolean, which is certainly subject to a lot of heated debate (should we use isConditionTrue or getIsCondition or isCondition or getCondition or simply condition or conditionTrue etc.: I'm being a bit facetious but the point stands and there have been very long debates on this and the topic certainly ain't close). 它与boolean的“ getter”问题并不完全不同,后者肯定会引起很多激烈的争论(我们应该使用isConditionTruegetIsCondition还是isConditiongetCondition还是简单地使用conditionconditionTrue等。:我有点滑稽但问题的根源在于,对此已经进行了很长时间的辩论,而且这个话题肯定还没有结束。

My question is different: seen that neither 我的问题是不同的:看到没有

CACHING_ON = false;
CACHING_OFF = true;

nor 也不

CACHING_ON = true;
CACHING_OFF = false;

make sense, shouldn't we simply get rid of the boolean and do: 有意义,我们不应该简单地摆脱布尔值而去做吗:

CACHING_ON = 1;
CACHING_OFF = 2;

Sure, you're losing the boolean but at least '1' and '2' cannot bring any confusion. 当然,您正在丢失布尔值,但至少'1'和'2' 不会带来任何混乱。

My point is: using a boolean and true/false is very often confusing because we're mixing two different things: english and logic and they simply, well, don't mix at all. 我的观点是:使用布尔值和true / false经常会造成混淆,因为我们混用了两种不同的东西:英语和逻辑,它们根本就完全不混在一起。

Is it bad to use an int here even tough there are only two possibilites? 在这里只有两个可能的情况下使用int很难吗?

Your method makes it unclear the value is a boolean. 您的方法不清楚值是否为布尔值。 I feel like I should be able to assign the value 3. 我觉得我应该能够赋值3。

I don't really get your point. 我真的不明白你的意思。 If you have 2 booleans you're representing 4 possible states so you shouldn't really be naming the variables in a way that suggests there are only 2 options. 如果您有2个布尔值,则表示4个可能的状态,因此您实际上不应以暗示只有2个选项的方式来命名变量。 Maybe you should be using an enum and constants or flags instead. 也许您应该使用枚举和常量或标志代替。

CACHING = ON
CACHING = OFF
CACHING = EITHER
CACHING = UNKNOWN
CACHING = UNKNOWABLE
CACHING = UNTHINKABLE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM