Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constantly refactoring my own code... a very bad practice [closed]

Tags:

refactoring

I have a strange question here.

Am I the only programmer here that constantly feels like I must rewrite / refactoring my own codes ?

Sometimes I do it only because I think the speed might be improved, or simply because I believe the code could be easily imported in a later project.

Hey, sometimes only because it looks cleaner for my eyes.

Am I sick ? Am I too perfectionism ? Am I the only one having that problem ?

like image 586
Cybrix Avatar asked Nov 09 '10 00:11

Cybrix


2 Answers

Take a look at TDD. The basic premise is:

  1. write a failing test
  2. write code to make the test pass
  3. aggressively refactor

As a general principle I think aggressive refactoring is a good idea - why have any more code than you really need. The difficulty comes in two parts, first, why refactor without a purpose and second, how do you know you haven't functionally changed the refactored code if you don't have a suite of tests against which you can test the refactored version? TDD is an answer to both of those things.

I would encourage you to get in the habit of refactoring, but I would also encourage a practice like TDD so you don't break your code as you do it.

HTH

like image 57
Simon Avatar answered Sep 18 '22 06:09

Simon


You are probably just experiencing iterative understanding of the problem. You make a stab for it and then when you are done, you have gained some new insight. This new insight you apply, only to learn that you have a new insight again, so you toss out part of the code and rewrite that.

You should not change code to be faster if you think it is. You should measure through profiling or wall-clock runs. And you should know to what level you need the code improved. Some code need very little speed improvement to be adequate.

like image 36
I GIVE CRAP ANSWERS Avatar answered Sep 20 '22 06:09

I GIVE CRAP ANSWERS