<story><title>Decision Table Patterns</title><url>https://www.hillelwayne.com/post/decision-table-patterns/</url></story><parent_chain></parent_chain><comment><author>ajuc</author><text>Logic tables are a secret weapon when refactoring spaghetti code.&lt;p&gt;You take all the conditions relevant to the part you&amp;#x27;re refactoring.&lt;p&gt;You refactor common parts of the conditions (so if one if has (x&amp;gt;3 &amp;amp; y&amp;lt;2) and another if has (x&amp;gt;3 and y&amp;gt;10) you get (x&amp;gt;3), (y&amp;lt;2), (y&amp;gt;10).&lt;p&gt;You put that into a table, write all possible values that can happen (sounds like combinatoric explosion but it&amp;#x27;s usually not, because many of them depend on each other and lots of combinations cannot happen or aren&amp;#x27;t relevant to the result) - then rewrite the whole thing looking at the table.&lt;p&gt;&lt;pre&gt;&lt;code&gt;   x&amp;gt;3 | y&amp;lt;2 | y&amp;gt;10 | effect
   F   | F   | F    | ...
   T   | F   | F    | ...
   F   | T   | F    | ...
   T   | T   | F    | ...
   F   | F   | T    | ...
   T   | F   | T    | ...
&lt;/code&gt;&lt;/pre&gt;
Another form is&lt;p&gt;&lt;pre&gt;&lt;code&gt;   x   | y      | effect
   &amp;lt;3  | &amp;lt;2     | ...
   &amp;gt;=3 | &amp;lt;2     | ...
   &amp;lt;3  | 2...10 | ...
   &amp;gt;=3 | 2...10 | ...
   &amp;lt;3  | &amp;gt;10    | ...
   &amp;gt;=3 | &amp;gt;10    | ...

&lt;/code&gt;&lt;/pre&gt;
Usually if the code is old enough there&amp;#x27;s redundant branches, checking conditions that can&amp;#x27;t happen, duplicated code &amp;quot;just in case&amp;quot;, etc.&lt;p&gt;With the table you can look at it and see input-output and refactor it without worrying about changing behaviour.&lt;p&gt;For simple cases it&amp;#x27;s overkill, but with big enough mess it&amp;#x27;s a life-saver.</text></comment>
<story><title>Decision Table Patterns</title><url>https://www.hillelwayne.com/post/decision-table-patterns/</url></story><parent_chain></parent_chain><comment><author>aszen</author><text>Decision table&amp;#x27;s are really good at representing complex rules, in a language with pattern matching u can convert a nested if else block to a decision table that is much more readable, as an example see &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;finos&amp;#x2F;morphir-examples&amp;#x2F;blob&amp;#x2F;master&amp;#x2F;src&amp;#x2F;Morphir&amp;#x2F;Sample&amp;#x2F;Rules&amp;#x2F;Direct.elm&quot; rel=&quot;nofollow&quot;&gt;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;finos&amp;#x2F;morphir-examples&amp;#x2F;blob&amp;#x2F;master&amp;#x2F;src&amp;#x2F;Mo...&lt;/a&gt;</text></comment>