How do you handle huge if-conditions?

后端 未结 21 1781
一向
一向 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:49

    I like to break them down by level, so I'd format you example like this:

    if (var1 = true
     && var2 = true
     && var2 = true
     && var3 = true
     && var4 = true
     && var5 = true
     && var6 = true){
    

    It's handy when you have more nesting, like this (obviously the real conditions would be more interesting than "= true" for everything):

    if ((var1 = true && var2 = true)
     && ((var2 = true && var3 = true)
      && (var4 = true && var5 = true))
     && (var6 = true)){
    

提交回复
热议问题