I worked on a project which involved complex boolean logic. This complexity made the code very efficient, but unfortunately hard to read.
So we laid the logic out as below, which made it easier to see the groups within the compound statements, and also made it possible to add comments to parts of the logic.
(This code isn't real code from the project, the real logic was more complex)
if (
//comments here!
angle.angle < kQuiteLow
&& (
previousAngle.angle > kQuiteHigh
|| previousAngle.time == kUnknownTime
)
//comments in here too!
&& pairedAngle.angle < kQuiteLow
&& (
//and here
previousPairedAngle.angle > kQuiteHigh
|| previousPairedAngle.time == kUnknownTime
)
)
Have you ever seen this done anywhere else?
Are there any conventions or style guide recommendations on how to lay out very complex boolean logic?
I would refactor the code to use external methods to make it easier to read.
if( ValidAngle(angle, previousAngle) && ValidAngle(pairedAngle, previousPairedAngle) )
ValidAngle( angle, prevAngle){
return angle.angle < kQuiteLow && (previousAngle.angle > kQuiteHigh || previousAngle.time == kUnknownTime)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With