jeudi 28 mai 2015

Method to refactor the ‘if’ in Python?

The code like this:

def func(param):
"""
:param 'A' or 'B' or 'C'
"""

var1 = do_something_1()
if param == 'A':
    var1 = do_something_A_1()

var2 = do_something_2(var1)
if param == 'B':
    var2 = do_something_B_1()

var3 = do_something_3(var2)
if param == 'A':
    var3 = do_something_A_2()

var4 = do_something_4(var3)
if param == 'A':
    var4 = do_something_A_3()

var5 = do_something_5(var4)

return var5

I was wondering that whether there's a way to refactor the 'if' statements in this code.

Anyone has ideas about this?

Aucun commentaire:

Enregistrer un commentaire