Page 1 of 1

Complex Decision Code_Best Approach

Posted: Mon Aug 04, 2008 12:49 pm
by MikeEller
Hi,
I have a function that must work through several layers of decision points. Right now implemented with nested If...Else blocks.
Many times the decision arrived at must implement the same code with just a change or two to variable values and such.
And it is getting difficult to follow.

My question is....is it acceptable coding practice to place function calls inside the If...Else blocks? This, in my mind would make the code more readable and easier to follow the logic. However, do I lose anything in all the function calls?

The If...Else blocks would remain, I would just be able to call different functions with different parameters to do the work.

What is the best approach when dealing with a high degree of complexity of decision points?

Thank You,
Mike

Re: Complex Decision Code_Best Approach

Posted: Mon Aug 04, 2008 1:27 pm
by Christopher
Certainly putting code that is reused will improve the code. Putting code that is long into functions will improve readability.

You may want to look into the Command, State Stategy, etc. patterns.

Re: Complex Decision Code_Best Approach

Posted: Tue Aug 05, 2008 12:05 pm
by chaos
When you find yourself duplicating the same code with different variable values, 99% of the time it's appropriate to factor that code out into a function.

Re: Complex Decision Code_Best Approach

Posted: Tue Aug 05, 2008 11:56 pm
by Ambush Commander
Functions are a good first place to start. As arborint says, if your logic is sufficiently complicated enough you'll want a full-blown model, but envisioning such a system in OOP will require a little practice.