12 Tips That Will Make You a Better Programmer
Do you know why people want to be better in stuff that interest them ? Because of other people. Imagine that you were the only one appreciating the value of learning how to drive blindfolded. If so, why would you do it ? What good is knowledge if nobody really messes with your findings ?
Consequently, the same idea applies to programmers and especially those who release closed source programs. If you are are the only one viewing your code, why make it better ? It’s certainly not a coincidence that people who code open source programs tend to follow better coding habits. Alas, there are some exceptions, aren’t there mr Fabrice Bellard ?
Sooner or later, you will find yourself ready to invest some time to learn more about good programming practices, not only to please those who take a look at your code, but also make it easier for you to read and understand even after some months have elapsed after your last editing. I decided to mention what i think are the 12 best habits that will help in that matter. Let’s review them one by one :
1. Error Checking
There are not many things that are more frustrating than getting a return value from a function that is actually handled as a correct one though it is an error. Error check beforehand in order not to debug at a later time. Use structured exception handling as well.
2. Sanitize User Input
Always make sure that you predict the unexpectable as much as it is feasible to do so. User input is one of the most cunning problems you would have to take care of. The PHP programmers among you are the ones that handle this problem all the time (hopefully). Remember that a user can be a security knowledgeable person who can compromise the security of your application. Escape variables, predict and most importantly limit possible user input in your programs.
3. Read The (Fancy) Manual
If you do not know how to do something, simply read the manual. If you are programming in Windows, read the MSDN reference. If you are doing it on linux, use the man pages. Whatever you do, read how the functions work. If you still can’t make it, search the internet for some examples and only post a question to a related forum only as a last resort.
4. Learn How to Debug
Believe it or not, most programmers do not really know how to debug their programs. When something weird happens, the only solution is to go through the execution of your program and analyze the state in which an error gets produced. Knowledge of assembly will be a plus here, but the most important thing is that you understand important data structure elements like the heap and the stack.
5. Be Class(y)
The most important thing to do if you are about to write a big program (lots of source) is to choose a language and implementation that involves classes. Organizing your code is vital for its reusability and will only serve to make your life easier and add the ability to more easily extend your program’s functionality in the future. Be classy when it is appropriate.
6. Stop Programming When You Are Tired
I know, i know. You can’t stop programming just because you are a bit tired. I probably do the same thing too. This does not mean it is a sensible choice though. Programming when tired raises the possibility of writing error prone code. It also makes you less sharp on choices and simple things take more time than usual. If you feel tired, just take a break, even a short one. Trust me, you will write better code after that.
7. Comment Your Code Properly
You have probably heard this more times than you have heard your own name. Unluckily, it is true. Using proper comments can really save the day for you and your partners who will have to read your code. Just make sure you comment properly. Do not describe what is happening, but WHY it is happening. For instance, the following comment is not a good one :
sum = sum + vote // adds sum and vote to sum
We know that already when we see the actual code. What this should be like is :
sum = sum + vote // calculate the sum of votes
Or maybe there would just be no need to have any comment at all in this line since it is pretty self descriptive.
8. Do Not Reinvent The Wheel
There are millions of great coders out there who have already written code for what you are trying to do. Unless you code explicitly to learn the mechanics on the actual problem you are trying to solve, learn to use other people’s code. It will not only save your time but most times provide a well tested solution already.
9. Pen and Paper for Large Projects
When programming something big, use pen and paper. Design your components first (maybe even use UML for complicated programs) and maybe also arrange them in classes. Only after you finalize that design start to write the actual code. Writing makes for better programmers.
10. Separate Code and Presentation of Data
Code and Data Presentation are two different things. This idea is heavily illustrated in PHP and the use of a template engine such as Smarty. When you need to change the code, you just need to edit the code and not a vast pool of data and code altogether and vice versa. Divide and conquer.
11. Use Versioning Control Tools
You wouldn’t want to lose code that you have already written right ? Make sure that something like this never happens. If you want to do that and do not really know how, you may want to take a look at this tutorial about how to use subversion to manage your projects.
12. What Else ? Practice !
Last but not least, practice. The more you write, the better a programmer you become.
What are the practices that YOU use in order to become better at writing code and produce programming gems ? I would really like to read your opinions.