views:

147

answers:

7

In a world where most ship dates are dictated by business needs, programmers usually ship code that works. Often, structure and efficiency of code being shipped become moot when you know code works. Unless production quality is specified (api to an algorithm for example), for code running into few hundred lines, shippable code equals code that works.

My question is this: Give an ETA for a feature, would you code until feature works and be done? Or would you get it to work as quickly as possible and refactor for release quality?

My inclination is towards the latter though it sounds like more work. When code that works is taken apart for algorithmic efficiency and patterns, it is a joyful experience putting it all together. Besides, it gets all that non-functional love - less bugs, performant, extensible, secure.. I don't think I am good at writing the best code first time. So this approach works well for me.

I'd like to know which one is preferred and why? I am not looking for industry-wide approach, just individual propensities so I can gauge likeness of thought.

A: 

For me, refactoring should be done after shipping, ensuring all the functionality works, and left it to the next iteration of code building. If you start manipulating code before shipping you may end up doing little optimisations and finally be left with invalid code.

Tomasz Kowalczyk
+2  A: 

If you're doing Test Driven Development. Often you write the simplest thing that works first and then refactor as additional functionality is added (AKA Red, Green, Refactor).

There's obviously a big grey area in this question though. If you're shipping an unmaintainable mess then perhaps you should review how you're going about writing code in the first place. Refactoring should be done for some purpose - to make code more flexible to allow a new type of Product for example. Don't just refactor because you feel you should.

Rob Stevenson-Leggett
So after you know feature works per spec, would you not take a step back and look for efficiencies and patterns? I don't think I meant refactor always. Should you look before you ship or not.
batta
As others have said, it depends on the business constraints. I'm not saying don't refactor. I'm saying don't refactor if it's going to affect your timescales. I often mark things like TODO: Refactor to pattern X or TODO: This should be encapsulated in it's own class etc.
Rob Stevenson-Leggett
+5  A: 

I prefer refactoring before and after shipping.

Postponing any refactoring until after release sounds awfully like you're probably never actually going to do it (more often than not, something more business-critical comes up). But even if you do it before shipping, it's not like you're code is perfect and you run out of things to improve. So afterwards too (as long as the code is maintained to any extent).

To me, refactoring code into something simpler and cleaner is continuously and naturally part of any software development work.

Edit: Obviously you need to consider business constrains when deciding how much and for how long you refactor at a given moment.

Edit 2: As for the question "how to convince my manager about refactoring" (see comments), here are some resources that might help:

Jonik
I fully agree to the notion of continuous refactoring. Trouble is, how often can you convince your manager that you need to change code because you need to refactor.
batta
IMO, a manager should trust developers in such matters (and consider refactoring as an integral part of development that's not really his business). Of course developers should also be trustworthy and not break trunk / production branch with untested or unreviewed changes.
Jonik
Jonik
Yes, but I want to stand my ground and prove the difference it makes. It's been a year and change does happen. Part of the problem could be legacy system nobody wanted to touch.
batta
@batta: It of course depends on what kind of manager you have, but the resources I edited in the answer *might* help in convincing him. By the way, if you're dealing with a big legacy codebase, that could actually make it easier to prove that refactoring first makes fixes/enhancements *faster*.
Jonik
And if the manager just won't buy it, well, *don't tell*. As Fowler puts it: "A schedule-driven manager wants me to do things the fastest way I can; how I do it is my business. The fastest way is to refactor; therefore I refactor."
Jonik
A: 

I try to write the best code initially but have always found that once it goes to production refactoring is inevitable. Usually refactoring after code release has been the norm because of user feedback.

Prashant
user feedback has nothing to do with refactoring - refactoring just changes the implementation, not the functionality
Arne Burmeister
+2  A: 

In our team, we consider non refactored code as not "done". In other words, "code has been refactored" is part of our Definition of Done, it's one of our criteria for shippable code.

Pascal Thivent
A: 

I would prefer to refactor before shipment. You are right, my first code is often not the best design. But if youu have a passing test for the code, there should be little risk to refactor directly afterwards.

And the problem with doing it later is simply "after the release if before the release". In my experience there is no reason to hope there is time to clean up after the release.

Arne Burmeister
+1  A: 

Problem is, if you have tendency to only refactor after shipping, you will never do it ;)

I tend to see 'done' including well factored code.

There are exceptions of this rule, if there is a very large scale refactoring which involves high effort. To meet a deadline you could push to next development iteration.

manuel aldana