A place to cache linked articles (think custom and personal wayback machine)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.md 24KB

4 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. title: Write code that is easy to delete, not easy to extend
  2. url: http://programmingisterrible.com/post/139222674273/write-code-that-is-easy-to-delete-not-easy-to
  3. hash_url: 7cd1ba7e421234316758516bff8be157
  4. <blockquote>
  5. <p>“Every line of code is written without reason, maintained out of weakness, and deleted by chance” <em>Jean-Paul Sartre’s Programming in ANSI C.</em></p>
  6. </blockquote>
  7. <p>Every line of code written comes at a price: maintenance. To avoid paying for a lot of code, we build reusable software. The problem with code re-use is that it gets in the way of changing your mind later on.</p>
  8. <p>The more consumers of an API you have, the more code you must rewrite to introduce changes. Similarly, the more you rely on an third-party api, the more you suffer when it changes. Managing how the code fits together, or which parts depend on others, is a significant problem in large scale systems, and it gets harder as your project grows older.</p>
  9. <blockquote>
  10. <p>My point today is that, if we wish to count lines of code, we should not regard them as “lines produced” but as “lines spent” <em>EWD 1036</em></p>
  11. </blockquote>
  12. <p>If we see ‘lines of code’ as ‘lines spent’, then when we delete lines of code, we are lowering the cost of maintenance. Instead of building re-usable software, we should try to build disposable software.</p>
  13. <p>I don’t need to tell you that deleting code is more fun than writing it.</p>
  14. <p>To write code that’s easy to delete: repeat yourself to avoid creating dependencies, but don’t repeat yourself to manage them. Layer your code too: build simple-to-use APIs out of simpler-to-implement but clumsy-to-use parts. Split your code: isolate the hard-to-write and the likely-to-change parts from the rest of the code, and each other. Don’t hard code every choice, and maybe allow changing a few at runtime. Don’t try to do all of these things at the same time, and maybe don’t write so much code in the first place.</p>
  15. <p><br/></p>
  16. <h1>Step 0: Don’t write code</h1>
  17. <p>The number of lines of code doesn’t tell us much on its own, but the magnitude does 50, 500 5,000, 10,000, 25,000, etc. A million line monolith is going to be more annoying than a ten thousand line one and significantly more time, money, and effort to replace.</p>
  18. <p>Although the more code you have the harder it is to get rid of, saving one line of code saves absolutely nothing on its own.</p>
  19. <p>Even so, the easiest code to delete is the code you avoided writing in the first place.</p>
  20. <p><br/></p>
  21. <h1>Step 1: Copy-paste code</h1>
  22. <p>Building reusable code is something that’s easier to do in hindsight with a couple of examples of use in the code base, than foresight of ones you might want later. On the plus side, you’re probably re-using a lot of code already by just using the file-system, why worry that much? A little redundancy is healthy.</p>
  23. <p>It’s good to copy-paste code a couple of times, rather than making a library function, just to get a handle on how it will be used. Once you make something a shared API, you make it harder to change.</p>
  24. <p>The code that calls your function will rely on both the intentional and the unintentional behaviours of the implementation behind it. The programmers using your function will not rely on what you document, but what they observe.</p>
  25. <p>It’s simpler to delete the code inside a function than it is to delete a function.</p>
  26. <p><br/></p>
  27. <h1>Step 2: Don’t copy paste code</h1>
  28. <p>When you’ve copy and pasted something enough times, maybe it’s time to pull it up to a function. This is the “save me from my standard library” stuff: the “open a config file and give me a hash table”, “delete this directory”. This includes functions without any state, or functions with a little bit of global knowledge like environment variables. The stuff that ends up in a file called “util”.</p>
  29. <p>Aside: Make a <code>util</code> directory and keep different utilities in different files. A single <code>util</code> file will always grow until it is too big and yet too hard to split apart. Using a single <code>util</code> file is unhygienic.</p>
  30. <p>The less specific the code is to your application or project, the easier they are to re-use and the less likely to change or be deleted. Library code like logging, or third party APIs, file handles, or processes. Other good examples of code you’re not going to delete are lists, hash tables, and other collections. Not because they often have very simple interfaces, but because they don’t grow in scope over time.</p>
  31. <p>Instead of making code easy-to-delete, we are trying to keep the hard-to-delete parts as far away as possible from the easy-to-delete parts.</p>
  32. <p><br/></p>
  33. <h1>Step 3: Write more boilerplate</h1>
  34. <p>Despite writing libraries to avoid copy pasting, we often end up writing a lot more code through copy paste to use them, but we give it a different name: boilerplate. Boiler plate is a lot like copy-pasting, but you change some of the code in a different place each time, rather than the same bit over and over.</p>
  35. <p>Like with copy paste, we are duplicating parts of code to avoid introducing dependencies, gain flexibility, and pay for it in verbosity.</p>
  36. <p>Libraries that require boilerplate are often stuff like network protocols, wire formats, or parsing kits, stuff where it’s hard to interweave policy (what a program should do), and protocol (what a program can do) together without limiting the options. This code is hard to delete: it’s often a requirement for talking to another computer or handling different files, and the last thing we want to do is litter it with business logic.</p>
  37. <p>This is not an exercise in code reuse: we’re trying keep the parts that change frequently, away from the parts that are relatively static. Minimising the dependencies or responsibilities of library code, even if we have to write boilerplate to use it.</p>
  38. <p>You are writing more lines of code, but you are writing those lines of code in the easy-to-delete parts.</p>
  39. <p><br/></p>
  40. <h1>Step 4: Don’t write boilerplate</h1>
  41. <p>Boilerplate works best when libraries are expected to cater to all tastes, but sometimes there is just too much duplication. It’s time to wrap your flexible library with one that has opinions on policy, workflow, and state. Building simple-to-use APIs is about turning your boilerplate into a library.</p>
  42. <p>This isn’t as uncommon as you might think: One of the most popular and beloved python http clients, <code>requests</code>, is a successful example of providing a simpler interface, powered by a more verbose-to-use library <code>urllib3</code> underneath. <code>requests</code> caters to common workflows when using http, and hides many practical details from the user. Meanwhile, <code>urllib3</code> does the pipelining, connection management, and does not hide anything from the user.</p>
  43. <p>It is not so much that we are hiding detail when we wrap one library in another, but we are separating concerns: <code>requests</code> is about popular http adventures, <code>urllib3</code> is about giving you the tools to choose your own adventure.</p>
  44. <p>I’m not advocating you go out and create a <code>/protocol/</code> and a <code>/policy/</code> directory, but you do want to try and keep your <code>util</code> directory free of business logic, and build simpler-to-use libraries on top of simpler-to-implement ones. You don’t have to finish writing one library to start writing another atop.</p>
  45. <p>It’s often good to wrap third party libraries too, even if they aren’t protocol-esque. You can build a library that suits your code, rather than lock in your choice across the project. Building a pleasant to use API and building an extensible API are often at odds with each other.</p>
  46. <p>This split of concerns allows us to make some users happy without making things impossible for other users. Layering is easiest when you start with a good API, but writing a good API on top of a bad one is unpleasantly hard. Good APIs are designed with empathy for the programmers who will use it, and layering is realising we can’t please everyone at once.</p>
  47. <p>Layering is less about writing code we can delete later, but making the hard to delete code pleasant to use (without contaminating it with business logic).</p>
  48. <p><br/></p>
  49. <h1>Step 5: Write a big lump of code</h1>
  50. <p>You’ve copy-pasted, you’ve refactored, you’ve layered, you’ve composed, but the code still has to do something at the end of the day. Sometimes it’s best just to give up and write a substantial amount of trashy code to hold the rest together.</p>
  51. <p>Business logic is code characterised by a never ending series of edge cases and quick and dirty hacks. This is fine. I am ok with this. Other styles like ‘game code’, or ‘founder code’ are the same thing: cutting corners to save a considerable amount of time.</p>
  52. <p>The reason? Sometimes it’s easier to delete one big mistake than try to delete 18 smaller interleaved mistakes. A lot of programming is exploratory, and it’s quicker to get it wrong a few times and iterate than think to get it right first time.</p>
  53. <p>This is especially true of more fun or creative endeavours. If you’re writing your first game: don’t write an engine. Similarly, don’t write a web framework before writing an application. Go and write a mess the first time. Unless you’re psychic you won’t know how to split it up.</p>
  54. <p>Monorepos are a similar tradeoff: You won’t know how to split up your code in advance, and frankly one large mistake is easier to deploy than 20 tightly coupled ones.</p>
  55. <p>When you know what code is going to be abandoned soon, deleted, or easily replaced, you can cut a lot more corners. Especially if you make one-off client sites, event web pages. Anything where you have a template and stamp out copies, or where you fill in the gaps left by a framework.</p>
  56. <p>I’m not suggesting you write the same ball of mud ten times over, perfecting your mistakes. To quote Perlis: “Everything should be built top-down, except the first time”. You should be trying to make new mistakes each time, take new risks, and slowly build up through iteration.</p>
  57. <p>Becoming a professional software developer is accumulating a back-catalogue of regrets and mistakes. You learn nothing from success. It is not that you know what good code looks like, but the scars of bad code are fresh in your mind.</p>
  58. <p>Projects either fail or become legacy code eventually anyway. Failure happens more than success. It’s quicker to write ten big balls of mud and see where it gets you than try to polish a single turd.</p>
  59. <p>It’s easier to delete all of the code than to delete it piecewise.</p>
  60. <p><br/></p>
  61. <h1>Step 6: Break your code into pieces</h1>
  62. <p>Big balls of mud are the easiest to build but the most expensive to maintain. What feels like a simple change ends up touching almost every part of the code base in an ad-hoc fashion. What was easy to delete as a whole is now impossible to delete piecewise.</p>
  63. <p>In the same we have layered our code to separate responsibilities, from platform specific to domain specific, we need to find a means to tease apart the logic atop.</p>
  64. <blockquote>
  65. <p>[Start] with a list of difficult design decisions or design decisions which are likely to change. Each module is then designed to hide such a decision from the others. <em>D. Parnas</em></p>
  66. </blockquote>
  67. <p>Instead of breaking code into parts with common functionality, we break code apart by what it does not share with the rest. We isolate the most frustrating parts to write, maintain, or delete away from each other.</p>
  68. <p>We are not building modules around being able to re-use them, but being able to change them.</p>
  69. <p>Unfortunately, some problems are more intertwined and hard to separate than others. Although the single responsibility principle suggests that ‘each module should only handle one hard problem’, it is more important that ‘each hard problem is only handled by one module’</p>
  70. <p>When a module does two things, it is usually because changing one part requires changing the other. It is often easier to have one awful component with a simple interface, than two components requiring a careful co-ordination between them.</p>
  71. <blockquote>
  72. <p>I shall not today attempt further to define the kinds of material I understand to be embraced within that shorthand description [”loose coupling”], and perhaps I could never succeed in intelligibly doing so. But I know it when I see it, and the code base involved in this case is not that. <em>SCOTUS Justice Stewart</em></p>
  73. </blockquote>
  74. <p>A system where you can delete parts without rewriting others is often called loosely coupled, but it’s a lot easier to explain what one looks like rather than how to build it in the first place.</p>
  75. <p>Even hardcoding a variable <em>once</em> can be loose coupling, or using a command line flag over a variable. Loose coupling is about being able to change your mind without changing too much code.</p>
  76. <p>For example, Microsoft Windows has internal and external APIs for this very purpose. The external APIs are tied to the lifecycle of desktop programs, and the internal API is tied to the underlying kernel. Hiding these APIs away gives Microsoft flexibility without breaking too much software in the process.</p>
  77. <p>HTTP has examples of loose coupling too: Putting a cache in front of your HTTP server. Moving your images to a CDN and just changing the links to them. Neither breaks the browser.</p>
  78. <p>HTTP’s error codes are another example of loose coupling: common problems across web servers have unique codes. When you get a 400 error, doing it again will get the same result. A 500 may change. As a result, HTTP clients can handle many errors on the programmers behalf.</p>
  79. <p>How your software handles failure must be taken into account when decomposing it into smaller pieces. Doing so is easier said than done.</p>
  80. <blockquote>
  81. <p>I have decided, reluctantly to use <span class="latex">L<sup>a</sup>T<sub>e</sub>X</span>. <em>Making reliable distributed systems in the presence of software errors. Armstrong, 2003</em></p>
  82. </blockquote>
  83. <p>Erlang/OTP is relatively unique in how it chooses to handle failure: supervision trees. Roughly, each process in an Erlang system is started by and watched by a supervisor. When a process encounters a problem, it exits. When a process exits, it is restarted by the supervisor.</p>
  84. <p>(These supervisors are started by a bootstrap process, and when a supervisor encounters a fault, it is restarted by the bootstrap process)</p>
  85. <p>The key idea is that it is quicker to fail-fast and restart than it is to handle errors. Error handling like this may seem counter-intuitive, gaining reliability by giving up when errors happen, but turning things off-and-on again has a knack for suppressing transient faults.</p>
  86. <p>Error handling, and recovery are best done at the outer layers of your code base. This is known as the end-to-end principle. The end-to-end principle argues that it is easier to handle failure at the far ends of a connection than anywhere in the middle. If you have any handling inside, you still have to do the final top level check. If every layer atop must handle errors, so why bother handling them on the inside?</p>
  87. <p>Error handling is one of the many ways in which a system can be tightly bound together. Thre are many other examples of tight coupling, but it is a little unfair to single one out as being badly designed. Except for IMAP.</p>
  88. <p>In IMAP almost every each operation is a snowflake, with unique options and handling. Error handling is painful: errors can come halfway through the result of another operation.</p>
  89. <p>Instead of UUIDs, IMAP generates unique tokens to identify each message. These can change halfway through the result of an operation too. Many operations are not atomic. It took more than 25 years to get a way to move email from one folder to another that reliably works. There is a special UTF-7 encoding, and a unique base64 encoding too.</p>
  90. <p>I am not making any of this up.</p>
  91. <p>By comparison, both file systems and databases make much better examples of remote storage. With a file system, you have a fixed set of operations, but a multitude of objects you can operate on.</p>
  92. <p>Although SQL may seem like a much broader interface than a filesystem, it follows the same pattern. A number of operations on sets, and a multitude of rows to operate on. Although you can’t always swap out one database for another, it is easier to find something that works with SQL over any homebrew query language.</p>
  93. <p>Other examples of loose coupling are other systems with middleware, or filters and pipelines. For example, Twitter’s Finagle uses a common API for services, and this allows generic timeout handling, retry mechanisms, and authentication checks to be added effortlessly to client and server code.</p>
  94. <p>(I’m sure if I didn’t mention the UNIX pipeline here someone would complain at me)</p>
  95. <p>First we layered our code, but now some of those layers share an interface: a common set of behaviours and operations with a variety of implementations. Good examples of loose coupling are often examples of uniform interfaces.</p>
  96. <p>A healthy code base doesn’t have to be perfectly modular. The modular bit makes it way more fun to write code, in the same way that Lego bricks are fun because they all fit together. A healthy code base has some verbosity, some redundancy, and just enough distance between the moving parts so you won’t trap your hands inside.</p>
  97. <p>Code that is loosely coupled isn’t necessarily easy-to-delete, but it is much easier to replace, and much easier to change too.</p>
  98. <p><br/></p>
  99. <h1>Step 7: Keep writing code</h1>
  100. <p>Being able to write new code without dealing with old code makes it far easier to experiment with new ideas. It isn’t so much that you should write microservices and not monoliths, but your system should be capable of supporting one or two experiments atop while you work out what you’re doing.</p>
  101. <p>Feature flags are one way to change your mind later. Although feature flags are seen as ways to experiment with features, they allow you to deploy changes without re-deploying your software.</p>
  102. <p>Google Chrome is a spectacular example of the benefits they bring. They found that the hardest part of keeping a regular release cycle, was the time it took to merge long lived feature branches in.</p>
  103. <p>By being able to turn the new code on-and-off without recompiling, larger changes could be broken down into smaller merges without impacting existing code. With new features appearing earlier in the same code base, it made it more obvious when long running feature developement would impact other parts of the code.</p>
  104. <p>A feature flag isn’t just a command line switch, it’s a way of decoupling feature releases from merging branches, and decoupling feature releases from deploying code. Being able to change your mind at runtime becomes increasingly important when it can take hours, days, or weeks to roll out new software. Ask any SRE: Any system that can wake you up at night is one worth being able to control at runtime.</p>
  105. <p>It isn’t so much that you’re iterating, but you have a feedback loop. It is not so much you are building modules to re-use, but isolating components for change. Handling change is not just developing new features but getting rid of old ones too. Writing extensible code is hoping that in three months time, you got everything right. Writing code you can delete is working on the opposite assumption.</p>
  106. <p>The strategies i’ve talked about — layering, isolation, common interfaces, composition — are not about writing good software, but how to build software that can change over time.</p>
  107. <blockquote>
  108. <p>The management question, therefore, is not whether to build a pilot system and throw it away. You will do that. […] Hence plan to throw one away; you will, anyhow. <em>Fred Brooks</em></p>
  109. </blockquote>
  110. <p>You don’t need to throw it all away but you will need to delete some of it. Good code isn’t about getting it right the first time. Good code is just legacy code that doesn’t get in the way.</p>
  111. <p>Good code is easy to delete.</p>
  112. <h1>Acknowledgments</h1>
  113. <p>Thank you to all of my proof readers for your time, patience, and effort.</p>
  114. <h1>Further Reading</h1>
  115. <h2>Layering/Decomposition</h2>
  116. <p><a href="http://t.umblr.com/redirect?z=https%3A%2F%2Fwww.cs.umd.edu%2Fclass%2Fspring2003%2Fcmsc838p%2FDesign%2Fcriteria.pdf&amp;t=OGM4ZWM5ZWEyZjA5M2VhM2ZiMjdjN2ZmNzQyY2QwNTdhYzdmY2MwMCxtMjBlanFUSQ%3D%3D">On the Criteria To Be Used in Decomposing Systems into Modules</a>, D.L. Parnas.</p>
  117. <p><a href="http://t.umblr.com/redirect?z=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DaAb7hSCtvGw&amp;t=ZGFlZGRlNmYxOTY5MzNlYzBiN2UzNTA3ZTUwZmI5YWVjM2ZiMDkyYixtMjBlanFUSQ%3D%3D">How To Design A Good API and Why it Matters</a>, J. Bloch.</p>
  118. <p><a href="http://t.umblr.com/redirect?z=http%3A%2F%2Fwww4.in.tum.de%2F%7Eblanchet%2Fapi-design.pdf&amp;t=NDIwMWQ0OTI5NWZmNGY2ZTc1NWY1MTc3ZGY3MDFkYmM0MmQ5MzE0YixtMjBlanFUSQ%3D%3D">The Little Manual of
  119. API Design</a>, J. Blanchette.</p>
  120. <p><a href="http://t.umblr.com/redirect?z=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DuqIx4VRIuwM&amp;t=MjlmMDhkZDJmZDM5OTUxZDZhNTk1NzI5MmRkMzg2YjIzMjEyMmFmNyxtMjBlanFUSQ%3D%3D">Python for Humans</a>, K. Reitz.</p>
  121. <h1>Common Interfaces</h1>
  122. <p><a href="http://t.umblr.com/redirect?z=https%3A%2F%2Fwww.rand.org%2Fcontent%2Fdam%2Frand%2Fpubs%2Fnotes%2F2009%2FN3017.pdf&amp;t=NjkzNzM5ODYwNzg2YWIwMWVmZWZhZTUzYWZjYWY5NGMxYmVmZjM2NyxtMjBlanFUSQ%3D%3D">The Design of the MH Mail System</a>, a Rand technical report.</p>
  123. <p><a href="http://t.umblr.com/redirect?z=http%3A%2F%2Fwww.vitanuova.com%2Finferno%2Fpapers%2Fstyx.pdf&amp;t=YmU4ZWY4ZDRhYjRkZmE4NTExOWQwZjg4Y2UyMDg0MDIxNDdhZTY3YixtMjBlanFUSQ%3D%3D">The Styx Architecture for Distributed Systems</a></p>
  124. <p><a href="http://t.umblr.com/redirect?z=http%3A%2F%2Fmonkey.org%2F%7Emarius%2Ffunsrv.pdf&amp;t=NGRmYWNjYTI1YzIzMzFiMzllNzg2MTBkODAwMDNiOTdhZDRjY2VjZCxtMjBlanFUSQ%3D%3D">Your Server as a Function</a>, M. Eriksen.</p>
  125. <h2>Feedback loops/Operations lifecycle</h2>
  126. <p><a href="http://t.umblr.com/redirect?z=http%3A%2F%2Fwww.slideshare.net%2FJolicloud%2Fchrome-release-cycle&amp;t=NGU2NzQ5MjhmZjdlMzJjYjc3NzJiODQ5MTVkMWE0MGYxNzFiMzEyMSxtMjBlanFUSQ%3D%3D">Chrome Release Cycle</a>, A. Laforge.</p>
  127. <p><a href="http://t.umblr.com/redirect?z=http%3A%2F%2Fwww.hpl.hp.com%2Ftechreports%2Ftandem%2FTR-85.7.pdf&amp;t=NzhkZGUxYzEwZWY0MzQyZWQ0MjYyY2ZkYTMyZmVhMjA2OWIxODNjZSxtMjBlanFUSQ%3D%3D">Why Do Computers Stop and What Can Be Done About It?</a>, J. Gray.</p>
  128. <p><a href="http://t.umblr.com/redirect?z=http%3A%2F%2Fweb.mit.edu%2F2.75%2Fresources%2Frandom%2FHow+Complex+Systems+Fail.pdf&amp;t=MzllMDA2M2FkNmE1MmVhYjA4M2VmYzFhMDI0NTUwNDE1ODQzYzAyNyxtMjBlanFUSQ%3D%3D">How Complex Systems Fail</a>, R. I. Cook.</p>
  129. <h2>The technical is social before it is technical.</h2>
  130. <p><a href="http://t.umblr.com/redirect?z=http%3A%2F%2Fwww.systemsguild.com%2Fpdfs%2FDeMarcoNov2011.pdf&amp;t=ZjcyNGE2MGMxZmU3ZjM2YzEwMjVhOTlhN2ZhZDkxMmU4NTIwZGIzNCxtMjBlanFUSQ%3D%3D">All Late Projects Are the Same</a>, <a href="http://t.umblr.com/redirect?z=http%3A%2F%2Fwww.computer.org%2Fcms%2FComputer.org%2FComputingNow%2Fhomepage%2Fmostread%2FMostRead-SW-SoftwareEngineeringAnIdeaWhoseTimeHasCome.pdf&amp;t=NjZiZjEwYzk1YzViMmY0ZjE0ZTc4MTA4NWFlNDVkMzM0NjU5Yzk2MSxtMjBlanFUSQ%3D%3D">Software Engineering: An Idea Whose Time Has Come and Gone?</a>, T. DeMarco.</p>
  131. <p><a href="http://t.umblr.com/redirect?z=http%3A%2F%2Fwww.cs.yale.edu%2Fhomes%2Fperlis-alan%2Fquotes.html&amp;t=YTJlODY1NWNlMGQ4NjIzM2Y3ODk2ZTNhNmViMjVjZmEwYTIzOGMzMCxtMjBlanFUSQ%3D%3D">Epigrams in Programming</a>, A. Perlis.</p>
  132. <p><a href="http://t.umblr.com/redirect?z=http%3A%2F%2Fwww.melconway.com%2FHome%2FConways_Law.html&amp;t=OGVhMTFlYzUwYmY3MjM2OTVjM2Q2MDA2N2NkNTdjMzVhNDNiNmVkZSxtMjBlanFUSQ%3D%3D">How Do Committees Invent?</a>, M.E. Conway.</p>
  133. <p><a href="http://t.umblr.com/redirect?z=http%3A%2F%2Fwww.jofreeman.com%2Fjoreen%2Ftyranny.htm&amp;t=MmUzYjVhY2VjZjJiZWI4YjBmYjY3ZjQ0MzQ0ZWYzZGRiNjA2ZmI1NixtMjBlanFUSQ%3D%3D">The Tyranny of Structurelessness</a>, J. Freeman</p>