Effective Programming Practices
This is for all those people who know how to solve a problem but can't seem to get it to work. I think it can be solved easily by reviewing your current workflow.
Here is how I think you can reduce hours of guessing.
1. Test test test
This cannot be stressed enough. Even if it's a silly thing that you know like the back of your hand, there may be other variables involved in your current environment that you may have overlooked. So the easiest way to start is to write a hello world program and compile it. Make sure it works.
2. Make a commented algorithm in the code itself
This follows the divide and conquer paradigm. You have a rough idea of what the program should do. You have a rough idea of the stages involved. So write down comments in code that very vaguely describes the steps you're planning to take to make the thing work.
Once you've got that down, start with the first section. Try to break this down into discrete and clear tasks that have to be done. You could even literally write comments for each of the steps that are required. Now, you could either proceed to do the same for the rest of the algorithm, or convert this much of the algorithm to code and then tackle the rest.
3. Test test test
Now that you have the discrete steps that are to be performed, convert the first line of the algorithm to code. Add a print statement to print any values associated with that statement, if applicable. Save, run. See if the value is coming correctly. If not, you know exactly where the logic is failing. If it worked, then remove that print statement and continue similarly for the rest of the algorithm. In this way, if you test each line of code before moving on to the rest of the code, you'll be able to keep track of exactly where you are when something goes wrong. This technique gives us the added advantage that we can be sure that what's been written upto now is perfect and doesn't produce any problems and so we can concentrate and zone in on the error really quickly without having to look at the previous lines of code again.
4. Use meaningful variable names
Have variable names describe what the variable holds. That can help a lot in readability. For example, if you need a variable to store the starting address of a text record, you would use the name textRecordStartingAddress. Now this one is a bit long, but you can understand exactly what it contains.
5. Use small bodied functions with descriptive names
Functions should ideally be less than 10 lines long. Even if a function has just one line, name it such that the purpose of the function will be obvious when you see the function call.
6. Use a debugger
If you have access to a debugger, learn how to use it effectively. A debugger is really powerful and can help you spot errors much more easily than if you simply guessed at the values. For C programming on Linux, the GNU Debugger (gdb) does the job quite nicely.
These are just a few things that can dramatically decrease frustration and time wasted staring blankly at a computer.
This was one of the articles written during my college days. You can find the original published article here