Nearly all of my personal projects are console applications so I do a lot of input parsing. In the course of this type of application I often find myself in situations where I need to do multiple logic blocks in the same method. True, I could route to multiple methods at this level but I prefer to do the initial parsing in one.
Often a simple case statement isn't sufficient. So it would be easy to end up with unruly if/else blocks. I typically define a handled boolean. When I find a match I set this to true. So all my if's first check that the handled is false. And if I have a match I set it to true. This keeps my if statements to a single level.
Within the matching if statements I typically call a method to do the actual work, this makes my central parsing method more readable and elegant.
I'm sure there may be better ways of handling this (And I welcome your suggestions), but so far it is the cleanest way I've come up with.
No comments:
Post a Comment