Can you write an application, or even a small program that does something without conditionals. Like, no case-switches, no ternary operators not even a single little if. My guess is your thoughts are racing, your thinking blood is rushing to your head. By now I might even have lost your full attention. So I’m giving away the answer you might suspect. Yes, you can.
I have been thinking about this for quite some time. I have just been waiting for the right problem to attack. Two weeks ago I found one. I got a little hooked on a small strategy game on facebook. It’s called WarBook. It’s a pretty basic find resources, attack others and gain power, resource and levels sort of game. Very basic and very calculative.
When you plan on attacking other gamers you must be fairly certain of who’s going to win the battle or else you might loose a lot of troops and other resources. That’s what the application is about. Simulate a game from stats that are entered and present a winner. It also has a nice little GUI to make things more realistic.
Looking like this:
Why bother doing this. Conditionals are a natural part of any programming language, right. Yes. And a big source of errors too. In fact, any condition makes your codebase twice as complicated. By definition.
While programming this little application I had to think two, three and sometimes a whole lot more about the decisions I make whilst programming. I mean I normally think, but this got me thinking way more. I _knew_ I couldn’t take an easy way out by creating a factory-method and if-else my way out. I had to make things very clear and very to the point. I will analyze the code in later posts. Until then feel free to download the full source here. It’s a complete Eclipse project with ant build files and all. Although there is a lot of hardwired paths which you probably can do without if you just want to look at the code.