My learnings from my mistakes about programing.

19 03 2009

Any fool can write code that a computer can understand. Good programmers write code that humans can understand.

That is true. I am also a fool until 2 months back. I used to write code that only computer can understand. I understood the importance of following programming standards from my mistakes. Here are some good programming practices that am following now.

Commenting the code. I never used to comment the code so that when ever i go back to the code I used to take some time to understand what am doing there. And when ever i am seeing some third party code i used to skip the comments and tried to understand the code. see how stupid i am.

Using Proper Indentation. If the code is properly indented then you can easily understand the code. If the code not indented it won’t look good also.

Strictly following the syntax. If we know that semicolon at the end of each statement is optional then we don’t bother if we don’t put semicolon at the end. But this may cause some other problem later. Use some syntax checkers like jslint for javascript, pyflakes for python to check the syntax before testing the code. So that you can find the errors before running the code and you can save the time also.

Not using recursive if-else blocks. If you use if-else in another if-else and this is in another if-else. Then we get confused and it is much harder to understand. I want to put some example here. This is the if-else loop i used previously

if request.POST{
if request.POST[‘submit’]{
……do some thing
return success page
……do some thing
return cancel page
……do some thing
return some page.

The above code is simple and if we are using the code like above the indentation is very much important otherwise we lost, we don’t understand any thing . The same code i can write like bellow.

if not request.POST{
……do some thing
return some page
if not request.POST[‘submit’]{
…… do some thing
return cancel page
do some thing
return success page.

See How beautiful the code is, we can understand very clearly and there is no chance of loosing in the middle.

Trying to make the classes(or modules or functions) more independent. The more independent you make it is more easy to debug or apply changes later. Because it won’t effect the other parts of code. And one more thing use only one way of dependancy, i want to give example here.

   obj1 ---------> obj2 --------> obj3  (correct)

  obj1 ----------->obj2---------->obj3  (wrong)

Suppose obj1 is dependent on obj2 and obj2 is dependent on obj3 this is normal single way of dependancy. if obj2 is again dependent on obj1 or obj3 is dependent on obj1 or obj2 then this dependency will become mess and we don’t understand any thing.

Not hard coding anything. If you need to use some max limit as 5, then while coding don’t use 5 directly make it as a variable, if we change that value we can change only once otherwise we have to change it where ever we hard coded and there is a chance of forgetting so it will cause problems.

Writing comments on every commit. While committing the code if you write what you did for this commit it will be easy when you are refer back, when you lost some code we can easily trace. When we do some mistake also we easily go back by seeing the comments. Commit after every change that you made. Suppose you added some simple one line statement which is not making wrong also you commit and say why you wrote that line. Before commit see the changes that you made to files by using diff command then you can know what are the changes you made clearly.

And some other things that i have learned are

  1. When ever we are using third party tools or frameworks or any thing, first read the documentation even if we don’t understand but we can know all the features supported by that tool or framework and later if we need we can use.
  2. When writing any function don’t rush to start coding. First understand what this function has to do and what it should return. Then think how to do that functionality. Then write the steps what you have to do in comments then write the code. Once when try this you came to know how cool this way.
  3. Dry the code as much as possible. Don’t repeat any small peas of code.
  4. Don’t use big big lines which causes the code look ugly which breaks indentation. Use maximum of 80 columns per line.
  5. When debugging the code. try to use debuggers they will help you to solve the problem quickly.

All these things I have learned from my team leader eduardo.
Thanks to eduardo :).




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: