Thursday, December 19, 2013

How Not To write code.


When you start working on a project you will learn lot of technologies, Techniques from your seniors, And ways to write clean and readable code. But there will be some projects which will teach you how not to write code specially if you are working on already developed project. No matter which project you are working on, there is a scope to learn either how to do thing or how not to do.
           . When I first went for code review few month back you can’t even imagine how many time my reviewer used f-word. For every extra line break, For every time when I placed private method between public methods, For every public method without comment, like this I made him use as many f-words as possible. But sometimes I thought why he is so picky about all these small things. And then suddenly a miracle happened.
            I got an opportunity to work at client location. So I moved to another place where I’m working on enhancing some functionality in the existing application. It was developed by a Java developer on a .NET platform using web forms and plain JavaScript. Pick any class in the project you will see all possible mistakes a developer can make in his entire life. In the beginning I was so happy that finally I found a guy who wrote code just like me, but when I started making changes real pain started. I will share all painful stories and what I learnt from them.

Violating DRY made me CRY
            Seriously If you end up write same lines of code twice that means you are making a mistake. You are building a system which is tough to maintain. One fine day my QA assigned me a bug asked me to fix it as soon as possible. I debugged, found the root cause, fixed, went home, had beer and slept. When I came to office next day I saw a mail from QA, she was saying bug was not fixed. I was sure I fixed it. I tested in my local it was working fine. She showed me other scenario how she was able to reproduce. I felt embarrassed, debugged the code I found out that, my dear friend wrote another method which has same code repeating with just one extra parameter.  I had to fix it in two places. I remember the same kind of code that I wrote few months back but thankfully I was caught in code reviews.
Bottom line is if you ever come across a situation where you are repeating code that is the best time to stop writing and start thinking. Considering my experience  I’m not entitled to tell how to write reusable code in a best possible way. But I can tell you if you repeat some code, it is going to be a nightmare for the guy who is going to maintain that application.

You may have lot of responsibility but your method/class should have only one responsibility

            Single Responsibility Principle was the most confusing thing in OOPS for the beginners, I wrote a method long back which will do following things.
1.  Read a connection string from configuration files.
2.  Create a connection
3.  Execute Stored Proc
4.  Cast result into a list of .NET objects
My misconception while writing this method was I thought this method is just making call to database and and returning the result. But if you see it in the granular level it was doing lot of things. I read somewhere a method or class should have only one reason to change. If you see here I have at least three reasons to change above method.
1. When connection string name changed in config file.
2. Stored proc Name or Parameter Name changed.
3. While casting if any property type changed or you need to validate before pushing into list you need to make changes to this method.
When I was adding one feature in my most beloved application I found some methods which has more responsibilities than a President of united states. I’m not sharing code here but Let me explain what a method doing.
1. Taking Excel file as input and reading data from excel.
2. Parsing all column values into appropriate types.
3. Validating the values
4. Creating .NET objects.
5. Passing that to other method which will save that in db etc…
You can find at least 100 reasons to change this method. if the file type changed to something else you will change this method, if validation logic changed you will changes this method etc.