Monday, November 16, 2009

Best coding practices

Over the many years of my programming experience. I have found common mistakes which people make again and again. In this series of articles I will post some of best practices one by one and common mistake which you will avoid by using it.

1) Read the API documentation carefully.

It is very common that programmer does not read API documentation carefully. Which include what are parameter boundaries. What are side effect of passing certain parameters.
Under what condition function will fail. What are error code or exceptions thrown by the api. If programmer take 10 min of extra time and reread the documentation once again carefully. I can bet many of programming errors can be avoided.
2) There should be very good logging mechanism in place.
While developing any piece of software there should be very good logging in place. Log statement should be well thought. These should give comprehensive description about the event. I have seen logger statement like enter the function exit the function. these are really of no value. There should be different log levels which should be configurable. In case of failure log should give clear picture why the failure has happened.
3) Write simple code
We should write as simple code as possible. We should avoid complex code. One hint for complex code could be, if one has to write comments to make the code understandable it is time to review the lines of code you have written. See if you can make these lines simpler.
4) Use standard libraries and api's :-
Many programmer keep inventing same thing again and again. e.g function to reverse a strings etc. There many standard functions for many tasks like this. Read all the standard api's and try to use already tested and used api's

No comments: