Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any style guides recommendations or conventions for formatting complex boolean logic?

Tags:

coding-style

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?

like image 627
compound eye Avatar asked Jul 27 '09 00:07

compound eye


1 Answers

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)
}
like image 137
Paul van Brenk Avatar answered Oct 19 '22 09:10

Paul van Brenk