I was doing a code review today and noticed that the code would work but could have easily been made more readable.
The example in particular was roughly as follows:
def group(x):
GROUP_ALPHA = ('a', 'b', 'e', 'f', 'i', 'j', 'k', 'l')
GROUP_BETA = ('m', 'n', 'o', 'p', 'q')
GROUP_CHARLIE = ('x', 'y', 'z', 'aa', 'ab', 'ae', 'af')
GROUP_DELTA = ('ag', 'ah', 'ai', 'aj')
GROUP_ECHO = ('ak', 'al', 'am', 'an', 'ao', 'ar', 'as')
if x in GROUP_ALPHA:
return 'ALPHA'
elif x in GROUP_BETA:
return 'BETA'
elif x in GROUP_CHARLIE:
return 'CHARLIE'
elif x in GROUP_DELTA:
return 'DELTA'
elif x in GROUP_ECHO:
return 'ECHO'
Note that every group is disjoint.
To me, this would be more readable with:
def group(x):
GROUPS = {
'ALPHA': ('a', 'b', 'e', 'f', 'i', 'j', 'k', 'l'),
'BETA': ('m', 'n', 'o', 'p', 'q'),
'CHARLIE': ('x', 'y', 'z', 'aa', 'ab', 'ae', 'af'),
'DELTA': ('ag', 'ah', 'ai', 'aj'),
'ECHO': ('ak', 'al', 'am', 'an', 'ao', 'ar', 'as')
}
for group_name, group_elements in GROUPS:
if x in group_elements:
return group_name
Should I mention this? The original code works and is used in a non-critical capacity. When I was researching, I noticed this answer, which remarks:
Every change costs money. Rewriting code that works and is unlikely to need to be changed could be wasted effort. Focus your attention on the sections that are more subject to change or that are most important to the project.
This would suggest that the answer is no, but it seems wasteful to have a code review and not suggest a better way to do things, even if the original way "works". Personally I would want someone to make such a suggestion to me.
Note: the above examples have been edited since the question was first written. As well, each entry ('a'
, 'b'
, etc.) is actually a human readable str
related to business logic.