Multiple conditions in an if clause

后端 未结 9 1059
醉梦人生
醉梦人生 2021-02-02 06:44

If I have an if statement that needs to meet these requirements:

if(cave > 0 && training > 0 && mobility > 0 && sleep > 0)


        
相关标签:
9条回答
  • 2021-02-02 07:27

    You could use an array with .every. This is less DRY, but more verbose:

    var isGreaterThanZero = function(val) {
        return val > 0;
    };
    if([cave, training, mobility, sleep].every(isGreaterThanZero)) {
        // Do Something
    }
    

    The reason I like this is that by using an array, it becomes apparent that you're repeating logic for every variable. Naming the callback in an obvious manner helps future readers understand exactly what that check will achieve. And finally, this gives scope for not just numbers, but any check of any type in the future - with any complexity hidden away from the if statement itself.

    0 讨论(0)
  • 2021-02-02 07:29

    Assuming 32-bit ints.

    if ((cave | training | mobility | sleep) > 0)
    

    If any of the above numbers are negative, the result of the OR will be negative and the requirements aren't met.

    Edit: that's equivalent to if(cave >= 0 && training >= 0 && mobility >= 0 && sleep >= 0) and won't when some of the parameters are 0. The fix is to invert the sign bit:

    if ((~cave | ~training | ~mobility | ~sleep) <= 0)
    

    Some alternative ways which work even for floating-point values

    if (cave*training*mobility*sleep > 0)
    if (Math.sign(cave) * Math.sign(training) * Math.sign(mobility) * Math.sign(sleep) > 0)
    if (Math.sign(cave) | Math.sign(training) | Math.sign(mobility) | Math.sign(sleep) > 0)
    
    0 讨论(0)
  • 2021-02-02 07:30

    As some have stated there is nothing wrong in having multiple simple conditions in an if statement, however I would consider changing the formatting into:

    if ( cave > 0
       && training > 0
       && mobility > 0
       && sleep > 0 )
    

    Alternatively I would change the from using these variables as integers, into bool variables, i.e. isCave, hasTraining, or similar, and then set the proper bool closer to where your code defines the different properties (Edit: And possibly return early if it is false, to prevent further unneccessary calculations). This would simplify your if statement to the latter in the next code block, which in addition shows a variant which can be used if the conditions becomes slightly more complex or you would like to ease the reading of the if statement:

    var isCave =  cave > 0; # What does cave > 0 mean?
    var hasTraining = training > 0;
    var isMobile = mobility > 0;
    var isNotSleeping = sleep > 0; # What does sleep > 0 indicate? Unclear
    
    if (isCave && hasTraining && isMobile && isNotSleeping ) {
       // Do your thing
    }
    

    In other words, the multiple conditions in your if statement is not your biggest code smell, I would shift my focus to giving your variables better names clearly indicating what the value indicates. This would improve reading and understanding of your code, way more than some strange syntax to avoid multiple if conditions.

    0 讨论(0)
提交回复
热议问题