Archive for the ‘Solve’ Category
Tell it to your Teddy Bear
After spending more time than I should have troubleshooting a coding (actually static code analysis) problem, I shared the story with a co-worker, who reminded me that Kernighan and Pike had been there before:
Another effective technique is to explain your code to someone else. This will often cause you to explain the bug to yourself. Sometimes it takes no more than a few sentences, followed by an embarrassed “Never mind, I see what’s wrong. Sorry to bother you.” This works remarkably well; you can even use non-programmers as listeners. One university computer center kept a teddy bear near the help desk. Students with mysterious bugs were required to explain them to the bear before they could speak to a human counselor.
Brian Kernighan and Rob Pike, in The Practice of Programming
Seems that there are rules that we learn, then love to break, and are always sorry afterwards. Here’s how.
My code doesn’t work
- I’ll just try this change to see if that fixes everything. And then this change, and this other change.
- I’ve been stuck for 10 minutes, but since nobody knows this code like I do, I won’t ask anyone for help.
- I know I don’t understand what every line of code does, but it’s OK — I’ll figure out the problem anyway.
- A complete build takes only a few minutes, so there’s no need to try a 3-line sample instead.
- I’ve read the documentation and I’m sure I’ve understood it, so I won’t ask anyone for a second opinion.
- I already asked someone 3 times for help and got it, so I can’t bother him or her a fourth time.
- It doesn’t compile (cleanly, or at all) just now, but soon it will again if I just keep at it
(The only way I got home today was that somehow I did realize it was, um, OK to call the tool support line.)
What are your favorite ”justifications” for why it’s better to stay stuck than to get help, or at least take a break and share your problem with someone else?
While I’m waiting, I’ll go talk to my teddy bear.
Code Review for People
Sometimes I get the feeling that when people consider and talk about code review, they’re talking about different things but they don’t know it. I see code review everywhere during coding.
Try this: put aside the image of code review as a meeting, or even code review as an independent activity. Limit your description of code review to: someone else is reading your code, you’re discussing it together, and there’s effective writing as a result. If you see those three things happening, you’re seeing code review.
Debugging
The great hope was that code review would be solely a preventive activity: do code review before execution, and there won’t be anything to debug. That’s not going to happen. Too much code is delivered under pressure, without code review. But all is not lost. Just review the code when debugging. Look again at the three steps — how does code review, as defined above, change debugging? First, don’t debug alone. Whatever you may think of pair programming for or against, pair debugging is a must. Especially if you wrote the code, having someone else look for the problem, or better, restate the problem, is essential. Second — discussion. Debugging that rushes to a fix often produces the wrong fix. And finally, effective writing. Sure there’s the writing which is fixing the code. But what about adding comments? And perhaps more important and effective, especially if it’s a quick fix: submit a new defect or change request to your tracking system for what really needs changing to reduce the chance of this failure recurring.
Static Analysis
Tools! If I have tools that can find problems, I won’t have to review code. Yes, you will. And you should want to. Static Analysis tools are great for finding simple errors, and potential defects. For narrowing the field of focus. But then, to find out what’s really wrong … code review. Additional benefit of the tool — since it has no feelings, it can be two of you against the reviewer: read the code with a partner, discuss why the tool thinks there’s a problem and whether you agree, and again, effective writing. Not just the fix, but why that fix, and also perhaps an adjustment to the tool so it will do better next time. High-end static analysis systems let you document that right inside the tool.
Testing
Testing, and its partner, code coverage, are often seen as the opposite of code review. Whatever I couldn’t catch by code review (and static analysis) is left for exercising the code under controlled conditions, causing failures, and fixing them. Actually code review is the most important part of developer testing. Not only reviewing the code under test in order to design good white-box tests. But also reviewing the code based on the test results and coverage results. Why does the software intermittently fail this test? Why is it so difficult to cover all the branches in this function? Again, read together, discuss, write. Here the effective writing is likely to be refactoring the code. If someone wants code review “documentation” — give them a diff and a one-sentence explanation of what you did and why.
Code Review is All-Purpose
There are many more activities in the developer’s daily life. Detailed design, branching and merging, delivery — all can benefit from in-line code review with this three-step model of reading, discussion, and effective results-writing. And by acknowledging this model, we see that code review, or almost code review, is happening all the time. Just have to complete the steps to get code review that real people will do.
How Quality Really Works
As good as the misconceptions surrounding Toyota make it sound, the truth is even better.
– Stewart Anderson in Quality Digest’s Quality Insider 20-Oct-2009
Thanks to Stewart Anderson, both for bringing us the real story about how Toyota, famed for focus on quality, actually goes about doing it. Put aside for a moment what you know about ISO, QMS, metrics etc and read the carefully crafted wisdom of experience. How quality really works at Toyota. And yes, you really can apply these ideas elsewhere, even in software development.
All quotes are from the linked article by Stewart Anderson.
Toyota’s basic pattern for improving a process is based on a simple three-part model:
- Understanding the current condition.
- Developing and defining a target condition.
- Understanding and tackling problems which need to be overcome to move from the current condition to the target condition.
This model has learning at its heart [to identify] actions to solve problems in the current condition.
The primary responsibility [of the team leader] is to monitor the process, ensure that standard work is being followed, and coach and mentor the work team in improving the process. Team leaders receive special training in process improvement and problem solving ….
To read the full article click here.
Related reading: Gemba Kaizen: A Commonsense, Low-Cost Approach to Management (my review here).
Full disclosure: My Toyota is over 15 years old and still running fine. My last car was also a Toyota.
From Success to Failure: Problem-Solving
Someone comes to me with a problem that sounds like,
“I wanted to do X but it doesn’t work.”
When I hear that request, often they know a lot more about the technical details than I do. I need them to teach me in a way that lets me put things in order. My method starts from success … and progresses to failure. Strange? But it works.
The questioner is focused on the failure scenario. I crave success. There must be some around — just have to find it. I ask what similar scenario succeeds. What version of the file did work? What build compiled successfully? When did this test last pass? As we discuss the successful scenario, I learn why it worked. What requirements were met by that configuration. I repeat those requirements out loud for confirmation.
With a success in hand, well understood, we have a checklist to apply to the failed configuration. We can go over the failure and check whether all the things that made something similar work are present. Of course, one will be missing. We have found the problem. I may not know how to fix it. But usually the questioner does.