When programming imperatively I often find myself writing code to groups items like this:
function group(items):
groups <- new Groups
curGroup <- new Group
for item in items:
if item doesn't belong in curGroup:
if curGroup is good:
add curGroup to groups
curGroup <- new Group
add item to curGroup
if curGroup is good:
add curGroup to groups
return groups
Unfortunately this code suffers from a few deficiencies:
-
The
if curGroup is good: add curGroup to groupscode is duplicated. While the condition in the conditional can be broken out into a function, the logic to invoke that function and add curGroups to groups still appears twice and it's easy to forget the second occurrence. -
The logic to create a new group appears twice. This logic may be trivial, and if it isn't then it can be broken out into a separate function, but like with the first bullet point, it indicates that the flow is incorrect.
-
It's possible that the first item fails the belong check, in which case we create a new group right after creating a new group. This issue may seem trivial but it occasionally requires explicitly preventing the initial empty group from being added to
groups. Regardless, it suggests incorrect expression of the desired logic.
I am wondering whether there is a cleaner way to express this logic. I apologize for the abstract nature of this question but this problem appears in multiple contexts. If it is necessary to address this issue in the context of a particular programming language, you can assume Java.
Aucun commentaire:
Enregistrer un commentaire