Are you afraid to refactor?

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 690 Views - Last Post: 24 January 2018 - 02:06 PM

#1 bobsmith76  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 171
  • Joined: 14-February 17

Are you afraid to refactor?

Posted 19 January 2018 - 11:10 AM

I was watching this lecture by Brett Slatkin

https://www.youtube....h?v=D_6ybDcU5gc

and I noticed that he said that some people are afraid to refactor because they've done all this work to get things working and then they don't want to go back and refactor because they're afraid that it will be too much effort. I remember back in my early days I did feel this fear. Fortunately, now I am more than happy to go back and rip up huge sections of code. Anything to get it as organized and as simple as possible. Thank God those days are gone.

Is This A Good Question/Topic? 0
  • +

Replies To: Are you afraid to refactor?

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6158
  • View blog
  • Posts: 21,233
  • Joined: 05-May 12

Re: Are you afraid to refactor?

Posted 19 January 2018 - 11:15 AM

I fear refactoring code that does not have unit tests, or code that is not in source control. If the code both does not have unit tests and is not in source control, then lets just say that a lot of chocolate is consumed while I'm touching the code.
Was This Post Helpful? 2
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Beginner
  • member icon


Reputation: 11040
  • View blog
  • Posts: 18,852
  • Joined: 19-March 11

Re: Are you afraid to refactor?

Posted 19 January 2018 - 11:28 AM

I assume that by "chocolate" you mean "a probably none-too-healthy combination of tranquilizers, antidepressants, caffeine, pharmaceutical-grade stimulants, and the occasional shot off a borrowed inhaler, just in case".
Was This Post Helpful? 1
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 14033
  • View blog
  • Posts: 56,136
  • Joined: 12-June 08

Re: Are you afraid to refactor?

Posted 19 January 2018 - 11:30 AM

No. Purposeful refactoring is great.. new ideas to see new trails to explore. Needless refactoring is silly.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7160
  • View blog
  • Posts: 14,924
  • Joined: 16-October 07

Re: Are you afraid to refactor?

Posted 19 January 2018 - 11:52 AM

I enjoy refactoring. Hell, I refactor code posted to this site for amusement...

Fear is in scale. There is always a possibly of introducing something unintended. As noted, appropriate unit tests assuage such fears.

The pain of programming is in finding a working solution and debugging. Running code has been through that pain; it works. Refactoring means you already have the answer and are now tasked with making it cleaner, more elegant, more satisfying, and hopefully more maintainable.

I tend to refactor as I go. The reason refactoring gets lost in real world development is time: it needs to be done now. If you take a moment to clean up something you did the day before you have the satisfaction of cleaner code and the benefit of something that will be easier to debug then you inevitably must look at it when the project is long forgotten.

When you refactor incrementally the time taken is simply part of the time of development, rather than some ephemeral extra time investment that only a programmer will understand.
Was This Post Helpful? 2
  • +
  • -

#6 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1460
  • View blog
  • Posts: 4,726
  • Joined: 20-March 10

Re: Are you afraid to refactor?

Posted 19 January 2018 - 12:07 PM

I totally love refactoring too...

Sometimes its all I want to do.... the bigger the project the better always looking for either

repetitive or redundant code.....

The bigger the project the more chances refactoring will bring improvements in executable size and speed....

This post has been edited by snoopy11: 19 January 2018 - 12:08 PM

Was This Post Helpful? 0
  • +
  • -

#7 jon.kiparsky  Icon User is offline

  • Beginner
  • member icon


Reputation: 11040
  • View blog
  • Posts: 18,852
  • Joined: 19-March 11

Re: Are you afraid to refactor?

Posted 19 January 2018 - 12:08 PM

View Postbaavgai, on 19 January 2018 - 01:52 PM, said:

I tend to refactor as I go. The reason refactoring gets lost in real world development is time: it needs to be done now. If you take a moment to clean up something you did the day before you have the satisfaction of cleaner code and the benefit of something that will be easier to debug then you inevitably must look at it when the project is long forgotten.


I'd add to this that if you find a need to refactor, it's got to be because you came across something about the existing code that made it difficult to make a change that the user wants. If you don't do the refactor now, you're paying for it twice, or maybe three times. First of all, you're paying in time to make the change the hard way. You might be paying again in code debt that you add in order to shove this change in where it doesn't fit well. And finally, you're paying for the eventual refactor.

On the other hand, "do it now" doesn't mean "refactor all the things, all the time". As I see it, there are two wrong times to do a refactor: the first is before you need it, and the second is after you need it.
Was This Post Helpful? 1
  • +
  • -

