MikeEller Posted August 4, 2008 Share Posted August 4, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/118127-complex-decision-code_best-approach/ Share on other sites More sharing options...
effigy Posted August 4, 2008 Share Posted August 4, 2008 Yes, functions should be used to avoid code duplication. Have you considered a switch statement? Is the logic as grouped as it can be? Quote Link to comment https://forums.phpfreaks.com/topic/118127-complex-decision-code_best-approach/#findComment-607769 Share on other sites More sharing options...
MikeEller Posted August 4, 2008 Author Share Posted August 4, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/118127-complex-decision-code_best-approach/#findComment-607795 Share on other sites More sharing options...
effigy Posted August 4, 2008 Share Posted August 4, 2008 It sounds like you're simply replacing one complexity with another--a string of if/elses for a string of functions. How often is this portion of the code used/repeated? Can you post the pertinent code with comments (if needed)? Quote Link to comment https://forums.phpfreaks.com/topic/118127-complex-decision-code_best-approach/#findComment-607836 Share on other sites More sharing options...
MikeEller Posted August 4, 2008 Author Share Posted August 4, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/118127-complex-decision-code_best-approach/#findComment-607908 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.