DevilKing's blog

冷灯看剑,剑上几分功名?炉香无需计苍生,纵一穿烟逝,万丈云埋,孤阳还照古陵

0%

Write code easy to delete

原文链接

Instead of building re-usable software, we should try to build disposable software.

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.

  • the easiest code to delete is the code you avoided writing in the first place.
  • 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.
  • 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. use utils
  • 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. write more boilerplate
  • don’t write boilerplate. good api, 足够多的封装
  • Everything should be built top-down, except the first time
  • 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.

Error handling, and recovery are best done at the outer layers of your code base. This is known as the end-to-end principle.

Good examples of loose coupling are often examples of uniform interfaces.