I have this huge __init__
method which I want to clean up even more after watching Clean Coders videos (content of Clean Code book by Robert Cecil Martin).
How could I get refactor the gigantic if..elif
block in a nice, readable way? Or should I leave it as it is? Do you find it readable?
@total_ordering
class Range:
"""Parses a poker range (str) into tuple of Combos (or Hands)."""
def __init__(self, range=''):
self._pairs = set()
self._suiteds = set()
self._offsuits = set()
for token, value in _RegexRangeLexer(range):
if token == 'ALL':
for card in itertools.combinations('AKQJT98765432', 2):
self._add_offsuit(card)
self._add_suited(card)
for rank in 'AKQJT98765432':
self._add_pair(rank)
# full range, no need to parse any more token
break
elif token == 'PAIR':
self._add_pair(value)
elif token == 'PAIR_PLUS':
smallest = Rank(value)
for rank in (rank.value for rank in Rank if rank >= smallest):
self._add_pair(rank)
elif token == 'PAIR_MINUS':
biggest = Rank(value)
for rank in (rank.value for rank in Rank if rank <= biggest):
self._add_pair(rank)
elif token == 'PAIR_DASH':
first, second = Rank(value[0]), Rank(value[1])
ranks = (rank.value for rank in Rank if first <= rank <= second)
for rank in ranks:
self._add_pair(rank)
elif token == 'BOTH':
self._add_offsuit(value[0] + value[1])
self._add_suited(value[0] + value[1])
elif token == 'X_BOTH':
for rank in (r.value for r in Rank if r < Rank(value)):
self._add_suited(value + rank)
self._add_offsuit(value + rank)
elif token == 'OFFSUIT':
self._add_offsuit(value[0] + value[1])
elif token == 'SUITED':
self._add_suited(value[0] + value[1])
elif token == 'X_OFFSUIT':
biggest = Rank(value)
for rank in (rank.value for rank in Rank if rank < biggest):
self._add_offsuit(value + rank)
elif token == 'X_SUITED':
biggest = Rank(value)
for rank in (rank.value for rank in Rank if rank < biggest):
self._add_suited(value + rank)
elif token == 'BOTH_PLUS':
smaller, bigger = Rank(value[0]), Rank(value[1])
for rank in (rank.value for rank in Rank if smaller <= rank < bigger):
self._add_suited(value[1] + rank)
self._add_offsuit(value[1] + rank)
elif token == 'BOTH_MINUS':
smaller, bigger = Rank(value[0]), Rank(value[1])
for rank in (rank.value for rank in Rank if rank <= smaller):
self._add_suited(value[1] + rank)
self._add_offsuit(value[1] + rank)
elif token in ('X_PLUS', 'X_SUITED_PLUS', 'X_OFFSUIT_PLUS'):
smallest = Rank(value)
first_ranks = (rank for rank in Rank if rank >= smallest)
for rank1 in first_ranks:
second_ranks = (rank for rank in Rank if rank < rank1)
for rank2 in second_ranks:
if token != 'X_OFFSUIT_PLUS':
self._add_suited(rank1.value + rank2.value)
if token != 'X_SUITED_PLUS':
self._add_offsuit(rank1.value + rank2.value)
elif token in ('X_MINUS', 'X_SUITED_MINUS', 'X_OFFSUIT_MINUS'):
biggest = Rank(value)
first_ranks = (rank for rank in Rank if rank <= biggest)
for rank1 in first_ranks:
second_ranks = (rank for rank in Rank if rank < rank1)
for rank2 in second_ranks:
if token != 'X_OFFSUIT_MINUS':
self._add_suited(rank1.value + rank2.value)
if token != 'X_SUITED_MINUS':
self._add_offsuit(rank1.value + rank2.value)
elif token == 'COMBO':
combo = Combo(value)
if combo.is_pair:
self._pairs.add(combo)
elif combo.is_suited:
self._suiteds.add(combo)
else:
self._offsuits.add(combo)
elif token == 'OFFSUIT_PLUS':
smaller, bigger = Rank(value[0]), Rank(value[1])
for rank in (rank.value for rank in Rank if smaller <= rank < bigger):
self._add_offsuit(value[1] + rank)
elif token == 'OFFSUIT_MINUS':
smaller, bigger = Rank(value[0]), Rank(value[1])
for rank in (rank.value for rank in Rank if rank <= smaller):
self._add_offsuit(value[1] + rank)
elif token == 'SUITED_PLUS':
smaller, bigger = Rank(value[0]), Rank(value[1])
for rank in (rank.value for rank in Rank if smaller <= rank < bigger):
self._add_suited(value[1] + rank)
elif token == 'SUITED_MINUS':
smaller, bigger = Rank(value[0]), Rank(value[1])
for rank in (rank.value for rank in Rank if rank <= smaller):
self._add_suited(value[1] + rank)
elif token == 'BOTH_DASH':
smaller, bigger = Rank(value[1]), Rank(value[2])
for rank in (rank.value for rank in Rank if smaller <= rank <= bigger):
self._add_offsuit(value[0] + rank)
self._add_suited(value[0] + rank)
elif token == 'OFFSUIT_DASH':
smaller, bigger = Rank(value[1]), Rank(value[2])
for rank in (rank.value for rank in Rank if smaller <= rank <= bigger):
self._add_offsuit(value[0] + rank)
elif token == 'SUITED_DASH':
smaller, bigger = Rank(value[1]), Rank(value[2])
for rank in (rank.value for rank in Rank if smaller <= rank <= bigger):
self._add_suited(value[0] + rank)
There is a similar question: Refactor deeply nested if-else, but my conditions cannot be simplified any more (well possibly except the token in ('X_PLUS', 'X_SUITED_PLUS', 'X_OFFSUIT_PLUS')
parts could be flatten out).
Maybe I could wrap small functions, but the if..elif
part would still be huge. Could you show a real life example how these vast conditions are simplified usually?