how could I reduce the cyclomatic complexity?

后端 未结 5 2158
感情败类
感情败类 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:29

    I would prefer a simple and less nested code like below:

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

提交回复
热议问题