17

I work in a pretty large Java-project (2500+ classes) that uses an old code standard where all member variables are prefixed with "m_" (e.g m_temperature). There is really no reason for this any longer and I'd like to get rid of them but:

  1. In order to make the change I must do all variables at once.
  2. It must not generate any bugs.

The first naive approach of simply renaming all the "m_variable" to just "variable" will not be sufficient as that could produce name collisions between an already existing variable named "variable", and the newly renamed one.

So, to sum up: How do I rename all these pesky member variablest without getting into trouble and are there any more problems than the one mentioned above?

Yes, I'm aware of the refactoring/renaming features within IDEs, please bear in mind that I want to do the changes to all variables matching the criteria at once and not by right-clicking on variables and renaming them one-by-one.

tommys
  • 865
  • 1
  • 11
  • 21
  • You can do that using eclipse ide's refactoring tool. – Josnidhin Feb 16 '12 at 09:16
  • maybe you mean that you only need to specify the criteria for example, prefix with "m_" and the "tool" will look up the entire project for all global variable and replace it? – Bryan Giovanny Feb 16 '12 at 09:35
  • Bryan: YES, that is what I'd like to do. In IntelliJ and Eclipse etc. one can create a code standard that the IDE follows when generating/autocompleting code and it is there possible to add or remove prefixes such as "m_" for class-variables. The sad thing is that when changing the code standard given these things(in my case removing m_ as prefix for class-variables) one cannot enforce it upon already existing code (not in a way that I have found at least) it do work with code formatting, though. – tommys Feb 16 '12 at 09:56
  • why do you need to make the change at once? – wintersolutions Feb 16 '12 at 09:57
  • 1
    Pizza: since It would first of all ruin my brain to do such dull things over and over and secondly It would be like stealing from my employer by spending the time it may require to do so. Automation is good, repetition is evil! :) – tommys Feb 16 '12 at 10:09
  • 1
    @tommys: Well my question wasn't very precise it should have been: how many variables. If you only got say 3 variables youre better off using an IDE if you got alot of variables maybe the link in my answer below gives you an non repetive, fully automated way to handle this. But you have to spend a little bit of time setting it up. On the other hand your employer was stealing from himself by letting such things happen to his codebase, didn't he? – wintersolutions Feb 16 '12 at 10:17
  • wintersolutions: I need to do this at once in order to prevent mergeconflicts for my fellow colleagues working on different branches of the codebase. Given the employer allowing this theft at an earlier state: Yes, you are right but we must not allow ourselves to push the responsibility of the bad tech quality on the business - they should be able to rely on the techies to not mess things up, just as we should expect them to not f**k up the business :) – tommys Mar 10 '14 at 10:20

6 Answers6

9

How about below from : mass renaming of java variables

by Simulant

click on the variable name.

1:press [alt] + [shift] + [R]

2:enter the new name.

3:press [enter] to confirm.

-->all instances of this variable will be renamed.

Community
  • 1
  • 1
smwikipedia
  • 61,609
  • 92
  • 309
  • 482
6

Theres a question on SC which is about a massive refactoring in java too. The best answer is using japvaparser and implementing a visitor to do the actual refactoring. This shouldn't be that much work for a simple rename.

Community
  • 1
  • 1
wintersolutions
  • 5,173
  • 5
  • 30
  • 51
0

To answer your second question (are there any more problems)

  1. I would avoid global variables. Favour encapsulation such that you can localise functionality.
  2. I would implement some unit tests such that you can verify future changes. At the moment it appears your only available check is that your project compiles properly.

I realise both of these are potentially sizable issues in an existing project. But it's worth lookingto identify possible pain points and work on those first of all.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
-1

Refactoring tool will help in this case. When you rename a variable, it will rename it in all dependent places including if it is called in different class file. While doing this, it will alert you in case if your renamed variable matches with already existing name.

However we have to do it manually for each variable.

Jayy
  • 2,368
  • 4
  • 24
  • 35
-1

I don't know if this can work. But I find this link to find and replace all occurence in a project using IntelliJ.

http://www.jetbrains.com/idea/webhelp/finding-and-replacing-text-in-project.html

Bryan Giovanny
  • 424
  • 2
  • 7
  • No it will not address the language specific problems such as variable name collision, but thanks anyway. – tommys Feb 16 '12 at 10:33
-3

If you are using eclispe IDE, you can do it in easy manner.

Steps:

  1. select the variable
  2. right click on it
  3. click on refactor
  4. click on rename

even in netbeans you can follow same steps. The IDE finds the reference and usages and replace it all.

dku.rajkumar
  • 18,414
  • 7
  • 41
  • 58
  • As I stated in my question: there are 2500+ classes with variables in them and I'd like to change them all at once, that is really not what you are proposing :) – tommys Feb 16 '12 at 09:36