{"id":98,"date":"2009-03-06T08:05:25","date_gmt":"2009-03-05T22:05:25","guid":{"rendered":"http:\/\/www.doolwind.com\/blog\/?p=98"},"modified":"2009-03-06T08:05:25","modified_gmt":"2009-03-05T22:05:25","slug":"how-to-write-perfect-code","status":"publish","type":"post","link":"https:\/\/www.doolwind.com\/blog\/how-to-write-perfect-code\/","title":{"rendered":"How To Write Perfect Code"},"content":{"rendered":"<p>Have you ever written perfect code?\u00a0 If so, how often does this happen?\u00a0 If not, why not, and do you think it&#8217;s possible?\u00a0 Perfect code is something that many developers strive for but few, if any, achieve.\u00a0 In this entry, I will discuss the set of requirements that must be met for a developer to be in a position to write perfect code.<\/p>\n<p><strong>What is Perfect Code?<\/strong><\/p>\n<p>My definition for perfect code is &#8220;Code that solves a problem, without bugs, in an optimal way&#8221;.\u00a0 This has slight ambiguities, so I&#8217;ll break it down.<\/p>\n<p>1.\u00a0 Solves a problem<\/p>\n<p>The whole idea of writing the code is to solve some non-trivial problem.\u00a0 To do this perfectly, the solution must be solved for all possible cases.<\/p>\n<p>2.\u00a0 Without bugs<\/p>\n<p>The code needs to perform the required task correctly in all situations.\u00a0 This includes valid and invalid input and situations.<\/p>\n<p>3. In an optimal way<\/p>\n<p>I come from a game development background and therefore performance is important.\u00a0 Depending on the situation, optimal will take on a different meaning. \u00a0For games, this might mean that the entire game loop must run at a minimum of 30 frames per second.\u00a0 For a banking program this might mean that a transaction takes less than 10 second to complete.<\/p>\n<p><strong>Is Perfect Code Possible?<\/strong><\/p>\n<p>The simple answer is, yes.\u00a0 Code can be written that perfectly solves a problem, without bugs and can therefore be classed as perfect.\u00a0 Unlike many professions, what we create is never fully &#8220;set in stone&#8221;.\u00a0 Code can change at any level, from fixing a broken statement to re-architecting an entire application.\u00a0 When the three requirements below are met, developers are in the best possible position to write perfect code.<\/p>\n<p><strong>The Requirements (aka What&#8217;s Needed To Write Perfect Code)<\/strong><\/p>\n<p>To expect perfect code from developers, the following 3 requirements must be met:<\/p>\n<p>1. Thorough Domain Knowledge<br \/>\n2. Enough Time<br \/>\n3. Great Developers<\/p>\n<p>Just think about each of these for a minute before continuing.\u00a0 If you&#8217;re a developer, do you meet all three requirements in your current position?\u00a0 If not, which ones don&#8217;t you meet, and why?\u00a0 Feel free to post a comment.<\/p>\n<p>Below I&#8217;ll break down these requirements and show two graphs.\u00a0 One for the amount of return gained for levels of input into each requirement.\u00a0 The second graph shows the cost of achieving a set level of quality.<\/p>\n<p>Each requirement also has a limiting factor.\u00a0 This is the top reasons why developers often write imperfect code.<\/p>\n<p><strong>1. Thorough<\/strong> <strong>Domain Knowledge<\/strong><\/p>\n<p>Developers must have a good grasp on the subject matter they are to be working on.\u00a0 If you&#8217;re making a driving game, you need to know the way cars handle, what torque is and why a blowoff valve sounds so cool.\u00a0 If you&#8217;re writing an accounting program then you need to be boring, and therefore understand accounting (or is it the other way around).\u00a0 You also need to know about debits and credits and the difference between cash flow and capital.<\/p>\n<p>Moving from no domain knowledge to a simple understanding helps a lot when striving for perfect code.\u00a0 After this point, learning more about the domain helps, however the next big leap in code quality comes from having an intimate understanding of the domain (the small mark on the x-axis in the graph below).<\/p>\n<p>The following graphs show the level of quality gained by increasing domain knowledge and the cost required to achieve this.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.doolwind.com\/images\/blog\/perfect\/perfectDomain.jpg\" alt=\"\" \/><\/p>\n<p>Limiting Factor:<\/p>\n<p>It&#8217;s difficult to find someone with expert domain knowledge and takes time for people to learn.\u00a0 Also, many developers simply won&#8217;t want gain an intimate understanding of the domain.\u00a0 This can be solved by having domain experts work closely with developers.\u00a0 See <a href=\"http:\/\/www.amazon.com\/Domain-Driven-Design-Tackling-Complexity-Software\/dp\/0321125215\">Domain Driven Design<\/a> by Eric Evans for an in depth discussion on this topic.<\/p>\n<p><strong>2. Enough Time<\/strong><\/p>\n<p>Most projects I&#8217;ve seen rarely have enough time.\u00a0 Time is critical for writing perfect code as it unlocks the following factors:<\/p>\n<ol type=\"1\">\n<li>Hacks\/quick fixes won&#8217;t be      written<\/li>\n<li>There will be little to no      overtime<\/li>\n<li>Peer reviews and pair      programming become possible<\/li>\n<li>Work can be designed      rather than simply hacked together<\/li>\n<li>Work can be thoroughly      tested<\/li>\n<\/ol>\n<p>The mark on this graph shows where &#8220;enough time&#8221; is given for a project.\u00a0 If enough time is given, the quality in code jumps substantially.\u00a0 After this time though, there is a point of diminishing return as having developers continue to work on the project won&#8217;t return great gains.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.doolwind.com\/images\/blog\/perfect\/perfectTime.jpg\" alt=\"\" \/><\/p>\n<p>One point to note is that if the amount of time given to developers changes mid project, all bets are off.<\/p>\n<p>Limiting Factor:<\/p>\n<p>Money is the major limiting factor with giving developers more time.\u00a0 However it isn&#8217;t the only one.\u00a0 Competitors can bring products out before yours or customers may have strict deadlines that you must meet.\u00a0 Also, developers don&#8217;t live forever, and rarely life in the same job forever.\u00a0 The longer a project runs, the more likely you are to lose developers and therefore have to retrain and rehire more, at great cost.<\/p>\n<p><strong>3. Great Developers<\/strong><\/p>\n<p>I agree with the concept that good developers are 10 times more efficient than average developers.\u00a0 Joel Spolsky put this perfectly when he said <a href=\"http:\/\/www.joelonsoftware.com\/articles\/HighNotes.html\">&#8220;Five Antonio Salieris won&#8217;t produce Mozart&#8217;s Requiem.&#8221;<\/a> For those that haven&#8217;t seen Amadeus, he&#8217;s saying that the order of magnitude difference between good and great developers is staggering.\u00a0 Unfortunately, you just can&#8217;t get the quality you need unless you have great developers; it&#8217;s as simple as that.<\/p>\n<p>The mark on this graph show&#8217;s the level of developer skills required to write perfect code.\u00a0 About the top 5-10% of developers fit into this bracket.\u00a0 The good news is that the difference in cost between an average developer and a great isn&#8217;t the same order of magnitude higher.<\/p>\n<p><img decoding=\"async\" src=\"http:\/\/www.doolwind.com\/images\/blog\/perfect\/perfectDeveloper.jpg\" alt=\"\" \/><\/p>\n<p>Limiting Factor:<\/p>\n<p>Great developers are hard to find!\u00a0 With companies like Microsoft and Google paying crazy amounts of money, the top tier will often work for them.\u00a0 However the good news is that great developers can be trained and in the right environment, so long as you have smart people you can often turn them into great developers if they (and you) care enough about software development.<\/p>\n<p><strong>Do We Want Perfect Code?<\/strong><\/p>\n<p>That may seem like a simple question, however in reality it isn&#8217;t.\u00a0 In a vacuum, for most developers that question is rhetorical.\u00a0 Of course we want to write perfect code, we take pride in our work and we strive for excellence.\u00a0 However, when you look at the cost involved in achieving perfect code it is not always a requirement.<\/p>\n<p>This is something we must take seriously when writing software as the realities of business mean that more often than not, imperfect code is exactly what the business needs.\u00a0 It&#8217;s better to ship code with issues than to not ship code at all.<\/p>\n<p><strong>Conclusion<\/strong><\/p>\n<p>I&#8217;m sure I&#8217;ve said at least a few controversial things, so please comment and\/or email me to continue the discussion.\u00a0 I continue to strive for perfect code in my life, and whether I&#8217;ll ever reach it is irrelevant.\u00a0 I&#8217;ll leave you with a quote from one of my favourite movies:<\/p>\n<p><strong> <\/strong><\/p>\n<p>&#8220;There are no perfect men in this world, only perfect intentions.&#8221;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Have you ever written perfect code?\u00a0 If so, how often does this happen?\u00a0 If not, why not, and do you think it&#8217;s possible?\u00a0 Perfect code is something that many developers strive for but few, if any, achieve.\u00a0 In this entry, I will discuss the set of requirements that must be met for a developer to <a class=\"more-link\" href=\"https:\/\/www.doolwind.com\/blog\/how-to-write-perfect-code\/\">Read More<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[33],"tags":[29],"class_list":["post-98","post","type-post","status-publish","format-standard","hentry","category-game-development","tag-game-coding"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pgEc5-1A","_links":{"self":[{"href":"https:\/\/www.doolwind.com\/blog\/wp-json\/wp\/v2\/posts\/98","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.doolwind.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.doolwind.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.doolwind.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.doolwind.com\/blog\/wp-json\/wp\/v2\/comments?post=98"}],"version-history":[{"count":0,"href":"https:\/\/www.doolwind.com\/blog\/wp-json\/wp\/v2\/posts\/98\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.doolwind.com\/blog\/wp-json\/wp\/v2\/media?parent=98"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.doolwind.com\/blog\/wp-json\/wp\/v2\/categories?post=98"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.doolwind.com\/blog\/wp-json\/wp\/v2\/tags?post=98"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}