Coding it right the first time

Wednesday, May 7, 2008

Has anybody consistently succeeded in writing code (say a simple one) and compiled it successfully without any errors or warnings in the first attempt?

Has anybody consistently got the expected output the first time the code was executed handling all possible inputs?

If the answer to the above questions are No, has anybody consistently strived to achieve the above results?

As I always hear, 80% of the time is spent maintaining code in the bigger picture compared to actual coding itself, but again its due to the fact that the fundamentals of writing code and the best practices are ignored most of the times.

The problem is not that the coders are bad, but the demands and expectations in S/W Industry has taken its toll on the programmers these days, the cost of missing the deadline being a primary factor why timely delivery takes high priority than the quality of the product, however its not an excuse for poorly designed code.

From my experience the frustration and anguish I got after discovering some hard to catch bugs in my code makes me ask the embarassing question
to myself "How the hell I could have missed that?".

Therefore lesson learnt is there should be a stringent methodology which I should adopt before implementing any solution, although every individual have their own coding style, but its always worth doing 'n' number of iterations in design, writing test cases and pseudo code which would lead to very few rounds of bug fixing, easing code maintenence. The following simple steps proved highly effective for me before coding and execution.

1. Come up with a comprehensive list of test cases from the end user's perspective.
2. Write pseudo code and ensure it covers all the tests you have come up.
3. If the pseudo code needs a change to pass a test, modify the code and iterate once again from the first case (In long term this would payoff during regression).
4. Finally decide to compile and execute the code (try to get the first and best impression from the compiler :)), it really will make a huge difference.

Once you come up with your own technique or adopt some best practices before you start the actual implementation, its highly likely that you will not be changing the code adding more if's and else's, deleting and commenting code fragments, moving part of code from top to bottom, etc, in the name of fixing bugs :).

The moral, we should ALWAYS try to get it right at the first attempt.

4 comments:

srid said...

Certain languages like Haskell has the side-effect of making you write less buggy code.

Also take a look at this idea - http://sunson.livejournal.com/169078.html

sprasanna199 said...

Thats cool, but the problem is these things are never taught or given much importance at the college level which is key for every programmer to succeed at the industry.

They teach data structures, algorithms, etc but never a mandatory subject on coding guidelines.

Rafeeq said...

Good Prasanna,

I am impressed by your initiative of writing a blog to have technical discussions.

Prasanna Seshadri said...

Thanks for saying so, appreciate your comments.


Copyright © 2016 Prasanna Seshadri, www.prasannatech.net, All Rights Reserved.
No part of the content or this site may be reproduced without prior written permission of the author.