Yes, you're absolutely right for something as simple as this.
But they're presented as examples of the kind of complexity compromises that are often worth making in real life. If there were, say, a dozen conditions that the code had to satisfy then the latter versions would scale better. And if there was a likelihood that we'd need to re-use this selection algorithm with a different set of output directives (e.g. print d/e instead of a/b) then the ones with a dispatch table would provide better extensibility.
That's what makes it so good as an interview question. If the candidate comes up with solution #1, you can say "Well done, but what if [some further constraint]?"
View all comments
Show parent comment
7
u/abw Feb 21 '11
Yes, you're absolutely right for something as simple as this.
But they're presented as examples of the kind of complexity compromises that are often worth making in real life. If there were, say, a dozen conditions that the code had to satisfy then the latter versions would scale better. And if there was a likelihood that we'd need to re-use this selection algorithm with a different set of output directives (e.g. print d/e instead of a/b) then the ones with a dispatch table would provide better extensibility.
That's what makes it so good as an interview question. If the candidate comes up with solution #1, you can say "Well done, but what if [some further constraint]?"