If you want to learn how I use commands and handlers to keep my code neat and tidy, read on. It’s not about avoiding
if-elseif-else. It’s about finding a more suitable approach.
Bear in mind that no single approach will ever get rid of traditional branch-based programming. You need to build a repository of techniques to draw from.
The method we’ll walk thru now is just one of many.
if-else is, at its very core, not bad. It’s merely just a hammer and nail situation we got going. …
switch makes for condense, simple code. But your software should not be comprised of the fewest lines possible, sacrificing readability, maintainability, or flexibility.
I see a lot of branching happening over enums, or, other discrete values. Some developers even get aggravated when told not to use
But, what do you think the consequences of using enums in your
if-then-else statement are?
Branching on discrete values makes your software difficult to change. Every new feature requires you to track down where the branching is happening, and modify your existing code accordingly.
This is definitely not how we want to develop great software. It’s perhaps a perfect first step towards making your code work. But, as you progress to making your code better,
if-then-else must be long gone. …
Rubbish software is produced when we try to do everything at once.
Principles, guidelines, best practices, and rules of thumb — they all make your life easier. Without them, ten-minute tasks can turn into ten-hour tasks.
One of the absolute best pieces of advice I received from my mentor very early on in my career was this simple one-liner:
“Make it work, make it better, make it faster.”
It’s a slight alteration of Kent Beck’s famous quote, and its simplicity is enabling and puzzling.
“Make it work” is quite easy to wrap your head around. You have a set of requirements, and you’re coding to fulfill them — kid stuff. …