文章目录
Working with Code Working with Code Working with Code
My notes on chapter 3 “Working with Code” from the book The Missing README by Chris Riccomini Dmitriy Ryaboy.
Software is probably the most complex thing human beings have ever created. Software is prone to get messier over time, a phenomenon called software entropy. Software entripy can be caused by differences in code style among developers, evolving tech stacks, bug fixes and many more. Each cause has its own solution, as we’ll learn along the way. This might include bug detection tools, code reviews, continuous refactoring, and more.
Technical Debt
Technical debt is future work owed to solve existing problems. It is a major cause of softwate entropy. Like financial debt, technical debt has principal and interests. Principal is the original shortcoming that needs to be solved, and interests are the increasing workarounds that is gradually added over the original shortcoming.
Technical debt can be divided into a 2*2 matrix:
| Reckless | Prudent | |
|---|---|---|
| Deliberate | We don’t have time for design | Good Technical Debt |
| Inadvertant | Caused by inability | A natural outcome of learning |
Adressing Technical Debt
In the short term, paying technical debt slows down shipping, whereas in the long term, paying debt speeds up shipping. The balance is highly context-specific. But now, with the help of coding agents, it is easier to make minor cleanups and refactorings as you go. However, large refactorings are still a serious topic to be discussed with your team.
A good framework to discuss technical debt with your team would be:
- State the situation factually
- Describe the risk and cost of the debt
- Propose a solution
- Discuss alternatives (including not taking action)
- Weigh the trade-offs
Changing Code
Changing code is (often) more difficult than writing code in a fresh repository, because you have to make sure that you don’t break things, and adhere to existing styles.
Recommended Steps to Modify Existing Code
- Identify change points
- first read the code, experiment, and ask questions
- Find test points
- Test points are the interfaces where you write tests, typically unit tests
- Break dependencies
- Sometimes we have to break dependencies to locate test points
- In this context, dependencies are objects and methods that are required to test your code, not external libraries or services.
- Modify the code structure so that the code you want to test can be isolated ffrom other code it depends on.
- This typically means using dependency injection over constructing dependencies inside a method, and breaking large methods into multiple smaller ones.
- And thus we can control whether we want to mock such dependency in test code. For internal dependencies, don’t mock them. For external dependencies, mock them.
- Write tests
- Add tests that validate the changes
- The first 4 steps are essentially buiding a fence around the code that you will change.
- Make changes and refactor
Refactor Incrementally and Pragmatically
Make refactoring commits and PRs separate from feature commits, so that it is easier to roll back features without affecting refactors.
Refactor along the way and leave code cleaner than you found it (eliminate code smell as you go).
Be pragmatic about refactoring. Consider deadlines and company priorities before refactoring. Don’t refactor code that is rarely used or is about to be deprecated.
Use IDEs when refactoring
IDEs have tools for renaming and moving code, extracting methods and fields, updating method signatures, and doing other common operations.
// TODO: this is interesting. We should probably learn to use a C++ IDE even in the age of AI
Avoiding Pitfalls
Use Boring Technology
The problem with new technology is that it’s less mature. Old technology break in predictable ways and new technologies break in surprising ways.
Bleed responsibly: Adopt bleeding-edge technologies where the risks are manageable, and avoid them where failure or migration would be expensive.
// TODO: I wonder how much this is still true in the age of AI when you can quickly experiment with things
Don’t Go Rogue
Stick with your company/industry’s coding/tech standards even if you don’t like them. They exist for a reason, probably for something nonobvious.
Don’t Fork Without Committing Upstream (within a firm)
The original title for this section is simply “don’t fork without committing upstream”, but I find it fun to just fork any open-source project I’m interested, play with it, and customize it the way I want, without committing upstream. So this might not apply to all scenario.
But within a firm, maintaining an internal fork can be nasty. Devs often say that they will commit back “later” but its often never happens, and different forks diverge as time goes. Obviously it goes against the DRY principle, and the mental model held by team members about the projects gradually drift away.
Resist the Temtation to Rewerite
Refactoring efforts often escalate into full-blown rewrites. Be pragmatic about rewrites. I think this is especially true in the AI era when everyone just rewrites everything in Rust. From my point of view many of these Rust rewrites make no sense. It just burns a ton of tokens and make people feel like they have accomplished significant performance optimization.
The cost of a rewrite can be high, especially when it comes to data migration.