how could I reduce the cyclomatic complexity?

后端 未结 5 2160
感情败类
感情败类 2021-01-04 01:48

Whenever I lint a piece of code I\'m working on I get the This function\'s cyclomatic complexity is too high. (7). But I\'m a bit confused on how I could rewrit

5条回答
  •  北海茫月
    2021-01-04 02:25

    Well you have only two actions in your code, but much too many conditions. Use a single if-else-statement, and boolean operators in the condition. If that was impossible, you could at least

    • remove the empty lines to get the full logic on one screen page
    • add some comments on what the branches are doing (and why)
    • avoid early returns

    Here's your function simplified:

    var duration = +new Date() - start.time,
        isPastHalf = Number(duration) < 250 && Math.abs(delta.x) > 20 || Math.abs(delta.x) > viewport / 2,
        isFarRight = this.content.getBoundingClientRect().left > viewport / 2, 
        direction = delta.x < 0;
    
    if (!isScrolling) {
        if (isPastHalf) {
            if (direction)
                this.close();
            else {
                if (isFarRight && pulled)
                    this.close();
                else
                    this.open();
            }
        } else {
            if (isFarRight) {
                // Looks like the opposite of `direction`, is it?
                if (this.isEmpty(delta) || delta.x > 0)
                    this.close();
                else
                    this.open();
            } else
                this.close();
        }
    }
    

    and shortened:

    var duration = +new Date() - start.time,
        isPastHalf = Number(duration) < 250 && Math.abs(delta.x) > 20 || Math.abs(delta.x) > viewport / 2,
        isFarRight = this.content.getBoundingClientRect().left > viewport / 2, 
        direction = delta.x < 0,
        undirection = this.isEmpty(delta) || delta.x > 0;
    
    if (!isScrolling) {
        if ( isPastHalf && !  direction && !(isFarRight && pulled)
         || !isPastHalf && !undirection &&  isFarRight )
            this.open();
        else
            this.close();
    }
    

提交回复
热议问题