vendredi 25 mars 2016

Eliminate if-else from while-loop

I have two class:

class A:
    b = B()
    function doSomething():
        while True:
             b.doSomething()

class B:
    counter = 0
    function doSomething():
        if counter < 10: 
            performMethod1()
        else:
            performMethod2()
        counter += 1
    function performMethod1(): ...
    function performMethod2(): ...

I feel this code is bad, because I know that B.performMethod2() is going to be performed much more times than B.performMethod1(), but the if-else (if counter < 10) is going to be checked every time I go inside B.doSomething().

Additionally, I don't want to break the while-loop of class A, because I want to hide the implementation details of class B from A.doSomething().

Is there any good way to eliminate the if-else of B.doSomething()? Thank you.

Aucun commentaire:

Enregistrer un commentaire