views:

214

answers:

5

As far as I know, and have gathered from other SO posts, there are no proper tools for refactoring PHP code yet, so when it comes to refactoring, it's probably good old search-and-replace for most of us, with a good amount of praying that we didn't overlook something.

I would like to know whether there are any coding recommendations on how to write code friendly for manual refactoring. Never to construct variable names from strings, would be one thing that comes to mind because a construct like that is impossible to grep:

$object->{"field_".$fieldname}

I could imagine there are several such do's and don'ts. Maybe somebody knows good resources / articles on the issue. It wouldn't have to be PHP specific, either.

+5  A: 

Unit tests always help me identify places where I've broken code due to a refactor. Unit tests in dynamic languages (PHP, Ruby, Python, etc.) provide assistance where static typing in other languages (Java, C#) would typically allow you to more safely refactor.

John Paulett
Yes, definitely. But I think it's a different issue and I'd like to concentrate more on the code writing aspect for the moment.
Pekka
@Pekka, I definitely see what you are saying, and unfortunately I have little to offer for that beyond grep/sed/awk. But, I fear that the lack of good upfront refactoring tools, means that the only effective way to deal with refactoring in dynamic languages is a comprehensive test suite.
John Paulett
A: 

Your question makes a certain amount of sense. But—at the same time—it implies that the implementation is known to be inadequate, and written to be replaced. Why not just architect it properly the first time?

wallyk
Totally Agreed with this.
You can't plan for every eventuality or requirement that might come across the project at a later date, and change its structure deeply. You start with nice, clean code, but if you don't look out, after a few versions, you are standing in front of a mess. In my experience, constant refactoring *before it becomes necessary* is the only way to prevent this, and is being done much too little
Pekka
No decisions are final so even if your architecture meets the requirements today, doesn't guarantee that it will meet the requirements tomorrow. Planning around ease of maintenance is a good approach -- much better than assuming your code base will live forever in its current form.
Jonathan Fingland
But you should plan it to be extensible and maintainable no? and this can be done by loose coupling and high cohesion and tiering applications.
+1  A: 

Well, The best way to write refactoring friendly code is to write loose coupled ,highly cohesive code and object oriented code.

You should try as much abstraction as you can, after all abstraction is the keyword while programming.

Moroever, you should be layering your code into presentation layer, business layer, data layer etc.. and Using design patterns is a pretty good solution.

I d recommend you to read Martin Fowler.

A: 

First, make sure your variable names make sense. If possible, go as OOP as you possibly can, or at least keep everything organized (image function file, database file, etc)

Second, and this is handy, check your IDE. Netbeans has options for refactoring. You can search in a file, in a folder, in a project, etc.

Cryophallion
+2  A: 

Avoid magic as much as possible: variable variables, eval, masking errors with @ and storing code in the database will come back to bite you.

Ken