tags:

views:

312

answers:

10

One of my personal programming demons has always been complex logic that needs to be controlled by if statements (or similiar). Not always necessarily that complex either, sometimes just a few states that needs to be accounted for.

Are there any tools or steps a developer can perform during design time to help see the 'states' and take measures to refactor the code down to simplify the resulting code? I'm thinking drawing up a matrix or something along those lines...?

+2  A: 

Truth tables and unit tests - draw up the tables (n dimensional for n variables), and then use these as inputs to your unit test, which can test each combination of variables and verify the results.

Visage
Truth tables are always 2-dimensions (unless there have been new developments?). Do you mean n columns for n variables?
Bill the Lizard
multi dimensional truth tables? sounds exciting!
Adam Naylor
Er... truth tables have columns for all the variables and rows for all the combinations of values. You can represent as many variables as you like.
cletus
multi-variable truth tables could be classed as multi-dimensional (with a new axis for each variable). However, due to our piddly brains being limited to 3 dimensions, we tend to collapse them down. We also tend to do them on a flat surface, so again collapse them down to 2d tables :)
workmad3
@Bill - you can have as many dimensaions as you like - If I wanted to come up with a boolean based on 3 variables for example I would have a 2x2x2 table, representing the 8 discrete combinations of inputs...
Visage
Can you link to an example of a 3D truth table?
Bill the Lizard
Conceptualise it... you have 3 variables, with each variable as a different coordinate axis. The range on each is [0,1] in discrete units, and each value can be 'looked up' using 3 coordinates. As the ranges are finite, it can always be collapsed down to a finite 2d case though (which is what we do)
workmad3
I'm not having trouble with the concept. I just want to know why someone would suggest this as a solution to this question. Constructing a 3-, 4-, 5-, or N-dimension truth table is not a simplifying step.
Bill the Lizard
A: 

Split the logic down into discrete units (a && b, etc.), each with their own variable. Then build these up using the logic you need. Name each variable with something appropriate, so that your complex statement is fairly readable (although it may take up several extra lines and a fair few temporary variables).

workmad3
Ok, this is pretty much the approach i take, the problem is i find my self stuffing new branches in as and when i identify the need for them and can't find an existing branch. This obviously results in spaghetti code... i need a way to take this and 'see-the-bigger-picture'
Adam Naylor
+6  A: 

I'd recommend a basic course in propositional logic for every aspiring programmer. At first, the notation and Greek letters may seem off-putting to the math-averse, but it is really one of the most powerful (and oft-neglected) tools in your skillset, and rather simple, at the core.

The basic operators, de Morgan's and other basic laws, truth tables, and existence of e.g. disjunctive and conjunctive normal forms were an eye-opener to me. Before I learned about them, conditional expressions felt like dangerous beasts. Ever since, I know that I can whip them into submission whenever necessary by breaking out the heavy artillery!

Pontus Gagge
+5  A: 

Truth tables are basically the exhaustive approach and will (hopefully) highlight all the possibilities.

You might like to take a look at Microsoft Pex, which can be helpful for spotting the fringe cases you hadn't thought of.

teedyay
+1 for the info about Pex. Looks like a great tool.
0xA3
+2  A: 

The biggest problem I've seen through the years with complex IFs is that people don't test all the branches. Make sure to write a test for each possible branch no matter how unlikely it seems that you will hit it.

HLGEM
A: 

Any reason you cannot just handle the logic with guard statements?

Ty
+2  A: 

You might also want to try Karnaugh maps, which are good for up to 4 variables.

Steve Melnikoff
you beat me to it.
Mr Fooz
Karnaugh maps are good for that. It's too bad I hate them with a passion. They're what convinced me that I don't ever want to be a Computer Engineer. :-)
Jason Baker
A: 

Karnaugh maps can be nice ways of taking information from a truth table (suggested by Visage) and turning them into compact and/or/not expressions. These are typically taught in an EE digital logic course.

Mr Fooz
A: 

If you haven't already, I'd highly suggest reading Code Complete. It has a lot of advice on topics such as this. I don't have my copy handy at the moment, otherwise I'd post a summary of this section in the book.

Jason Baker
A: 

Have you tried a design pattern? You might look into what is known as the Strategy pattern: http://en.wikipedia.org/wiki/Strategy_pattern

Matt Lehmann