how to simplify boolean logic in if else statement?

前端 未结 2 1704
一生所求
一生所求 2021-01-17 03:01

I have 4 variables, some of them will be True and False, and for each combinations, i will have to call one or few functions. i am currently using a if else statement for ea

相关标签:
2条回答
  • 2021-01-17 03:08

    You implied in your question that some sort of lookup table for the logic might be the way to go, and there are many ways you could make something like that.

    One way would be, as you say, with a dict, as shown below.

    This way results in rather more complex looking code than the nice looking "simplify and factor out duplication" solution for your particular example, but this method extends more easily to the case where you have more different logic for the different cases... I'm not saying it's "better" ... just a different option!

    def launch_isp_portal_and_radius():
       print "launch ISP portal, modem and radius"
       # etc
    
    # etc
    
    DecisionTable = {
     # cpe_ip cpe_passwd phone ppoe
       (True,  True,  True,  True ) : launch_isp_portal_and_radius,
       (True,  True,  False, False) : launch_modem_test_only,
       (False, False, True,  False) : only_Bell_portal,
       (False, True,  True,  False) : only_Bell_portal,
       (True,  False, True,  False) : only_Bell_portal,
       (True,  True,  False, True ) : launch_modem_and_radius_test,
       (False, False, False, False) : only_radius_tests,
       (False, True,  False, False) : only_radius_tests,
       (True,  False, False, False) : only_radius_tests,
       (False, False, True,  True ) : bell_and_radius_tests
       (False, True,  True,  True ) : bell_and_radius_tests,
       (True,  False, True,  True ) : bell_and_radius_tests,
       (True,  True,  True,  False) : launch_modem_and_bell_tests
    }
    
    action = DecisionTable.get((self.cpe_ip, self.cpe_passwd, self.phone, self.ppoe))
    
    if action:
       action()
    else:
       raise StandardError("Whoa, internal logic error!")
    

    (And of course there are other ways to make a decision table - an array is another obvious option. The problem with an array is making the static declaration of it look readable for the logic it represents...)

    (Edit: made the dict indexed on tuple, as per jons suggestion)

    0 讨论(0)
  • 2021-01-17 03:16

    One way to simplify it would be to recognise and factor out the duplication:

    if self.phone:
        if self.isp() == "east":
            self.launchbell()
        else:
            self.launchtelus()
    
    if self.cpe_ip and self.cpe_passwd:
        print 'check modem...'
        self.modemstatus()
    
    if self.pppoe:
        radius = sgp_radius.Radius(self.pppoe)
        print 'check radius logs...'
        self.data = radius.sgp()
        self.radius_save()
    
    0 讨论(0)
提交回复
热议问题