Code Reviews have a bad reputation. It seems easy to do them poorly, to create friction, to hurt people, and to waste both time and energy doing them. For this reason, I developed a simple serious game: “What’s Not To Like About This Code?” I’m going to practise criticizing code in public. I’ve decided to do this for two key reasons: It might help readers by making concrete some of the abstract-sounding advice that I provide. It might increase the amount of civility in code criticis...| The Code Whisperer
A mentoring client of mine said this recently: I took ownership of some strategically pivotal code and did precisely that, typically refactoring twice a week for about 18 months (my delivery was unaffected). The refactorings took at most half an hour. Last week I got reprimanded. I’ve read the room and stopped. Someone asked why someone reprimanded them, from which came this reply: I should be refactoring under an initiative, the colleague felt. I said I was refactoring to the Four Rules of...| The Code Whisperer
What is refactoring? You’d think that, in 2022, we’d have long settled this question already. Yes and no. A Problem I routinely encounter programmers who struggle with refactoring because they become stuck on this central question: I’m making these kinds of code changes (describes example)… am I refactoring? They might arrive at this central question because some well-meaning person has looked at what they’re doing and exclaimed, “That’s not refactoring!” To someone trying har...| The Code Whisperer
Someone is circulating a talk—a concise one, which I appreciate—entitled “Refactoring is a code-smell” (sic). Before I continue, let me make a few things clear: I do not wish to shame nor criticize the speaker. Their talk triggered this article, but… I have not seen the talk; I’ve only been given one viewer’s impression it and… I am not trying to rebut/refute this talk, but rather I want to say a few things about the claim as I understand it. Please take a moment before you co...| The Code Whisperer
I noticed that Ron Jeffries had written about a kata I hadn’t tried before, so I decided to try it. The exercise involves evaluating a polynomial of one variable at a single point. For example: 5x3 + 4x2 + 19x + 3 at the point x = 5 evaluates to 823. Check my arithmetic if you like. How does one write this incrementally and test-first? First things first: if you’ve never tried it, and especially if you’re new to test-first programming, then try it yourself. Take small steps and never wr...| The Code Whisperer
When I teach evolutionary design with TDD, I often encounter programmers who don’t like all these little classes and all these extra interfaces. (Or all these little higher-order functions, if you prefer. It’s the same problem.) They worry that it creates a serious performance problem: deeper call stacks, passing parameters around, that kind of thing. They see memory being unnecessarily allocated and freed, memory becoming fragmented, and garbage collection happening at the worst possible...| The Code Whisperer
Over at The 260 Bowler I’ve added a handful of diary entries (just blog posts), so now I want a diary page (think recent posts). I started simply, iterating over all the entries, summarizing them, then starting to add nice styles so that they look vaguely like a diary page. I’ll make it look really nice before I move on. I thought, “Hey! I should show these in reverse order, and then limit then to something like 5 or 10 entries.” When I thought about how to do that, I immediately jump...| The Code Whisperer
In my evolutionary design practice, I consider removing duplication one of the three fundamental moves.1 Accordingly, I practise this move daily and trust it as a primary tactic for understanding the needs of the design. I would like to share a little example with you. We encountered this example during a session of Evolutionary Design Without* Tests. We’re adding a feature to the Point of Sale system: adding provincial sales tax to each item as the shopper purchases it. The system already ...| The Code Whisperer
Complicated code only creates problems when humans try to change it. When we try to change code, we spend energy understanding it, and so we label code “complicated” when it feels like it requires “too much” energy to understand. That’s why we design software: we organize code in a way that reduces the cost of understanding it. “Too many” if conditions represents just one way to write complicated code. Most of us don’t immediately grasp long chains or deeply-branching trees of...| The Code Whisperer
Refactoring, the activity, involves the following things. improving the design of existing code … in a sequence of small transformations … that preserve the important behavior of the system … which you can complete relatively quickly … and which gives you inexpensive options to change direction. Effective refactoring combines the value of Real Options thinking with the care and attention of engineering. It reduces volatility in the marginal cost of adding features. It reduces the over...| The Code Whisperer
You don’t hate mocks; you hate side-effects.1 When a mock annoys you, it realizes its purpose, showing you where a side-effect is getting in your way. If you refactor away from the side-effect, then you eliminate the mock. The mock is a consequence of your pain, not the cause of it. Please don’t blame the poor mock for doing its job. Clarifying Terms Here, when I say mock I specifically mean an expectation, often implemented by library functions with names like verify(), expect().toHaveBe...| The Code Whisperer
When I teach evolutionary design, participants still often ask me which tools they ought to use to introduce test doubles into their projects. Yes, I nudge them gently in the direction of focusing on principles and techniques and away from fixating on tools, but they still need a place to start. This question often comes in the following form: Do you favor hand-rolling test doubles or using a library (like NSubstitute, Mockito, or rspec-mock)? I choose the option that balances removing duplic...| The Code Whisperer
If you add a port in Elm code, then you need to use that port in your Elm code, otherwise the generated Javascript might not even know that your Elm code uses ports at all. As I learn Elm and become accustomed to its compiler’s wonderfully-detailed error messages, encountering error messages like this suddenly feel jarring and I even have trouble understanding them for a moment. TypeError: app.ports is undefined I saw this message in the browser’s Javascript console when I tried to add th...| The Code Whisperer
How do object hierarchies evolve? Many programmers struggle with this question. Mostly, they want to know how object hierarchies can evolve safely and sensibly! They have a picture in their mind that TDD is only safe in the hands of programmers with excellent design sense, but I believe that anyone can learn these skills with a combination of simple guidelines and guided practice. It would be nice to have more examples. I have stumbled upon one that I’d like to share with you. I’m refacto...| The Code Whisperer
A recent tweet caught my attention. The drawback of ‘contract’ as a metaphor is that it leads to discussion as to whether a party is right (conforms) or wrong (violates) instead of overall outcome (what’s the best way to make the system meet the needs of the stakeholders in our technical/economic context). — Nat Pryce (@natpryce) May 28, 2019 When I read this, one thought immediately came to my mind: are people really doing this?! And of course they are, because people relentlessly fi...| The Code Whisperer
Doing (test && commit) || revert amounts to doing TDD very carefully, as an étude. If you already routinely take tiny steps, then you already almost do TCR, but something significant might change if you do it exactly. This remains to discover. Join me. References Kent Beck, “test && commit || revert”. Kent describes the technique and makes it clear: he doesn’t know exactly how it affects the programmers just yet. James Shore, “Études for Excellence”. James and I both see the XP pr...| The Code Whisperer
A reader asked me: Regarding contract tests, would you group both expectation and assertion checks together or would you keep them as separate checks along the lines of each separate line in your talk? The very short answer is “neither”. You can find the only slightly longer answer here.| The Code Whisperer
I taught evolutionary design in person to about 200 programmers this year.1 In the 15 years that I’ve been doing this, I’ve spoken with confused programmers who felt unsure how to use dependency injection to improve their design. I tend to notice this confusion more often among the programmers who have “some experience” practising test-driven development—say, 6 months to 2 years. I’d like to share a few short pieces of advice, and if this advice leads you to ask some follow-up que...| The Code Whisperer
“I want to get a lot of wisdom on TDD techniques”. Many people ask me about this, especially when they sign up for my mailing list. They want the “advanced stuff”. I have good news and bad news for them: I don’t believe in “advanced TDD”. If you want advanced testing techniques, then you’re probably looking for techniques that will make your code worse, not better. (I don’t want you to do that.) If you want “advanced TDD”, then you’ve probably missed the single most im...| The Code Whisperer
A recent experience with a hotel coffee machine illustrated one primary difference of philosophy related to software design. It’s a Keurig machine and my wife Sarah was figuring out how to use it to make coffee. She noticed that when she inserted a K-Cup (the little containers of coffee), then the lid for the water reservoir popped open, so that she could fill it with water. At first, that seemed helpful, but immediately, I wondered about being able to open that reservoir even without inser...| The Code Whisperer
I don’t like to inherit implementation (create subclasses).1 When refactoring class hierarchies, I often pull behavior up into superclasses, then out into collaborators.2 Over time, I might find myself left with subclasses that only differ by overriding methods to return different constant values. Smalltalkers are used to this, but I never felt entirely comfortable with it. (No, I can’t explain that.) I often end up here because I’ve moved all the significant behavior variations out int...| The Code Whisperer
I’ve been teaching programmers about the value of isolated tests1 for a long time, and recently I’ve seen increasing resistance to the idea. I worry that this comes partly from having presented motivations and reasons that tends towards the overly-abstract, ironically enough. Perhaps I can improve matters by returning to the concrete issues and constraints that led me to being exploring this idea in the first place. Consider the following questions. When I want to fix a problem within the...| The Code Whisperer
The name template method is a perfect example of a structural name: it describes the implementation rather than the meaning or purpose. This is what happens when programmers name things. The Problem We have standard workflows in our systems that programmers copy and paste from module to module in order to achieve some kind of standard behavior. Programmers call this boilerplate. Programmers tend to copy and paste this code because — They don’t see the duplication, because usually not much...| The Code Whisperer
Many programmers believe that tests have special permission to repeat themselves, arguing that this repetition makes those tests “simpler” or “easier to read”. I challenge that claim in this free preview article at The jbrains Experience, where members get personalized consulting and answers to their questions.| The Code Whisperer
In 2014 I read an article with the provocative title “Mockists are Dead. Long live Classicists.”. In it, the author frames mock objects (I assume that they mean all test doubles, as opposed to specifically method/function expectations) as “dangerous” and warns against a mistake he calls “tautological TDD”, which he describes as writing tests that merely repeat an idea in different words. This sounds risky to me, and something I’d probably want to warn against, but not something ...| The Code Whisperer
At Øredev 2013, I asked managers to tell me what makes their job difficult. I spoke to one manager who told me that they face immense pressure on two fronts: from the board of directors to deliver more features more often and from the programmers to give them more time to refactor. I’ve heard dozens of stories like these over the years and I’ve found a trick that helps. It doesn’t resolve the problem, but it helps take a significant step in the direction of a resolution. Warning. You a...| The Code Whisperer
The bottom line: Modularity. Details. Pick one. I often spend time with programmers who don’t like small methods and small classes, in spite of all the benefits of small methods and small classes: easier to test easier to read, quicker to skim costs less to understand, less to understand at once easier to build correctly, quicker to build easier to compose into working systems These programmers range from feeling vaguely uncomfortable to showing strong and open antipathy towards small metho...| The Code Whisperer
TDD is for those who don't know how to design software, which doesn't have to mean that we're all dopes who are doomed to perpetual failure. Let's explore th...| blog.thecodewhisperer.com
A guard clause might be an embryonic parser, so what happens if you nudge it in that direction?| blog.thecodewhisperer.com
It's easy to give the instruction to **reveal intent**, but harder to provide helpful examples. I'd like to provide a tiny one that illustrates the point qui...| blog.thecodewhisperer.com
When you notice that you need| blog.thecodewhisperer.com
Let's look at a simple example of a name. Let's judge the name (kindly!), then imagine some likely next steps in refactoring. We can learn and do quite a lot...| blog.thecodewhisperer.com
The stronger your refactoring skill, the more easily you can use architecture advice as guidelines instead of as rules to enforce. This makes it significantl...| blog.thecodewhisperer.com
Just another little example of two people looking at a situation, one seeing a problem and the other seeing a solution.| blog.thecodewhisperer.com