Jan 29

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.

Jan 29

In the many nights of hanging out on the IRC channel for www.donationcoder.com, I have had the chance to learn a new quote that helps me to be more efficient at things, mostly programming. The quote is this: “Premature optimization is the root of all evil.” The whole quote is: “We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.” This is a wonderful quote that I try to follow anytime I’m coding.

I have known of this quote but never knew its origin, until tonight. Ewemoa, from DonationCoder.com, shared the quote with mouser who recommended it be shown to me. Thanks to both of them, I can now effectively quote its origin as well. Long story short, I wanted to include this quote, and the page that was referenced to me, on my website. Here is the link: http://shreevatsa.wordpress.com/2008/05/16/premature-optimization-is-the-root-of-all-evil/

Enjoy :)

Jan 26

So, I have been wanting to learn how to save data  into a binary file so that I can load massive amounts of data extremely quick. I figured that the best way to go about this would be creating an example program that could do exactly that. So I started with this program originally to get binary reading and writing working correctly… The most confusing part at first was how to store the dynamic array of integers I was using. I didnt know whether or not we could write the whole block of memory into the binary file or if we had to read through each record and store it individually (which ended up being the case.) After a little playing around, I was able to figure out that this was an extremely easy task because I could set up the template for reading/writing. Continue Reading »