Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i structure a program (proces) with a very high number of IF statements

i have to create a program for a kind of complex proces. Well, the process is not complex, but there are a lot of variables which control the process. I can't in detail tell about the process, so i've made up one, which has the same load of IF's:

the process is: should we stop the iron melt oven or not. We've got these parameters: if the temp goes above 800 degrees celcius, stop it Except, when we expect cool water to be available from oven 2 in the next 20 minutes, we can continue

Except, when the temp rises 10 degrees in the next 10 minutes, we can't wait another 10 minutes for the cold water, so we have to stop.

Except, when the temp goes down for some reason to 790-800 degrees for 5 minutes, we add an extra 5 minutes to the time we need the extra cool water.

Except, when the temp goes down for some reason to 780-790 degrees for 5 minutes, we add an extra 5 minutes to the time we need the extra cool water.

etc. etc.

You can all think of another 20 except / if / then's

in our process we have > 50 situations, all for one goal: should the machine stop or not.

I must say i normally don't have that many situations for one goal/issue (namely: stop the machine or not), and it is also timebound: if this is happening for 10 minutes then...., and we have to calculate the situation every minute again.

Is there a way to program this in a smart way?

(and also unit test, because we have loads and loads of combinations which are all seperate unit tests i guess?)

like image 546
Michel Avatar asked Sep 22 '10 08:09

Michel


People also ask

Do many if statements slow down code?

Nope. In fact, it'll actually speed it up in most cases (because it's allowed to skip over blocks of code). If you're considering merging a bunch of if statements into one: don't, you won't get any benefits.

What is the maximum number of statements that can be associated with a conditional?

There is no limit on the number of statements that can appear under the two clauses of an if else statement, but there has to be at least one statement in each block.

How many lines of code will be executed after an if statement?

Only one line(s) of code will be executed, but which depends on the condition.


2 Answers

Instead of manually working out all the situations in which you must stop the oven based on your knowledge of how an oven works, you could get the computer to do this for you.

Try to program a model for how an oven works and at each time point you can run a simulation of what will happen in the next 10 minutes or so. If the simulation shows that a serious problem will occur soon then you can stop the oven now, otherwise let it run for a minute, read the inputs and run the simulation again.

Coding an accurate model will require some effort, but as your requirement change slightly (for example your over was upgraded or repaired) it will be relatively easy to update the model - in the best case you just modify the model parameters or update a single formula.

With your current approach if you modified the oven slightly you would have to first calculate manually what effects that will have, and then go through all the code line-by-line updating the different conditions.


Regarding unit testing - ideally you should test your model on data from a real oven in a variety of situations and check that your simulation predicts the actual measured results (with some tolerance allowed for reading inaccuracies). If this is not possible then you can hardcode some example scenarios and the expected result (e.g. overheating) and test that the simulation correctly predicts the result.

like image 72
Mark Byers Avatar answered Oct 06 '22 16:10

Mark Byers


Some problem spaces dont fit into the OOP principle very elegantly. It sounds your situation can be managed much better in a rule based paradigm (especially when rules change). Here is an exampe of a rule based framework for .NET: http://www.codeproject.com/KB/cs/Drools_NETPrimer.aspx

like image 36
MrDosu Avatar answered Oct 06 '22 17:10

MrDosu