#8 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 1460
  • View blog
  • Posts: 4,726
  • Joined: 20-March 10

Re: Are you afraid to refactor?

Posted 19 January 2018 - 12:12 PM

Yeah,

I would agree with that Jon to a certain extent but Project goals change or new features are added so refactoring as you go isn't always possible...
Was This Post Helpful? 0
  • +
  • -

#9 ndc85430  Icon User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 798
  • View blog
  • Posts: 3,259
  • Joined: 13-June 14

Re: Are you afraid to refactor?

Posted 19 January 2018 - 12:37 PM

The team should be taking part in defining what their upcoming work is (we do this by backlog grooming) and making the case for refactoring tasks.

Of course, as mentioned earlier on, you must have good automated tests in place to let you refactor safely.
Was This Post Helpful? 0
  • +
  • -

#10 astonecipher  Icon User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2517
  • View blog
  • Posts: 10,086
  • Joined: 03-December 12

Re: Are you afraid to refactor?

Posted 19 January 2018 - 01:55 PM

I have worked in code bases that made it impossible to refactor, I know people will find that hard to believe. It is a good exercise when you learn new things.
Was This Post Helpful? 0
  • +
  • -

#11 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7160
  • View blog
  • Posts: 14,924
  • Joined: 16-October 07

Re: Are you afraid to refactor?

Posted 20 January 2018 - 07:37 AM

View Postjon.kiparsky, on 19 January 2018 - 02:08 PM, said:

On the other hand, "do it now" doesn't mean "refactor all the things, all the time". As I see it, there are two wrong times to do a refactor: the first is before you need it, and the second is after you need it.


To be clear, when I say "refactor incrementally" I'm talking about taking the time to step back from what you've recently done and seeing if there is some obvious clean up that presents itself. This is not to be confused with premature optimization, which I believe is part of your protestation.

I agree: premature optimization is to be avoided. I'd also avoid the other extreme of just "getting it to work" and moving on without any further consideration. Ideally, refactoring incrementally is the happy medium between those two extremes.

Another extreme, when folks talk about refactoring, is looking at an entire code base and applying sweeping changes. This, indeed, is cause for concern. Here, refactoring can come uncomfortably close to rewriting. If rewriting is on the table, I usually just prefer to black box the thing and not look at the code base at all, beyond understanding business rules in play.

View Postastonecipher, on 19 January 2018 - 03:55 PM, said:

I have worked in code bases that made it impossible to refactor, I know people will find that hard to believe.


Alas, I find this very credible. Hence, my suggestion of the black box.
Was This Post Helpful? 1
  • +
  • -

#12 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2388
  • View blog
  • Posts: 5,013
  • Joined: 11-December 07

Re: Are you afraid to refactor?

Posted 23 January 2018 - 06:21 PM

A third reason to refactor is "refactoring to understand". I find that shifting the code around helps me to understand it. If I think I've made it clearer, I'll check it in. If not, I'll revert. Either way, I understand more than I did before.
Was This Post Helpful? 2
  • +
  • -

#13 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 6158
  • View blog
  • Posts: 21,233
  • Joined: 05-May 12

Re: Are you afraid to refactor?

Posted 23 January 2018 - 08:44 PM

Before people shout YAGNI and premature optimization, I sometimes refactor to see if I can implement it as a parallel friendly implementation. Like cfoley noted, I find that this helps my understanding of the code, because it makes me reason with what the code is doing, rather than just mechanically applying the refactoring methods listed by Fowler. This version usually doesn't get merged back into the development branch unless the code is significantly better. Better in terms of clarity and performance. If it is merely a perf gain, but makes the code harder to understand, then definitely no merge.
Was This Post Helpful? 0
  • +
  • -

#14 jon.kiparsky  Icon User is offline

  • Beginner
  • member icon


Reputation: 11040
  • View blog
  • Posts: 18,852
  • Joined: 19-March 11

Re: Are you afraid to refactor?

Posted 23 January 2018 - 08:47 PM

No shouting here, I'm just going to quietly sigh and wonder where you get the free time.
Was This Post Helpful? 0
  • +
  • -

#15 BetaWar  Icon User is online

  • #include "soul.h"
  • member icon

Reputation: 1509
  • View blog
  • Posts: 8,273
  • Joined: 07-September 06

Re: Are you afraid to refactor?

Posted 23 January 2018 - 09:16 PM

I don't fear refactoring. In fact, I typically enjoy it (possibly a little too much). I have been known to start a refactor and then before I finish it I find some other design decision that would be "better" and start refactoring the refactor. When it comes to work code though, I typically find that management won't allow refactors because it isn't a "new sellable feature" which means that the tech-debt just grows...
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2