Problems? Is your data what you think it is? | |
PerlMonks |
Re: Why I Hate Nested If-Else blocksby mstone (Deacon) |
on Jan 04, 2002 at 02:30 UTC ( [id://136124]=note: print w/replies, xml ) | Need Help?? |
I use two general solutions, depending on how complex the problem is. The first option is to use a table of function pointers:
and since I happen to like object syntax, I have a simple little class that wraps function pointers up as objects:
Naturally, it helps to make sure all the functions/objects in a table take the same list of arguments. That restriction appeals to me, because it helps me write code that ends in a known state no matter what. Nested if-else statements make it a little too easy to set one set of globals in one branch, and another set of globals in another, IMO. That makes it really difficult to decide what state the program is in when it leaves the outermost statement. If a project involves serious branching, I'll occasionally roll my own linear state machine:
This code comes from a larger project, where the parameter $model was itself an object. The Model was responsible for all the data in the program, and the Commands simply called sets of Model methods.
In Section
Meditations
|
|