Why developers are scared to refactor code ?

Nilesh Jarad
Mobility
Published in
3 min readJul 21, 2016

How do I do it . I’m afraid to refactor the code ……

I have seen many programmers being afraid while changing or refactoring code. Even I was afraid five months ago to change the code but now I am confident enough while changing the code .

And I am sure I am not alone . There are millions of coders around world who are afraid to change or refactor their code.

Why programmers are afraid of changing code ?

  1. Lack of Confidence ! .They are not confident about their changes (is it going to work or will it break other dependent code).
  2. If it doesn’t work, what amount of existing code will it hamper ?
  3. They are not sure about Regression testing.

Now since we have established why programmer are anxious about refactoring . Let me tell you why it isn’t grueling process as it is imagined to be .

Here comes in picture TDD(Test Driven Development)

What is TDD?

TDD mantra “Red-Greeen-Refactor”

Following is sequence for TDD:

  1. Failing Test : Write a failing test.
  2. Passing Test : Write a passing test that defines function .
  3. Write Code : The next step is to write some code that makes the test to pass. At this stage code is not perfect but it is acceptable as code will be improved in 5th stage.
  4. Run test : If all tests pass programmer is confident about code.
  5. Refactor code : Eliminate redundancy .By continually re-running the test cases throughout each refactoring phase, the developer can be confident that process is not altering any existing functionality.

Test-driven development is a way of managing fear during programming . Fear makes you tentative. Fear makes you want to communicate less. Fear makes you shy away from feedback. Fear makes you grumpy. — TDD By Example

What are advantages of TDD:

  1. You feel confident about your code and logic.
  2. Your code is cleaner .
  3. Code quality & maintainability increases .
  4. TDD can lead to more modularized, flexible and extensible code
  5. Regression testing is much more easier as automated test cases find bugs early (so you are confident while changing code or delivering code to production).

Problem with less acceptance of TDD:

The problem with TDD is that it does not show it’s impact in initial stage.You will see it’s true value when you will have to refactor the code after two-three months down the line.

Hope this piece of information provides you relief in some manner.

Would like to hear your inputs on this article.

Hit little ❤️ below. Cheers !

--

--

Nilesh Jarad
Mobility

Android/iOS Developer, Stackoverflower , Reader & Open source lover. Reach me at nlshjarad7@gmail.com. Profile :- http://stackoverflow.com/users/4626831