This past week and a half has been rough for me. I have spent countless hours debugging my program because of stupid errors I’ve made when trying to manage my program. I have learned, all too well, the importance of clear variable names and the use of additional commenting. I really have spent some time trying to think about how I can make my code beautiful.
So, Here’s 3 easy things you should remember when dealing with the management of data structures in C.
Step 1: Name your pointers in someway that indicates its a pointer.
Alot of the times, I manage to pass a variable and at that time I knew it was a pointer but what happens one or two days later? If you name your variables inefficiently, your brain will explode when trying to understand what you were thinking. It’s a fact. In order to avoid something that will take you MUCH more time than it should, just make the effort to do it right in the first place.
Bad examples of pointer names:
int * temp; char *** stringArray; char ** string;
Good examples of pointer names:
int * tempPointer; char *** stringArrayPointer; char ** stringPointer; // - OR - int * tempP; char *** stringArrayP; char ** stringP;
Step 2: Function Commenting
Be sure to add comments to your function that help keep you on track. Make these comments clear and straight to the point. Try to describe what the goal of the function is, precisely. This will keep you on track when you need to refer back to what the heck you’re doing.
P.s. – If you think you can get by without commenting things, you’re thinking foolishly and you should change.
Step 3: Don’t say your code doesnt work.
Saying your code doesnt work is like saying your book doesnt work. If I gave you my book and said what do you think of it, and you replied with: It doesn’t work, I’d take my book and hit you in the face with it.” – Mouser, donationcoder.com
Basically, when you stare at your code wondering what is going on, because its inevitable, take a second to realize WHY its not working instead of saying, my code doesnt work.