Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
-Kernighan's Law
๐คฏ
When I teach programming, I emphasize that to me, the most important part is that the code is "readable."
You write code (once or twice), but it is read all the time. Optimize for what happens a lot.
Not:
๐ฆ Brevity
๐ฆ Cleverness
๐ฆ Using all of the features
At some point, we will need to start debugging. This is a skill that young kids can master. (My daughter typoed some code last night and was able to fix it)
Here are my debugging tips:
๐ช Prepare
๐ช Use "print" debugging
๐ช Rubber duck it ๐ฆ
๐ช Sleep on it
๐ช Debugger
๐ช Simplify/Test
Prepare - Make sure you know what you are trying to do. Read the documentation before diving in. Many problems I see are syntax errors. (See tweet above)
Print debugging - I use this all the time. Programming genius I know also use it. You can bend this into logging for production debugging.
Rubber duck - It turns out that you use a different portion of your brain when you vocalize your problems. Often times in the middle of describing it, you know your misstep.
This is called "rubber duck" debugging because people will speak to an inanimate object to get advice. The important part is to say it OUT LOUD (From "The Pragmatic Programmer")
Sleep on it - Another brain hack is called "incubation". I've slept on problems multiple times and woken up knowing what to do. Your brain will churn on issues even when you step away.
This is why pulling all-nighters trying to fix things is a misguided approach.
Debugger - A tool that allows for breakpoints and inspecting code can be useful. However, I find that the above steps often work before that.
Simplify/Test - "It works for me" is frustrating. You need to come up with the most simple version of the problem sometimes to get help. Writing tests can aid in this (and protect from future problems).
๐ Thanks for reading!
What is your favorite advice for debugging?
Follow me for more programming-related tips.
โข โข โข
Missing some Tweet in this thread? You can try to
force a refresh
One of the most effective things I did for my career is probably the most difficult.
๐งต๐
It is not difficult because it requires strength or knowledge, it is difficult because it requires will power.
I'm talking about writing a book. ๐
Writing a book taught me a bunch of lessons about work, myself, and business. I would say it jumpstarted my career (even though I had been employed for over 13 years by that point). โ๐ง