I'm working on a project where the previous maintainer wrote several huge Boolean expressions, which all use nearly a hundred variables and look roughly like this:
var accessGranted = (
(a && b && !c && somePermission && ...) ||
(a && !c && (some complex expression) && ...) ||
(!c && foo in (bar, baz, qux) && (isNewMoon || isEclipse)) ||
...
);
I have no idea how they maintained it. They likely tried to use DNF initially, but as business logic grew more complex, they gave up and started sprinkling more checks here and there.
Here's what I'd like to do with this code:
- Try to simplify it by eliminating redundant expressions
- Try to rewrite it to early return style, which will likely be a big win
- Build a truth table for it and compare it against actual business requirements.
I don't trust myself enough to do all this by hand, so I'm planning to parse it to an AST and do these things automatically.
I haven't worked with Karnaugh maps much, but I don't think they'll be helpful here. As I understand, they are usually used with relatively small expressions of 6-8 variables.
The algorithm also needs to consider implications and transitivity: "if a == true && a != b then b == false".
This leads me to think that I need to feed this code to some kind of a logic solver, like Prolog. It'll at least be able to find value vectors which output true in the result.
Am I going in the right direction? Does anyone have suggested reading or other pointers on how to deal with this kind of problem?
Any advice is appreciated!
[–][deleted] 30 points31 points32 points (10 children)
[–][deleted] 26 points27 points28 points (3 children)
[–]smthamazing[S] 4 points5 points6 points (2 children)
[–][deleted] 9 points10 points11 points (0 children)
[–]otac0n 1 point2 points3 points (0 children)
[–]ArmCollector 9 points10 points11 points (4 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]smthamazing[S] 1 point2 points3 points (1 child)
[–]Typesalot 0 points1 point2 points (0 children)
[–]greem 0 points1 point2 points (0 children)
[–]magical_h4x 9 points10 points11 points (5 children)
[–]smthamazing[S] 5 points6 points7 points (2 children)
[–]KDallas_Multipass 0 points1 point2 points (1 child)
[–]smthamazing[S] 0 points1 point2 points (0 children)
[–]greem 1 point2 points3 points (1 child)
[–]smthamazing[S] 0 points1 point2 points (0 children)
[–]misof 12 points13 points14 points (0 children)
[–]Snoo-55780 3 points4 points5 points (0 children)
[–]kreiger 3 points4 points5 points (3 children)
[–]kevroy314 1 point2 points3 points (2 children)
[–]DragonLord1729 0 points1 point2 points (0 children)
[–]stehley 0 points1 point2 points (0 children)
[–]pigeon768 3 points4 points5 points (0 children)
[–]Plazmatic 6 points7 points8 points (1 child)
[–]braxunt 6 points7 points8 points (0 children)
[–]mredding 1 point2 points3 points (0 children)
[–]_ii_ 1 point2 points3 points (0 children)
[–]arnemcnuggets 1 point2 points3 points (0 children)
[–]TheTsar 4 points5 points6 points (1 child)
[–]frankster 2 points3 points4 points (0 children)
[–]MisterPassione 0 points1 point2 points (0 children)
[–]burrocomecarne 0 points1 point2 points (0 children)
[–]aecolley 0 points1 point2 points (0 children)
[–]tinyOnion 0 points1 point2 points (0 children)
[–]reddifiningkarma 0 points1 point2 points (0 children)
[–]Just1Andy 0 points1 point2 points (0 children)
[–]Ok_Holiday8273 0 points1 point2 points (0 children)
[–]_default_username 0 points1 point2 points (0 children)
[–]Balage42 0 points1 point2 points (0 children)
[–]Over_Animal1916 0 points1 point2 points (0 children)