Wednesday, April 21, 2010

How to be a better programmer:

12 best habits that will help to make you a better programmer.
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 un-expectable 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. 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
You can’t stop programming just because you are a bit tired. 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 :
1 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 :
1 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. 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. Use appropriate versioning control tools.
12. What Else ? Practice !
Last but not least, practice. The more you write, the better a programmer you become.

No comments: