views:

358

answers:

5

After frequently coming across recommendation not to use leading and double underscores in C/C++ identifiers I decided to fix all our sources once and for all. What I need to do now is convert _Identifier to Identifier_.

Should I use a specialized tool for this task of regular expressions will do for this job? In latter case what is the pattern to match C/C++ identifier?

A: 

If your regression tests are solid, then you should have no problems if you just write a quick perl script to replace everything and run the test suite. If you don't have solid regression tests...well, then you can do the perl script replacement and just rebuild the code. If the compilation works, then chances are pretty good that everything is fine. In other words, try the quick solution, and only use a specialized tool if that doesn't work.

William Pursell
+13  A: 

Although I am one of those that frequently points out that names with leading underscores may be reserved, I strongly recommend you don't do this unless you are experiencing problems caused by the names. Making this global change will make your version control system less useful than it might otherwise be by causing all sorts of spurious diffs. Also, there is a chance of creating duplicate names.

Also, there are many underscore prefixed names that re perfectly valid. One thinks immediately of __LINE__ and __FILE__ etc, and of all the names of non-standard functions that may be provided by your specific implementation. Filtering out those names will be far from trivial (I would say next to impossible), certainly a simple Perl or sed script will not be enough.

So instead, I would encourage you to change the names on a file by file basis as you make other changes to the code. Change your programming standards to avoid the leading underscore names and write new code in accordance with the standards.

anon
I really agree with this. Where the underscore is is not that important.
Edouard A.
If you have issue tracking, though, do raise a (low priority) fault about naming, and when you update a file do make two checkins: one to fix the names and one to make other changes. Checkins like "made one-line change, and also modified 200 lines to fix naming" have really useless diffs.
Steve Jessop
I agree too. Don't try to fix something that isn't broken. I know code sometimes may look terrible. My projects are full of such things. But you will get used to it.
Holli
+7  A: 

If you use Visual Studio, there are refactoring plugins such as Visual Assist X to help you with this.

Edouard A.
+4  A: 

Perl should do the job, but there's Coccinelle for when it gets tricky.

+1  A: 

Netbeans can do this for the whole project, using the Refactor->Rename menu command. But it only works for a single identifier a time, so you'll need to reiterate for every identifier you need to change.

janesconference