{"id":16,"date":"2005-11-01T22:52:00","date_gmt":"2005-11-02T03:52:00","guid":{"rendered":"http:\/\/www.toomik.net\/helen\/wordpress\/?p=16"},"modified":"2005-11-01T22:52:00","modified_gmt":"2005-11-02T03:52:00","slug":"good-programmers-think-differently","status":"publish","type":"post","link":"https:\/\/www.toomik.net\/helen\/blog\/2005\/11\/01\/good-programmers-think-differently\/","title":{"rendered":"(Good) programmers think differently"},"content":{"rendered":"<p>\nMy job description doesn&#8217;t say (that is, if I actually had a job description then it wouldn&#8217;t say) a word about software development. It would probably say a fair bit about modelling, data management and data analysis, though. And once those pass a certain level in terms of complexity, you really end up in the realm of programming anyway. So while Excel is the main tool for me and my colleagues, we spend a fair amount of our time developing small applications in VBA, using VBA to augment our Excel models, and now also coding in <a href=\"http:\/\/www.sas.com\/technologies\/analytics\/index.html\">SAS<\/a>.\n<\/p>\n<p>\nBut we&#8217;re not programmers. And that shows.\n<\/p>\n<p>\nIn my case, programming has been my main hobby for the past 2 years or so, so I consider myself fairly proficient. I don&#8217;t have the level of experience that a professional developer does, but I have come to think like a developer. The other two &#8220;modellers&#8221; in our group don&#8217;t. They are very intelligent people, but when it comes to programming, they just don&#8217;t think the right way. In fact I think their general intelligence is making the problem worse, because it makes them too confident: they <b>assume<\/b> that they know what they&#8217;re doing.\n<\/p>\n<p>\nCase in point:<br \/>\nThis past Friday, I found a small but very important bug in code that colleague A had written, in a project that we are working on together. A number had been hard-coded as a constant, and in order to get the right results, that number needs to be changed manually once a month. Turns out that this number had been off by one for the last 3 months. And the output from this model is used to make million-dollar decisions.\n<\/p>\n<p>\n(To make things more interesting, colleague A happened to be travelling on Friday, so I couldn&#8217;t take this up with him, but had take this directly to our manager, because he was about to use the output. The rest of the day was spent in a state of moderate panic, estimating the effects of the bug, and trying to minimise the effects.)\n<\/p>\n<p>\nThe bug was in no way subtle or hard to find. The report output wasn&#8217;t behaving the way I expected, and once I&#8217;d noticed that, it took less than 10 minutes to find the error.\n<\/p>\n<p>\nHard-coding a number that needs to change is dangerous and stupid in itself. But I think it&#8217;s only a symptom of a larger problem.<br \/>\nThis really comes down to looking at code with the right mindset &#8211; and a big part of &#8220;right&#8221; is &#8220;defensive&#8221;.\n<\/p>\n<ol>\n<p>\n<b>1) Don&#8217;t assume that your code is correct.<\/b>\n<\/p>\n<p>\nThe starting point should be that your code may be broken. Even if it runs, assume that it is not doing the right thing.\n<\/p>\n<p>\nEvery time I finish a piece of code, I ask myself &#8211; where could this go wrong? How might this output, which looks OK at first glance, be wrong? How can I check it? In the case of financial models, I usually track at least one data point in each result set all the way back to the input data, manually, using pen and paper and a calculator.\n<\/p>\n<p>\nColleague A would have found the problem if he had looked, but he didn&#8217;t &#8211; because he assumed that if it doesn&#8217;t look obviously wrong, it must be right. (It&#8217;s not the first time I find such problems in his work, either.)\n<\/p>\n<p>\nIt helps that I know what can go wrong. I know to look for edge cases, off-by-one errors, misaligned data, mismatched units \/ currencies. But these are less obvious to those who have little programming experience.\n<\/p>\n<\/ol>\n<ol>\n<p>\n<b>2) Failures should be visible.<\/b>\n<\/p>\n<p>\nChecks and double-checks and triple-checks. In environments that support it, tests and checks should be automatic. If there are two routes that should get you to the same point, take both and show both results. If there are conditions that the results should satisfy, those should be checked automatically, and very loud bells should start ringing if they don&#8217;t match.<br \/>\n<br \/>\nProduce far more output than needed. Display your assumptions, and save intermediate results. Show inputs side by side with the output.\n<\/p>\n<p>\nIn this case, showing the key inputs would have made the problem very visible and we&#8217;d have discovered it immediately.\n<\/p>\n<\/ol>\n<p>\nAll of this doesn&#8217;t mean that my code is bug-free. It will never be. But it will have fewer bugs than code written by someone who doesn&#8217;t approach things this way.\n<\/p>\n<p>\nThings will improve as my colleagues gain experience &#8211; but in the meantime, mistakes like this might cost us a lot of money. So I think I need to start taking more responsibility for what they do in their code.\n<\/p>\n<p>\nThe question is, how? How do you teach someone to think the right way &#8211; especially if they don&#8217;t even think of their job as programming?\n<\/p>\n<p>\n&#8220;Leading by example&#8221; will help somewhat, but so much of this is quiet and invisible&#8230; It&#8217;s hard to make my assumptions visible. Thinking out loud while discussing our code (his and mine) might be one way. Politely adding checks and balances to the parts of his code that I review is another. But it doesn&#8217;t seem quite sufficient.\n<\/p>\n<p>\nAnd that&#8217;s assuming that this is even &#8220;learnable&#8221; &#8211; which it might not be, it might just be a more general mindset that you either have and value, or don&#8217;t&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Programming has been my main hobby for the past 2 years or so, so I have come to think like a developer. The other two &#8220;modellers&#8221; in our group don&#8217;t. They are very intelligent people, but when it comes to programming, they just don&#8217;t think the right way.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-16","post","type-post","status-publish","format-standard","hentry","category-geeky_things"],"_links":{"self":[{"href":"https:\/\/www.toomik.net\/helen\/blog\/wp-json\/wp\/v2\/posts\/16","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.toomik.net\/helen\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.toomik.net\/helen\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.toomik.net\/helen\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.toomik.net\/helen\/blog\/wp-json\/wp\/v2\/comments?post=16"}],"version-history":[{"count":0,"href":"https:\/\/www.toomik.net\/helen\/blog\/wp-json\/wp\/v2\/posts\/16\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.toomik.net\/helen\/blog\/wp-json\/wp\/v2\/media?parent=16"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.toomik.net\/helen\/blog\/wp-json\/wp\/v2\/categories?post=16"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.toomik.net\/helen\/blog\/wp-json\/wp\/v2\/tags?post=16"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}