Categories
c programming tips and tricks

When you get stuck, read your code out loud

To help you track down that bug, start reading your code aloud. Pretend that a programmer friend is sitting right next to you. Explain what your code is doing and how it works. As you talk through your code, you’ll find the problem. If you don’t, have your imaginary friend ask you questions during your explanation.

Don’t worry about going mental. You’re a C programmer. You’re already mental.

As a bonus, talking through your code also helps you identify which portions need to have comments and what the comments should be. For example:

a++;      /* increment a */

In the preceding line, you see a terrible example of a comment. Duh. Of course, a is incremented. Here’s a better version of that comment:

a++;      /* skip the next item to align output */

Don’t just comment on what the code is doing — comment on why. Again, pretend that you’re explaining your code to another programmer — or to future-you. Future-you will thank present-you for the effort.

Leave a Reply

Your email address will not be published. Required fields are marked *