March 30, 2004
This one's about programming. Come back next time if that ain't your cup of tea.
One of the blogs I check regularly is JWZ's; he's usually got pretty cool tech news stuff that doesn't make it to the Register. Then I saw this entry on object-oriented programming. I'm with him; that's got to be a joke of some kind.
I realize that it's just a simple example detailing the "right" way to do things, but even in a more complex example it's tough to make a case for using objects and classes instead of just a plain-ol' if/else (or a switch, which I would use in that case). When the Right Way creates code that's so outrageously convoluted that the only person who can follow it is the guy who wrote it, and then only some of it, it ceases to be the Right Way. Give me a hack I can follow any day of the week.
I am a fan of convenience functions, though, and probably would've written his little example like this:
public class PrintOS {
public static void main(final String[] args) {
String osName = System.getProperty("os.name");
System.out.println(displayOS(osName));
}
private static String displayOS(os String) {
switch (os) {
case "SunOS":
case "Linux":
return "This is a UNIX box and therefore good.";
break;
case "Windows NT":
case "Windows 95":
return "This is a Windows box and therefore bad.";
break;
default:
return "This is not a box.";
break;
}
}
}
There. Isn't that much better than that polymorphic garbage the professor was peddling? You don't even have to edit main() any more to make changes. And the switch statement lets you make adjustments later on (maybe WinNT is OK, but 95 is still bad).
Now I admit that the database worms have crawled into my head, but even I know better than to write a mess like the example.