Yesterday, as I was regarding a bunch of bananas in the kitchen, I mentally dashed off this code snippet:
#typedef enum {
Banana_Green = 0,
Banana_Yellow,
Banana_Spotted,
Banana_Brown
} BananaRipeness;
#typedef enum {
Take_Banana = 0,
Hold_Out_For_Banana_Bread
} BananaAction;
– (BananaAction) takeBanana:(BananaBunch *)bunch
{
if ( (0 == [bunch count]%3) && [bunch ripeness] > Banana_Yellow ) {
return Hold_Out_For_Banana_Bread;
}
else {
return Take_Banana;
}
}
People who live in houses where banana bread is made will, of course, understand at a glance that when the bananas are getting on in ripeness and there is a multiple of three bananas remaining, one does not take a banana, but rather one holds out for banana bread, lest they face the ire of their fellow residents. Some debate is possible whether the ripeness threshold should be past yellow, as in my code here, or whether one should start holding out earlier, even though the bananas still have a few days left.
It is very good to be in a house where banana count is important.
Code notes: this is written in (more or less) Objective-C, and assumes there is already defined a collection called BananaBunch. I generally avoid multiple exit points in a function, but this one is simple enough that I decided it was OK. I haven’t bothered checking the code for errors, it’s just not that sort of exercise.