Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/118127-complex-decision-code_best-approach/
Share on other sites

I thought about using a switch...and just letting it fall through when appropriate....

but the decision points are not related that way.

 

Now what I was thinking of doing is say having an If....Else block for one test level and calling a function based on that point.  then inside the called function

have the next level If...Else block and again call another function based on that decision....and so on.

 

Is this something that is logically or functionally acceptable or not?

 

The code is for calendaring.  So it is checking given dates against other dates, previously selected dates, based on year, month, time of year.

So there is at least 5 levels of If...Else blocks and the code goes in a different direction based on each level's decision.  I was thinking I could use

a function at each decision level and pass in a parameter based on the decision.  So I would have to work through 5 functions.

To me it looks good on paper....but is it the right way to do things? That is my question.

 

Thanks,

Mike

That is what I would be doing....replacing a string of If....Else (or a string of nested If...Else) blocks with a string or hierarchy of functions.

That is what I want to know...which is best/more acceptable/correct?

 

The code will not be called but once per user to make date based reservations.  Each user would not make multiple reservations.  So the code would, theoretically, be used no more than 52 times per year.

 

I will post the function if needed.

But I am just looking for the correct approach to this.

 

Thanks,

Mike

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.