Prolog refactor if/else How do I refactor this mess of if/else conditions so that it's more readable? 


    ( Reverse_Path = [[]] -&gt; Child = [c(New_Score,G1,Pos),P]
    ; otherwise -&gt; Child = [c(New_Score,G1,Pos),P|Reverse_Path]
    ),
    (  memberchk(Child,NewAgenda) -&gt; addChildren(Others,Reverse_Path,Current,NewAgenda,Target,Result)

    ;  otherwise -&gt; 
        (   NewAgenda=[] -&gt; BrandNewAgenda = [Child|NewAgenda]
        ;   otherwise    -&gt; 
            (New_Score =&lt; FP -&gt; BrandNewAgenda = [Child|NewAgenda];
            otherwise  -&gt; append(NewAgenda,[Child],BrandNewAgenda)
            )
        ),

    )).
Generally speaking, you can resolve if/else logic by using multiple clauses for the same predicate, as multiple clauses implicitely represent 'or' or 'case', e.g.:
foo([[]], Child) :-
   Child = [c(New_Score,G1,Pos),P].
foo(Reverse_Path, Child) :-
   Child = [c(New_Score,G1,Pos),P|Reverse_Path].
bar(Reverse_Path, Child, NewAgenda, BrandNewAgenda) :-
   memberchk(Child,NewAgenda),
   addChildren(Others,Reverse_Path,Current,NewAgenda,Target,BrandNewAgenda).
bar(Reverse_Path, Child, [], BrandNewAgenda) :-
   BrandNewAgenda = [Child].
bar(Reverse_Path, Child, NewAgenda, BrandNewAgenda) :-
   New_Score =&lt; FP,
   BrandNewAgenda = [Child|NewAgenda].
bar(Reverse_Path, Child, NewAgenda, BrandNewAgenda) :-
   append(NewAgenda, [Child], BrandNewAgenda).

main(Reverse_Path, BrandNewAgenda) :-
   foo(Reverse_Path, Child),
   bar(Reverse_Path, Child, [], BrandNewAgenda).

This is just a rough sketch, there are a lot of loose ends here like variables Others, Target or Result (I guess you are juggling too many variables anyway), but I hope you get the gist of it:

Decompose into multiple simpler predicates that encode alternatives through multiple clauses.
Bind results to &quot;out&quot; variables in the clause head (like Child in foo/2 or BrandNewAgenda in bar/4).
Compose the smaller predicates in a &quot;main&quot; predicate that glues them togehter.