A Parser for Conditional Expressions
Photo by Yung Chang on Unsplash.
I've created a tool at https://www.wimpyprogrammer.com/conditional-parser/ for analyzing conditional expressions.
A while back my co-worker and I encountered a tricky conditional statement on a legacy C# project:
!HasDeadlinePassed
&& Active
&& (IsVerified.HasValue
&& (IsVerified.Value
|| (!IsVerified.Value
&& (AccountType != "CannotValidate"
&& !String.IsNullOrWhiteSpace(AccountType)
)
)
) || importID.HasValue
)
While troubleshooting that portion of the code, we needed to understand all of the circumstances in which the expression would evaluate to true. "Surely there's a tool that can break this down for us!" I thought, but couldn't find one and decided to build my own.
The Conditional Expression Parser will divide a conditional statement into its operators and conditions, then list the combinations that would cause the entire expression to evaluate to true
. It recognizes AND
(&&
), OR
(||
), and XOR
(^
) operators and is smart enough to ignore strings ("a && b"
) and function calls (foo(a && b)
).
The source code is available on GitHub.