What's your refactoring process?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
VirtuosiMedia
Forum Contributor
Posts: 133
Joined: Thu Jun 12, 2008 6:16 pm

What's your refactoring process?

Post by VirtuosiMedia »

I'm just curious how everyone on here goes about refactoring their code in hopes of picking up some good advice. For me, I try to write a working version first and then I go back and see where I can separate functionality into different methods or classes so that each one has one primary task. If I find that I repeat a particular section of code, I try to find a way to abstract it so that it can be reused. I try to write good comments the first time around, but I'll clean those up when I go back over it. I also check to see if my variable names are logical and descriptive and that my class and function parameters are consistent. What do you do?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: What's your refactoring process?

Post by alex.barylski »

I imagine someone on here will advocate writing tests first.

Unforunately most refactorings I've been responsible for it was not possible to refactor, as the code was such a spaghetti mess.

My process on other code typically goes:

1. Extract all HTML into templates
2. Extract all SQL into table data gateway API
3. Extract all business logic into model and wire gateway API into model
4. Merge TDG into model and remove duplication
5. Funnel everything through a single point of entry (index.php)

I don't bother with actual controllers unless I know it's a project I'll be working on for a long time to come.

Step 4 I find helps as the SQL code is often duplicated so having it centralized first in a TDG lets me minimize duplication before standizing the SQL and merging with business logic.

Obviously testing every step of the way is important but perhaps more important is

1. Learning the codebase and its conventions/caveats
2. Start simple and don't continue to next step until everything in previous steps are finished

Test. Test. Test.

Write tests when you actually have an API to test if desired.

Removing the HTML from code will *really* clean it up and model code makes a huge difference as well. Controller logic and such always comes last otherwise you end up with controller logic in your templates and/or model.

Cheers,
Alex
User avatar
allspiritseve
DevNet Resident
Posts: 1174
Joined: Thu Mar 06, 2008 8:23 am
Location: Ann Arbor, MI (USA)

Re: What's your refactoring process?

Post by allspiritseve »

PCSpectra wrote:I imagine someone on here will advocate writing tests first.
Me! Test first. Test often. Otherwise how will you know everything still works when you're done? Manually visiting every page and filling in every form?
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: What's your refactoring process?

Post by crazycoders »

Too often, coding standards are not respected at all and refactoring is almost impossible. I'm stuck at work here with a 15 years of VB6 software that has been coded in an unknown design pattern (can't even say if there is a design pattern at all)

I've so far redone and reimplemented the API completly into a singleton/factory style (never able to remember which is which) and am now redoing the web/app visual part... one hell of a job.

What i could say is you need to evaluate all the lacks of the current implementation and establish what is dependant on what so that you don't kill something else in the process of refactoring. More than often its not even possible so you have to scrap something and then fix everything that was dependant on it. Just easier when everything is OOP since it will all crash at once when recompiling.
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Re: What's your refactoring process?

Post by panic! »

export svn repository of framework
create new svn directory
create views (or use the views the web producers have made)
do php for controllers
QA
svn branch on to live server
done!
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: What's your refactoring process?

Post by Jenk »

1. Review and refactor if necessary (or if legacy, create) tests.
2. Run tests, ensuring they pass.
3. Begin cleaning code under test.
4. Run test, ensuring pass.
5. Goto 3.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: What's your refactoring process?

Post by josh »

Re factoring should remove coupling ( for instance of 2 blocks are similar, refactor them to be identical and once they are extract to a method ), and increase cohesion ( which comes naturally with TDD )

TDD is a great way to drive the refactoring process, it doesn't improve your design directly I find, as much as I find that it improves your design as a result of making re factoring much more painless, with what used to take days and leave me stressed out now gets done in hours and leaves me motivated to hit that next green bar or write some more tests
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Re: What's your refactoring process?

Post by Jenk »

TDD improves your design because of one simple point - it allows you to entirely focus upon design. Once you have a passing test, you don't need to be concerned with "will it still work" as your test(s) are there to do that for you, and you should be running your tests after every small change, right up until you are satisfied the requirement is obtained, then you can refactor, and again after every small change, or even if you just feel like it ("Did I run my tests?") run your tests again. As often as possible is the best mantra, because as soon as it fails, you can immediately identify what broke it.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: What's your refactoring process?

Post by josh »

Yep, it removes the fear and uncertainty, allowing you to focus your cognitive efforts at the task at hand
Post Reply