Thursday, January 12, 2017

Don't get Trapped In Code Refactoring

This blog is coming out of discussion with one of my old client. I worked with him for almost 5 months in year 2010, that is 6 years ago. We build a nice hybrid mobile app. Recently we had a talk about a new product. During our conversation I came to know that for one of his other product he hired a developer and it took almost year to build a product which was originally estimated for 3 months.  That was bit surprising for me so we discussed more about it. Client said, the developer took most of the time in changing the code so many times, however output was the same and there was no functionalities changes . So immediately I understand, developer refactored code so many times Client asked me is it necessary to change the for the things so many times which are already working?  Well yes that's the important question, at what level refactoring is good?

For the readers who are not aware of refactoring, here is some information about code refactoring. 

"Code refactoring is the process of restructuring existing computer code—changing the factoring—without changing its external behaviour. Refactoring improves nonfunctional attributes of the software. Advantages include improved code readability and reduced complexity; these can improve source-code maintainability and create a more expressive internal architecture or object model to improve extensibility. Typically, refactoring applies a series of standardised basic micro-refactorings, each of which is (usually) a tiny change in a computer program's source code that either preserves the behaviour of the software, or at least does not modify its conformance to functional requirements. Many development environments provide automated support for performing the mechanical aspects of these basic refactorings. If done extremely well, code refactoring may also resolve hidden, dormant, or undiscovered computer bugs or vulnerabilities in the system by simplifying the underlying logic and eliminating unnecessary levels of complexity. If done poorly it may fail the requirement that external functionality not be changed, introduce new bugs, or both."

Since I started working as a software developer, code refactoring always confuses me. Every time I refactor a code, I am not sure if I am doing it right or wrong. 

In my frank opinion I also believe in refactoring in code and make it more simpler to amend future changes easily and to make it more understandable by others. Code refactoring is good, but my confusion is at what level and how many times? 

I still remember my team lead when I was working as a software engineer, My team lead always come to me and there is always one instruction, "Hiren, reactor your code" and I do reactor code by all means and make it more reusable.  But at certain point I think it's useless, the code is already in good condition, why should I make it more complicated? Is it really necessary? 

There are two output of code refactoring, either it will make code better or it will make it worse. Worse to the level that it will introduce many bugs and only option then you will have is to rewrite code. And why shall we make such complex code that nobody can understand and it's not necessary when things are working fine. Code refactoring is not going to change output, yes it may improve performance, but it will be at some level, beyond that no matter what you do, performance will remain same. So it does not make any sense to refactor the code. Its waste of time and money. Yes unless you want no one to understand your code, you can make it complex by refactoring it many times. This way you will have patent of your code and nobody will be able to change it. But this does not make any sense when you are working in team. Code refactoring is sometime end less, you just keep on refactoring the code and you will never finish the project.

So in my opinion, code refactoring is good at certain level, not beyond that. Don't get trapped in refactoring. 

No comments:

Post a Comment