简体   繁体   English

每次运行时进行检查的更好编码

[英]Better coding of making a check each time function runs

I am trying to eliminate the useless check in a function, needed check variable is assigned at start-up of the app, so I don't want to check for it each time. 我试图消除功能中的无用检查,需要在应用程序启动时分配所需的检查变量,所以我不想每次都检查它。

I have a code like this: 我有这样的代码:

btnClose.addEventListener('click', function(e) {
    window.close()
    if (appSettings.sendAnonymousStats) {
      visitor.event("Application", "App has been closed.").send()
    }
})

This is my first try to optimize it, so now it doesn't need to do a "if" check each time it's called; 这是我第一次尝试对其进行优化,因此现在无需在每次调用时都进行“ if”检查。

let btnCloseEv = appSettings.sendAnonymousStats ? btnClose.addEventListener('click', function(e) {
    window.close()
    visitor.event("Application", "App has been closed.").send()
    }) : btnClose.addEventListener('click', function(e) {
    window.close()
})

I wonder if theoretically there are better ways to achieve what I am trying to achieve? 我想知道理论上是否有更好的方法来实现我要达到的目标?

Removing a single if statement, especially considering it only occurs once per click will not effect running time at all. 删除单个if语句,特别是考虑到每次单击仅发生一次,将完全不会影响运行时间。

However for the purpose of discussion say it was performance critical, like if it was attached to onmousemove, then you can adjust your second approach with a small change to reduce code redundancy. 但是,出于讨论的目的,它说对性能至关重要,就像将它附加到onmousemove一样,那么您可以通过少量更改来调整第二种方法,以减少代码冗余。

let btnCloseEv = btnClose.addEventListener('click',
    appSettings.sendAnonymousStats ?
    function(e) {
        window.close();
        visitor.event("Application", "App has been closed.").send();
    } : function(e) {
        window.close();
    }
)

This works because functions in JS are higher order functions, which means they treated as variables and can be passed around in the same way variables can be. 之所以可行,是因为JS中的函数是高阶函数,这意味着它们被视为变量,并且可以像传递变量一样传递。 For instance this would work if a and b were numbers, or functions, or any other type of variable. 例如,如果a和b是数字,函数或任何其他类型的变量,则此方法有效。

var c = someBoolean ? a : b;


Say each function was a lot bigger and you wanted to use this approach yet things were becoming unreadable, it would be better to name each function and attach them like so: 假设每个函数都大得多,并且您想使用这种方法,但是事情变得不可读了,最好像这样命名每个函数并附加它们:

 function moreComplexFunc(e) { window.close(); visitor.event("Application", "App has been closed.").send(); // More complex code } function simpleFunc(e) { window.close(); } let btnCloseEv = btnClose.addEventListener( 'click', appSettings.sendAnonymousStats ? moreComplexFunc : simpleFunc ) 


Now say that you noticed there was a lot of code duplication in moreComplexFunc and simpleFunc , you could go a step further, and separate the similar code into a 3rd function like so: 现在说,您注意到moreComplexFuncsimpleFunc有很多代码重复,您可以更进一步,并将类似的代码分成第三个函数,如下所示:

 function commonFunc(e) { window.close(); } function func1(e) { commonFunc(e); visitor.event("Application", "App has been closed.").send(); // other code } function func2(e) { commonFunc(e); // other code } let btnCloseEv = btnClose.addEventListener( 'click', appSettings.sendAnonymousStats ? func1 : func2 ) 

The opportunities in a language which supports Higher Order Functions are really endless. 支持高阶功能的语言所带来的机会确实是无穷无尽的。

Just in case the function was a little more complex, another approach would be to simply put it in an additional event handler: 万一函数有些复杂,另一种方法是将其简单地放在一个附加的事件处理程序中:

let btnCloseEv = btnClose.addEventListener('click',
    function(e) {
        window.close();
        /*
        ... more code ...
        */
    }
)

if (appSettings.sendAnonymousStats) {
    btnClose.addEventListener('click',
        function(e) {
             visitor.event("Application", "App has been closed.").send();
        }
}

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

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