Telling the difference between good and bad code is a sure sign of expertise. What happens when we treat this as a skill rather than something that “just comes with experience”?
Oh, So That’s What A Senior Engineer Can Do
It was my first month at my first software development job. I was getting familiar with the company and the codebase. Things were surprisingly comfortable…until all of a sudden they weren’t.
It was a Tuesday morning. I was pair programming with a senior engineer. Let’s call him Tim. Tim had to step into a meeting, so I continued working on the service class we had started together. When he got back, he took one glance at the code and, completely in stride, said “hmm, this function might cause issues down the road. Let’s move it into a separate module and rewrite the caller like this.”
I blinked twice, looked at the code, and then back at Tim. I asked why he thought so, and he took another 30 seconds to actually read the code before he answered me by describing a software engineering principle and showing me an example elsewhere in the codebase.
Convinced by his explanation, I circled back to the source of my initial surprise — “how did you diagnose that so quickly in the first place? You barely looked at the code!”
“I dunno, I could just tell.”
“Anyways, shall we?”
I wanted to write good code like Tim someday, but it felt like he was Michael Phelps and I couldn’t even float without a pool noodle. I could maybe have come up with the same solution after a few hours, but it seemed like it came naturally to him.
When I brought this up in a 1-on-1 with my tech lead, she reassured me by saying “don’t worry Tiger, it’ll come with experience.” Everyone else said the same thing. It wasn’t the answer I was looking for, but without the knowledge to push back, I moved on.
What is Taste?
What Tim demonstrated and what I lacked that day is the hard-to-pin-down idea of “taste”, “feel”, or “intuition” (I’ll refer to it as taste from now on). It’s the same trait displayed by a chess master who analyzes a board and makes a brilliant move, or a film critic admiring how a scene has been shot.
Indeed, this underlying sense is so tightly correlated with expertise that they are almost synonymous; you could argue that the ability to see a block of code and make an immediate diagnosis is the core distinguishing trait that made Tim a senior engineer and me a junior.
Discouragingly, all of the advice for developing taste is the same: “it comes with experience”. Sorry, but…that’s completely unhelpful! Perhaps it goes unchallenged because of the perceived subconsciousness of taste. Whatever the case may be, I find it unacceptable that an ability so closely and universally associated with mastery is treated so nonchalantly. I want to change that. You want to get better at basketball? Go shoot some hoops. Want to be an expert at guitar? Start strumming.
Want to learn how to write clean code? Apply the technique of perceptual learning.
Let me explain.
Building Taste With Perceptual Learning
In a nutshell, perceptual learning is a method for training subconscious pattern-matching.
This is perhaps best demonstrated with the seminal example, a 1994 study on pilot training by UCLA cognitive psychologist Phillip Kellman and NASA researcher Mary Kaiser.
The task at hand was to interpret a flight display panel as one of several states, such as “straight climb”, “descending turn”, “straight descent”, or “malfunction”. The ability to quickly and accurately make these judgments can be the difference between life and death for a pilot.
First, the researchers took a group of civilians through 2 hours of training. Critically, only 5 minutes of this time was direct instruction — just a quick run-through of the dials on the panel. The rest of it was spent on perceptual learning exercises. These exercises ran as follows:
- The subject is shown a set of 24 unique images of flight display panels
- For each of the panels, they are asked to assign one of the aforementioned labels (climb, turn, etc.)
- After getting through 24 images, they are given feedback on their speed and accuracy, then asked to work through another set of 24 images, and so on.
While the process sounds mundane, the results were anything but. After this training, when professional pilots were brought in to compare performance on images that neither group had seen before, the non-pilots were just as accurate and significantly faster than the professionals.
Professional pilots spend thousands of hours training to develop this diagnostic ability. It took just 2 hours of perceptual learning for complete newbies to match them.
The reasoning for this is striking. The taste for code that I so admired does not come from reading textbooks or tutorials. It comes from training your subconscious pattern matching to the point where it’s good enough to simply see a problem and immediately jump to the correct solution. That is what mastery feels like.
Now let’s make this concrete. In her book Badass, Kathy Sierra writes that effective perceptual learning requires: a large number of diverse and positive examples in a short amount of time.
- large number: as many examples as possible to train longer. By extension, this means you should bias towards many smaller examples instead of a few big ones
- diverse: there should be enough differences between examples to make it clear which parts they have in common
- positive: the brain learns best what it records the most. The best way to recognize bad examples is to be skilled at recognizing good ones.
- examples: not descriptions or abstract definitions. They must be concrete implementations of the concept.
- short amount of time: focused and intentional practice
The best implementation of this technique I’ve seen is Cedric Chin’s Perceptual Learning Playlists.
Perceptual Learning Playlists
We can take the pieces above and apply them to a repeatable process. Here’s how Chin suggests you do this:
- Gather a set of examples you admire (if you’re a beginner, consider getting help from a peer or instructor). Print them all out, or use a digital notebook like Evernote.
- Browse through these examples one by one. Don’t dwell. Just look, and absorb.
- Close your playlist and move on with your day
- Schedule time to update and curate your playlists. Re-visit them regularly. You will notice your taste improving over time.
The trickiest part of this process is between steps 2 and 3: there is a distinct lack of immediate, visceral feedback. It’s important to keep in mind that this is a different type of practice from the other types of work in a typical coding bootcamp or similar learning environment; what you are trying to develop is subconscious. It’s supposed to eventually feel natural. You may not be able to directly articulate what you’ve learned right after going through your playlist, but rest assured that you have just condensed the exact process that distinguishes experts from novices.
Example: Unit Tests
Let’s walk through an example of a perceptual learning playlist structured around writing good unit tests.
Unit testing is a useful application of perceptual learning because it is lighter on technical knowledge (by design, unit tests should be simple, straightforward, and repeatable), and heavier on the type of pattern-matching we are discussing here. The difference between a good tester and a bad tester is not memorizing some special syntax. It is — you guessed it — “experience”, or the ability to think of all the ways things could go wrong.
Without further ado, a playlist of Ruby unit tests:
Yep, that’s all there is to it! Simple, yet incredibly powerful.
Today, four years down the road, I can look at a piece of code in a language I’m familiar with and make the same type of thorough diagnosis as Tim. It turns out that, yes, it did come with experience. But that experience took a loooong time. I am hoping that you, dear reader, can give yourself the gift of a more pleasant and efficient learning path by applying perceptual learning.
I’ve used perceptual learning myself for the past several months to improve my taste in writing and music. You can imagine what my playlists look like — snippets of prose I admire for the former, and songs for the latter, both accompanied by short descriptions of what exactly I like about them. A dozen hours of perceptual learning on each of these skills has had a greater effect than thousands of hours of passive “practice” in the years before.
So — try it out, and let me know how it goes. Happy coding!
For further reading, I’d recommend the blog post that introduced me to perceptual learning.