是否通过设计检查真正的明显性? [英] Is checking for true explicity bad by design?

查看:82
本文介绍了是否通过设计检查真正的明显性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

明确检查布尔值是否被认为是错误的。如果(成功)那么做一个简单的会不会更好?

Is it considered bad to explicitly check for the boolean true. Would it be better to do a simple if(success) ?

我看过关于如何做的各种笑话 if(someBoolean === true)是强类型语言中的可怕代码,但在弱类型语言中它也被认为是坏的吗?

I've seen various jokes made about how if (someBoolean === true) is horrible code in a strongly typed language but is it also considered bad in weakly typed languages?

这适用于在if语句中输入强制类型的任何弱类型语言。

This would apply for any weakly typed language that does type coercion on an if statement.

具体示例如下:

var onSuccess = function (JSONfromServer) {
    // explicitly check for the boolean value `true`
    if (JSONfromServer === true) {
         // do some things
    }
}

// pass it to an ajax as a callback
doSomeAjax(onSuccess);

在此特定case成功变量是从服务器返回的任何有效JSON。所以它可能是任何东西。如果它的布尔值为true则成功发生。如果它是一些错误处理对象,那么它将被处理。如果它是其他东西那么它可能会被安静地处理。

In this particular case the success variable is any valid JSON returned from a server. So it could be anything. if its the boolean true then a success happened. If it's some error handling object then it will be handled. If it's something else then it will probably be handled quietly.

问题是让服务器返回 true 作为JSON并检查处理动作成功的情况的好方法。

The question was is getting the server to return true as JSON and checking for a good way to handle the case where the action succeeded.

我想避免特定于JavaScript&但AJAX。

I wanted to avoid being specific to JavaScript & AJAX though.

推荐答案

我自己也有两种想法。

在一个方面,您发布的代码示例很好,因为Javascript处理类型强制的方式。一个简单的 if(成功)只要成功 truthy 就会输入if块 - 例如一个非空字符串就可以了。三等于保证成功确实是布尔值 true ,这是一个比你得到更强的保证使用较短的版本(可能是你想要的版本)。

In one respect, the code sample you've posted is good, because of the way Javascript handles type coercion. A simple if (success) would enter the if block so long as success was truthy - e.g. a non-empty string would do the case. The triple-equals guarantees that success is indeed the boolean value true, which is a stronger guarantee than you'd get with the shorter version (and probably one that you want).

但是,如果你需要这个 - 也就是你不知道是否 success 将是一个布尔值,或一个字符串,或一个整数 - 我会说这本身就是一个代码味道。无论你如何进行比较,我总是会与一个不可避免地成为布尔值的变量进行比较。在这一点上,使用哪种形式的比较并不重要,因为它们是等价的。事实上,我甚至会引入一个冗余变量,如下所示:

However, if you need this - i.e. you don't know whether success will be a boolean, or a string, or an integer - I'd say that's a code smell in itself. Regardless of how you perform the comparison, I'd always compare with a variable that's going to unavoidably be a boolean; at which point, it doesn't matter which form of comparison is used as they'd be equivalent. In fact, I'd even introduce a "redundant" variable like so:

var successCount = items.size(); // or some other way to get an integer
var success = successCount > 0;
if (success) {
   ...
}

所以,嗯,是的。我认为任何人都不能在比较中抱怨(明确)使用 === 因为它的功能差异。但出于同样的原因,如果你明显使用布尔成功标志,那么我认为不应该有人抱怨短款式。

So, erm, yeah. I don't think anyone could really complain about the (explicit) use of === in the comparison because of its functional difference. But by the same token, if you're clearly using boolean success flags then I don't think someone should complain about the short style either.

(关于你的第一句话,我认为在动态类型语言中明确检查布尔值为true是不是很糟糕,如果那个值实际上是你想要的那样。这只是静态时多余的输入已经将变量约束为布尔值。)

(Regarding your first sentence though, I don't think it's bad to explicitly check for the boolean value true in a dynamically typed language, if that value is actually what you want. It's only superfluous when static typing already constrains the variable to be a boolean.)

这篇关于是否通过设计检查真正的明显性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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