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
Complex Decision Code_Best Approach
Moderator: General Moderators
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Complex Decision Code_Best Approach
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.
You may want to look into the Command, State Stategy, etc. patterns.
(#10850)
Re: Complex Decision Code_Best Approach
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.
- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
Re: Complex Decision Code_Best Approach
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.