Complex Decision Code_Best Approach

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
MikeEller
Forum Newbie
Posts: 11
Joined: Sun Feb 05, 2006 3:43 pm

Complex Decision Code_Best Approach

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Complex Decision Code_Best Approach

Post 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.
(#10850)
User avatar
chaos
Forum Newbie
Posts: 22
Joined: Thu May 15, 2008 9:20 am
Location: New Jersey

Re: Complex Decision Code_Best Approach

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Re: Complex Decision Code_Best Approach

Post 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.
Post Reply