views:

471

answers:

6

My studio has a large codebase that has been developed over 10+ years. The coding standards that we started with were developed with few developers in house and long before we had to worry about any kind of standards related to C++.

Recently, we started a small R&D project in house and we updated our coding conventions to be more suitable for our environment. The R&D work is going to be integrated into existing project code. One major problem facing us is that we now have two standards for the two areas of work, and now the code bases will cross. I don't want two standards at the studio, and I'm actually quite happy to move forward with a single standard. (The 'how' of how we got into this situation isn't important -- just that we are and I had hoped that we wouldn't be.)

The problem is refactoring existing code. I'm not very keen on having two code bases (one relatively small and one very large) looking different. I am interested in doing some refactoring of one of the existing codebases to make it conform to the other standard. The problem is, the smaller code base is (IMO) the more desireable standard.

I started looking around for a tool that could do large scale refactoring for me. I'm not interested in rearranging and tightening code. I'm interested in changing things like

class my_class {}
....
class my_class A;

to

class MyClass {}
....
class MyClass A;

Basically doing function/variable level renaming. I'd prefer not to use something like Visual Assist because that will take a long time. I have upwards of 10000 source/header files with hundreds of thousands of lines of code. Using VA one class at a time would be a time killer and not worth the effort.

I did run across Vera in another post on SO. That seems like it might do the job and do it well. I'd like to know if anyone has specific experience using Vera for the situation that I'm in, or has any other recommendations for tools that might get the job done. I think that it's important that this tool actually understand code structure so that we don't wind up just renaming variables in a search/replace manner because that will lead to subtle bugs if not done carefully.

EDIT: While my example shows I'm going from using _ between names to camelcase type notation, it might be more beneficial for us to move the other way. I'm really looking for a generic solution that will help with large scale renaming.

Thanks.

A: 

I think renaming variables is going to be tricky - fortunately you're going from _ convention to Capitalised so it won't be so hard (though _ is easier to read and better)

I would take a code beautifier (eg Artistic Style or Uncrustify) and modify them to do the conversion. You only need a few custom rules for this conversion so it won't be too hard.

gbjbaanb
My example was perhaps a preference, but far from a certainty. We might move the other way. I'm more curious about ways to get the job done. I've edited the question for clarity.
Mark
crikey, then you have a large job ahead of you. I would do it piecemeal - as you alter a module, alter the names. You'll also get to see which ones are untouched that way. Your team should be ok with the practicalities of such an approach too.
gbjbaanb
PS _ are roughly the standard used in C++ code, Stroustrup thinks they offer better readability, and the STL and boost libs use them so it kinda makes sense.
gbjbaanb
+8  A: 

My process would be to rename each time someone touches a given module. Eventually, all modules would be refactored, but the incremental approach would result in less code breakage(assuming you have a complete set of tests. ;) )

Paul Nathan
+2  A: 

I've made changes like this using custom scripts. If I can, I use sed. Otherwise I'll use a scripting language with good support for regular expressions. It is a crude hack which is sure to introduce bugs but unless you find a better solution, it is a path forward.

ejgottl
I agree, if the tools are too slow for you and the transformations are simple -- as the example above -- a little script will work like a charm, especially if you have automatic tests which check if everything still works
Tetha
A: 

IMHO, variable renaming is simply not worth the effort. The important thing is that code is robust, readable and performant. Just adopt whatever style was used before and spend your time on important things.

Nemanja Trifunovic
variable renaming will be a problem once code with the new style calls functions or uses classes from the old-style code. While I agree it's not as important as being robust, it becomes hard to write after some time.
moogs
It's not just about variables. It's about consistency, which I think is important. Believe me, I have wrestled with the notion of whether this truly important, but I think that some measure of consistency in the codebase is worth some amount of effort.
Mark
Sometimes it is worth the effort. If you are using hungarian notation, the variable type may be encoded in the name. Change the variable type, and you may need to change the name as well. I never use the notation on new code, but these days I'm mostly working in old code and it is everywhere.
criddell
+2  A: 

Unless you have (1) a fairly complete set of reliable and automated tests and (2) a refactoring tool that understands C++ semantics (I haven't heard of such a tools), I would advise against making automated renames. Everywhere I worked the practice always was to only refactor modules you were working on at the moment. It's a lengthy but relatively painless process.

sudarkoff
A: 

You might want to consider looking into "pork", which is used, created and maintained by the Mozilla folks, to do largely automated C++ source code analysis, including source code transformations such as refactorings. It is scriptable using JavaScript and supports pretty complex semantic analyses and transformations. So renaming symbols is one of the easier things to do for pork.

Now that GCC plugins are on the way, it is likely that such things are going to become easier in the future.

none