Find your content:

Search form

You are here

What are the recommended ways to refactor in Apex?

 
Share

I had to rename a class or two and I'm used to the Eclipse refactoring capabilities which update all references as part of the name change. In Salesforce the rename is not supported in the IDE (v 25), so the way to rename is to do it in SF (web) itself. The problem is that when I rename the class not all references are updated. After I did the rename I compiled, saw errors, fixed and repeated that until no more errors.

Afterwards, I thought a better workaround would be to create the new class in the IDE and copy the contents of the old class into it, fixing up any self references to get it to save w/o errors. Then do a find and replace in files in eclipse to update any *.cls, *.trigger, (and maybe *.page, *.component) files to use the new reference. Then save to the server and delete the old class. Next time, I may try that.

My questions are what is the best approach to refactoring Apex? Are there any tools or best practices to be more efficient and accurate? Thanks.


Attribution to: Peter Knolle

Possible Suggestion/Solution #1

I do it via your second way i.e. File search in eclipse, but instead of a new class. I mostly create a new method with required changes in the same class, after all the dependencies are updated I just delete the old method header and reuse the same old code with new one. Same applies to variables as well.


Attribution to: Abhinav Gupta

Possible Suggestion/Solution #2

I use the second method. After refactoring, I select the 'src' folder, use File Search/Replace and all the changes are made and saved to the server in one go.


Attribution to: Bob Buzzard
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/875

My Block Status

My Block Content