Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 用布尔值乘法安全(更好)吗?_Javascript_Boolean - Fatal编程技术网

Javascript 用布尔值乘法安全(更好)吗?

Javascript 用布尔值乘法安全(更好)吗?,javascript,boolean,Javascript,Boolean,我有一段代码包含很多if和else if。我刚才想,在乘法中,true的值为1,false的值为0。替换以下内容是否安全且易于阅读(因为它较短): if(!this._isFetched('studentInfoFetched')) { tempAddedTime += 1; estimatedTimePerStudent += 0.04 + 0.2; } if(formInputValues.student_expiration){ tempA

我有一段代码包含很多if和else if。我刚才想,在乘法中,true的值为1,false的值为0。替换以下内容是否安全且易于阅读(因为它较短):

if(!this._isFetched('studentInfoFetched')) { 
        tempAddedTime += 1;
        estimatedTimePerStudent += 0.04 + 0.2;
}
if(formInputValues.student_expiration){
        tempAddedTime += (!this._isFetched('studentExpirationFetched'))? 14 : 0;
        estimatedTimePerStudent += 1; 
}  
用于:


注意:\ isFetched返回布尔值。这只是一个例子,对于其他情况,我有更多的if,这样可以节省更多的行。

不,
if
s版本更好

原因:

  • 它更具可读性:表达式更短,行也不太长。例如,我在屏幕上看到一个水平滚动条,显示乘法表达式,而我不必在
    if
    -snippet:)中滚动


  • 它更快,因为你避免了乘法运算

  • 它甚至更快,因为您避免调用
    this.\u isFetched('studentInfoFetched')
    两次

  • if
    s在语义上定义了程序流,而乘法在语义上是一个数学表达式,在这种情况下用于伪造程序流。使用
    if
    s,语句按条件分组,您可以一目了然地看到如果满足某个条件会发生什么

然后,考虑到必须创建另外两个<代码>如果子句。乘法运算将变得完全无法维持

代码应该易于阅读、快速理解和快速更改。 比在下班时间发表的评论更好的是清晰的变量名,尽管它们有助于一般性地说明原因。名称常量(即
0.04+0.2
??)和名称表达式,以便在上下文中简洁明了(还避免了不必要的函数调用)

我通常喜欢将布尔值乘以开关,尽管在这种情况下,if可能更容易阅读、理解和更改

tempAddedTime +=
    !infoFetched* 1 +
    (hasExpired && !expirationFetched)* expirationFetchTime

estimatedTimePerStudent +=
    !infoFetched* fetchTime +
    hasExpired* 1

不是最好的例子,如果我有权/知道它打算做什么/来源,可能会有很大的不同。我个人会离开它,选择第一个,因为它更容易更新,而且我发现格式更容易阅读,因为水平长度更少。
可能安全吗(取决于你对“安全”的定义),
和更容易阅读吗?
肯定不安全。第二个很混乱,因为所有人都出去了。。。也许要留出更多的空间,但总的来说,要避免变得聪明。这样做不是一个好主意。解释器/编译器应该为您进行必要的优化。您所做的只是使代码难以阅读。另外,这个问题可能更适合。另外,您的第二个示例调用
this.\u isFetched('studentInfoFetched')
两次,(取决于调用
\u isFetched
所涉及的内容)效率较低。假设两次返回的值都相同!谢谢你的详细解释。我完全同意,但我自己想不出这些。我想我的问题是有300行If来改变3个变量的值。但是我想如果是“它更快,因为你避免了乘法运算”,这是不正确的。谷歌无分支Programming@Niton,请先创建并共享一个基准测试,然后再声明多重乘法(包括浮点数)比两个
快,如果
s.@AlexShesterov一般来说branchless可以很好地提高性能,但它确实非常依赖于用例:语言、使用的编译器、,用过的处理器。你可以在引擎盖下看一看,看看编译器产生了什么,但一般来说,无分支编程会带来更好的性能,我没有做基准测试,但其他人做了。同样,在这种情况下,使用if可能更快(而不是更多)。
// Estimate process time
const infoFetched = this._isFetched('studentInfoFetched')
const infoFetchTime = 0.04 + 0.2
const canExpire = formInputValues.student_expiration
const expirationFetched = this._isFetched('studentExpirationFetched')
const expirationFetchTime = 14

if (!infoFetched) tempAddedTime += 1
if (hasExpired && !expirationFetched) tempAddedTime += expirationFetchTime

if (!infoFetched) estimatedTimePerStudent += fetchTime
if (hasExpired) estimatedTimePerStudent += 1
tempAddedTime +=
    !infoFetched* 1 +
    (hasExpired && !expirationFetched)* expirationFetchTime

estimatedTimePerStudent +=
    !infoFetched* fetchTime +
    hasExpired* 1