{"id":616,"date":"2011-03-02T00:49:25","date_gmt":"2011-03-02T06:49:25","guid":{"rendered":"http:\/\/www.ourada.org\/blog\/?p=616"},"modified":"2011-03-02T00:49:25","modified_gmt":"2011-03-02T06:49:25","slug":"heuristics-and-bestiaries","status":"publish","type":"post","link":"https:\/\/www.ourada.org\/blog\/archives\/616","title":{"rendered":"Heuristics and bestiaries"},"content":{"rendered":"<p>I&#8217;ve worked on more than what I consider my fair share of projects where I had to deal with crazy input. What I mean by crazy input is: it fails to meet the basic criteria of the standard to which it&#8217;s supposed to conform, or there is not a standard to which it&#8217;s supposed to conform, or standard elements are mashed together in unanticipated ways to try to achieve non-standard effects, or some combination of those.<\/p>\n<p>I suppose that if you dig deep, you&#8217;ll find that most of the data in the world is crazy in this sense. So despite my distaste for it, I&#8217;ll probably never get away from it. Still, I need to whine about it occasionally. But in addition to whining about it, I&#8217;ll present a little practical advice on dealing with it.<\/p>\n<p>The first and most obvious reality in dealing with crazy input is that some heuristic methods will have to be used. For example, in some HTML input, I had to deal with this little number, which, in some documents, occurred between every two paragraphs (well, divs, because these documents don&#8217;t use p tags much at all):<br \/>\n[html]<br \/>\n&lt;div style=&quot;margin-top: 6pt; font-size: 1pt&quot;&gt;&amp;nbsp;&lt;\/div&gt;<br \/>\n[\/html]<\/p>\n<p>These aren&#8217;t really paragraphs. Even if you call a div a paragraph, they&#8217;re still not paragraphs; they&#8217;re just there for spacing. So I have a rule that says something like &#8220;If a paragraph is preceded by a paragraph that is essentially empty and has a font size less than 5, remove the empty paragraph and change the spacing on the current one to get the same spacing effect&#8221;.<\/p>\n<p>That&#8217;s not the best\/worst example, but it gets the point across. Heuristics will be necessary. That implies two things: the overall code structure must be adaptable to the addition of heuristics, and I&#8217;ll need a bestiary to test the code.<\/p>\n<p>It&#8217;s part of the software developer&#8217;s mindset to try to neatly partition the entire universe into non-overlapping subsets, then write chunks of code to deal with each partition separately. The introduction of heuristics into such a beautiful scheme will cause some pain. In the beautiful world, I&#8217;d have code that says &#8220;it&#8217;s a paragraph, let&#8217;s do the paragraph thing with it&#8221;. In a world laden with heuristics, I have code that says &#8220;it&#8217;s a paragraph, but let&#8217;s see if it&#8217;s _really_ a paragraph, then we&#8217;ll either do the paragraph thing or do some wildly different thing&#8221;. I guess it&#8217;s less that the code structure has to be adaptable than it&#8217;s that my mindset has to be adaptable.<\/p>\n<p>Regardless of the flexibility of my mindset or my code, though, heuristics, by their nature, do not neatly partition the universe. They leave some things out, they overlap, and\/or they tangle together in increasingly strange ways. I&#8217;ll never remember, when it comes time to add some new code, all the situations that got me to this point or all the ways that things can go wrong.<\/p>\n<p>I&#8217;m not yet a full convert to test-driven development, but when dealing with beastly input, I consider a bestiary to be quite necessary. A big set of unit tests, with a perfect specimen of each of the beasts I&#8217;ve encountered, each named after the ticket in the ticket-tracking system that brought it to me. I had one project in the past where I should have created a bestiary but didn&#8217;t, and that project was one of the worst disasters in my professional life. Another one like that and I would have traded my keyboard in for a shovel and started a new career&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve worked on more than what I consider my fair share of projects where I had to deal with crazy input. What I mean by crazy input is: it fails to meet the basic criteria of the standard to which it&#8217;s supposed to conform, or there is not a standard to which it&#8217;s supposed to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-616","post","type-post","status-publish","format-standard","hentry","category-general","author-admin"],"_links":{"self":[{"href":"https:\/\/www.ourada.org\/blog\/wp-json\/wp\/v2\/posts\/616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.ourada.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ourada.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ourada.org\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ourada.org\/blog\/wp-json\/wp\/v2\/comments?post=616"}],"version-history":[{"count":0,"href":"https:\/\/www.ourada.org\/blog\/wp-json\/wp\/v2\/posts\/616\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ourada.org\/blog\/wp-json\/wp\/v2\/media?parent=616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ourada.org\/blog\/wp-json\/wp\/v2\/categories?post=616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ourada.org\/blog\/wp-json\/wp\/v2\/tags?post=616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}