title: Reducing Objects to Categories based on arbitrary logic
body: <p><em>This question could be subjective. I'm not sure if it belongs here or on <a href="http://programmers.stackexchange.com">Programmers</a></em></p>
  
  <p>Say I have a data type, <code>X</code> (think of business objects constructed from a relational database). My end goal is represent many instances of this type in a table for a report, with each instance under one of a few different headings.</p>
  
  <p>The heading to display the object under is selected based on arbitrary logic handed down from management, which anybody who's done corporate software development will be familiar with:</p>
  
  <p>Examples:</p>
  
  <p><code>If the instance has a FooID of 6 and a BarFactor of &lt; 0.5, place it under the "Borked" heading.</code></p>
  
  <p>or</p>
  
  <p><code>If the Weight is &gt; 0, and the CreatedDate is before Midnight but after 3PM, and today is not the 3rd Wednesday of the month, the object should be categorised as "Fluffy".</code></p>
  
  <p>My question: Is there a common idiom for taking an instance of <code>X</code>, applying a potentially headache-inducing amount of logic to the state of the instance and getting a category/string/arbitrary value from the result of that logic?</p>
  
  <hr />
  
  <p>My ideas so far have been:</p>
  
  <ol>
  <li><p>A function which takes an <code>X</code> and returns a <code>String</code>. I could easily see this turning into a <a href="https://stackoverflow.com/questions/2349378/new-programming-jargon-you-coined#2396633">Megamoth</a>, and a b***** to maintain as requirements are constantly modified.</p></li>
  <li><p>Define a <code>Heading</code> abstract type, and a factory function which gives me an instance of <code>Heading</code> with the <code>ToString</code> method correctly overloaded. I think this technique will be plagued by the same issues as idea no. 1.</p></li>
  <li><p>A hierarchy of functions, each splitting the problem up a little further until we arrive at the correct heading. </p></li>
  </ol>
  
  <p>For example:</p>
  
  <pre><code>public String GetHeading(X x) 
  {
      if (x.Weight &gt; 0)
          return WeightGreaterThanZero(x);
      else if (x.Weight &lt; 0)
          return WeightLessThanZero(x);
      else
          return WeightIsZero(x);
  }
  </code></pre>
  
  <p>The three "Weight" functions would test further conditions until we arrive at a value. The problem I see here is that we need to track which function called which. The <code>FooIDIs6</code> function needs to know whether it was called by <code>WeightIsZero</code> or some other function, otherwise any preceding decisions are potentially meaningless. We end up with something like <code>WeightIsGreaterThanZero_FooIDIs6_GrandmotherIsOlderThan100</code> etc. etc.</p>