Search This Blog

How to Get Better at Programming with Feedback

Feedback is a critical part of improving at any skill. Without feedback, you have no idea whether you're getting better or worse. You lose any sense of accomplishment or direction in what you're doing. Imagine trying to do something new and difficult without any kind of feedback whatsoever. You wouldn't even know when you've done something worthwhile, and you'd quickly lose any interest or motivation in the task.

Instead, with feedback you can gauge your progress. You can make changes in what you're doing more quickly and efficiently to improve your technique and tackle more difficult challenges. Feedback gives you the confidence to know when you're doing things right and to course correct when you're doing things wrong.

Programming is a challenging skill that takes years to learn and a lifetime to master. Programmers can use as much feedback as they can get to improve at this skill, so let's look at a few ways we can create good feedback loops to get better at programming.

Inner Tennis


I recently read Pragmatic Thinking and Learning by Andy Hunt (by the way, I love the subtitle: Refactor Your Wetware; it's so perfect), and in it he has a section where he goes into lessons from the Inner Game series of books, specifically The Inner Game of Tennis. I remember reading this book of wisdom in high school. My tennis coach highly recommended everyone read it, and it was as much a book about how to improve at anything as it was about how to improve at tennis.

Hunt reminded me of one lesson in particular that epitomizes the use of feedback when practicing. We're talking about tennis now, but we'll get to programming in a second. So, to get better at accuracy in tennis, place a chair somewhere on the court, and then start hitting balls at it. Don't try to consciously aim directly for the chair. It should just be in the path of where you would return the ball. Then, every time you hit a ball over the net, make a mental note of where the ball landed in relation to the chair. Was it to the left, right, in front, or behind the chair? Was it closer or farther away than last time? Don't try to forcefully correct where you're hitting the ball. Instead, make that mental note, maybe even say it out loud softly to yourself, and keep hitting balls in the general direction of the chair.

It doesn't seem like this exercise should do anything useful, but you'd be surprised. After a couple minutes of simply observing where the ball lands and being fully aware of that fact, your subconscious and your body kind of take over and your shots start to coalesce on the chair. It's an excellent way to use feedback during practice to improve your accuracy.

What's the equivalent of the chair in programming? Well, we need to deal with a lot of strict syntax rules while coding, and if you have unit tests, those tests need to pass when you run your programs, too. Compilers, interpreters, and unit tests all provide excellent forms of feedback for improving your programming skills, but you have to be fully aware of what they're telling you. If you rush through fixing syntax errors and unit test bugs without paying enough attention, you're missing a chance to improve your debugging skills.

When you get a compile error or a unit test fails, stop and really look at the error. Read it quietly to yourself and simply be aware of what it means. What type of error is it? What line is it occurring on? Did you change that code recently? This mindfulness may sound like a silly thing to do, especially if you think your compiler spits out cryptic error messages, but I've found it really helps. Quite often the fix for the error will pop into my head before I even finish reading the error message. It works much better than noticing that the compile failed, flipping back to the code I just changed, and hacking at it to try to fix the problem. When I fall back into that bad habit, I'll normally waste a few more compile cycles before fixing the real problem.

Not only does being fully aware of the error help get you to the right fix faster, it also helps you avoid those errors in the future. It's easy to make the same mistakes over and over again, but simply being conscious of those mistakes when they happen seems to help embed the correct actions in your memory. It's like you're telling your brain, "Oh, look at that mistake. That's another x-type mistake and here's the right way to do it instead." Eventually, your brain responds, "By golly, you're right. I'm tired of being constantly reminded of that mistake. I'll just do it the right way next time."

I used to not be able to write ten lines of code without making half a dozen errors, but constantly being aware of those errors helped. Now I can write a hundred lines of code and have it compile the first time. When that happens, wow is it satisfying.

Perfecting a Golf Swing


This technique would work for tennis as well, but the explanation works better for golf because in golf the ball is stationary so there aren't as many moving parts to think about. Perfecting your swing is the thing to do in golf, and there are a crazy number of technical details that surround the perfect golf swing. There's your stance, your posture, your grip, rhythm, timing, speed, breathing, back-swing, and follow-through. Is your leading shoulder dropped? Is your club face open or closed when you hit the ball? How quickly do you drop your arms toward the ground on your forward swing before starting to arc the club around? Are your elbows straight through the swing? What's your X-factor? Did you start the swing by rotating your hips? Did you keep your eye on the ball?!

That's only the beginning. There are truly an amazing number of points to a perfect golf swing, so many in fact, that if you were to try to keep them all in your head for each swing, you would never be able to hit the ball. You would likely look like you were having a full-body spasm, and you would probably go insane trying. You simply cannot think about the entire golf swing while doing it, and it's not recommended anyway.

What is recommended to improve your golf swing quickly is to focus on exactly one thing at a time. Try to figure out what the biggest issue is with your swing (a golf instructor could be a big help in this regard) and work on that one issue until it has improved. Then move on to the next issue. For example, most golfers (myself especially) have a hard time keeping their head down and their eye on the ball. The natural reaction when swinging a driver is to pick your head up to make sure you see the ball shooting off into the distance. The result is that instead of the expected beautiful ball flight, you shank, duff, chunk, or completely whiff your shot. Not good.

The best thing to do for this problem is to make sure to watch the ball until it disappears because the club head just smashed right through it. To get it through my head that I need to keep it down until the momentum of the club and my arms wrench my eyes from looking at the ground, I pick a short phrase and silently repeat it to myself while I'm setting up for the shot. I normally take two practice swings, so I'll go through those practice swings while repeating, "head down, eyes on the ball…head down, eyes on the ball…head down, eyes on the ball" to myself, all the while staring at the ground where the ball would be. Then I step up to the ball, still repeating, "head down, eyes on the ball" and staring at the ball until I'm ready to pull the club back. By now my brain is getting tired of this phrase, which is good. I let my mind go blank, which is easy at this point, and take my shot.

Invariably, I accomplish the thing I was focusing on quite well. My swing may have other issues, but I will have had my eye on that ball. Once I feel that that particular thing is not an issue anymore, I can move on to another issue. There are always so many to choose from. This technique can be done at any time in golf. It doesn't matter if you're playing 18 holes or hitting a bucket of balls on the range. If you're on the driving range, you have the added benefit of a faster cycle of hitting balls, and you can focus on one thing for ten or so shots before moving to the next thing. That seems to be a good number for cementing something in your subconscious or muscle memory. Whenever you feel it slipping, you can come back and work on it again.

Where is the feedback in this process, you might ask? After each attempt, you should note whether or not you corrected the issue, and if you can do it better the next time. This creates a tight feedback loop that will help create a strong habit that will stick with you.

When programming you can do the same type of thing. Do you have trouble remembering to end statements with semicolons? Do you use all the hot keys in your IDE or text editor that you can? Could you be better at using your language's standard library—or the libraries that you use often—without looking at the documentation? Plenty of aspects of programming lend themselves to this kind of practice, and you'll gain lots of productivity by making them automatic. Once they're automatic, you'll have more mental resources to spend on solving the hard problems you're dealing with.

Working Under a Microscope


If you've ever worked on something that requires fine motor skills under a microscope, you'll know how much better control you can have with it than without it. With a 10x or 20x microscope, you can do things that would never be possible otherwise. Working with electronic circuit boards is one such task that is greatly enhanced with this tool. When soldering small components with fine pitched leads on a circuit board, a good microscope can be critical to aligning pins correctly on the pads.

The reason a microscope works so well for the fine work of soldering circuit boards is that it enhances your visual feedback. If you can't see what you're doing because it's too small, you won't succeed. Enhancing your vision gives you the feedback your muscles need to work at that level of detail. The human hand is capable of amazingly fine movements if controlled with the right feedback.

The same idea can be applied to improving your programming skills. Find ways to enhance the feedback on your code so that you can zoom in to the issues you may have and improve them. Code reviews from constructive coworkers can be one microscope for your code. Your reviewers can point out gaps and shortcomings that you may not even be aware of, and they can give helpful advice for making the code cleaner, clearer, and faster.

Practicing algorithms and other programming concepts is another good way to put your skills under the microscope. Find programming problems that you can really dig into and explore through multiple levels of complexity. Take the time to refactor and polish your solutions. Experiment with different styles of writing solutions to the same problem so that you can compare their strengths and weaknesses. The problems on Project Euler are especially good for these kinds of explorations. In many of these cases you're providing your own feedback, but the process of exploration and discovery is an excellent way to develop your own sense of what works and what doesn't so that you can work on generating good feedback for yourself.

Books are another great source of detailed feedback. Find the books for the languages that you use most that cover how to write well in those languages, and study them. They normally have 'Effective' or 'Eloquent' in the title, and they are usually much more interesting reads than the basic language manuals. They will show you how you can dramatically improve the programs you write in those languages, and how to use the languages to their fullest potential. Books on other programming concepts like algorithms, compilers, or machine learning can also give you good feedback on programming skills that you may be missing. Find something that looks interesting and dive in deep. There are tons of programming topics out there to learn.

By improving at the basic skills of programming, the act of writing code starts to become second nature. We can stop thinking about coding and spend more brain power doing the real work of solving problems. Feedback is invaluable in this process. Letting feedback work with your mind instead of forcing it, focusing on a tight feedback loop, and adding a microscope to your feedback are all great ways to enhance feedback to improve your programming skills.