Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

thejadedmonkey

macrumors G3
Original poster
May 28, 2005
9,464
4,067
Pennsylvania
mrichmon said:
When that happens it is time to refactor.

In any moderately complex software your understanding of the problem domain changes over the development life of the code. This almost always means that some of your early assumptions are invalidated as time goes on.

The primary reference for refactoring is: http://martinfowler.com/books/refactoring.html

This quote in another thread got me thinking. What's the best way to refactor PHP? It's near impossible to detect errors from an IDE, as PHP isn't strongly typed, and returning an integer is just as valid as a string or an object.

How do you do it?
 
Having a good suite of automated tests is probably the most helpful thing, in my opinion.

If you have no test coverage, I'd recommend getting in the habit of writing some high level tests using something like selenium or phantomjs. So when you do a refactoring, start by testing the existing functionality. Run your tests after each change to make sure you didn't break anything.

Often you'll break some other part of your app with your refactoring. When that happens, write a test for that part too. Then fix the regression. Little by little, you'll develop a suite of tests that act as a safety net for your refactorings.
 
This quote in another thread got me thinking. What's the best way to refactor PHP? It's near impossible to detect errors from an IDE, as PHP isn't strongly typed, and returning an integer is just as valid as a string or an object.

How do you do it?
Typically I go in with a plan for the original code structure. As stated, that plan evolves over time where I end up needing to decouple functions into full classes, for example. Other things I do is clean up the constructors and try to make them all standardized.

I don't refactor code for the fun of it. If it works then let it lie, learn your lesson and move on. We are all continually refining our skills. Coding hasn't been around very long ( compared to farming, medicine, law, barber shop quartets ) and we are all still learning what works best.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.