How do you handle huge if-conditions?

后端 未结 21 2086
一向
一向 2021-01-31 17:14

It\'s something that\'s bugged me in every language I\'ve used, I have an if statement but the conditional part has so many checks that I have to split it over multiple lines, u

21条回答
  •  耶瑟儿~
    2021-01-31 17:52

    If you happen to be programming in Python, it's a cinch with the built-in all() function applied over the list of your variables (I'll just use Boolean literals here):

    >>> L = [True, True, True, False, True]
    >>> all(L) # True, only if all elements of L are True.
    False
    >>> any(L) # True, if any elements of L are True.
    True
    

    Is there any corresponding function in your language (C#? Java?). If so, that's likely the cleanest approach.

提交回复
热议问题