Get rid of Hungarian notation in C# code in automated manner?
Asked Answered
F

2

6

I have a large codebase that uses Systems Hungarian for most variable names, which basically means I have lots of objQueue's, objCommon's, dtDataSet's et cetera.

Is there any way to convert most of them? Especially, I want to get rid of irritating obj ones that have absolutely no sense, make variable names seem similar and the code completely unreadable.

There was a similar question but it was asking whether it's worth to do the replace.
Answers like “leave it as is” are not of any use to me because I'm definitely sure I need to do this.

The codebase is really large, and I don't want to rename variables one by one. Neither do I want to run Find & Replace because I'll get false hits and mess up code even further.

Is there any automated tool to make such replacements in Visual Studio? Some addin for Resharper, whatever?

Flinders answered 22/2, 2011 at 18:43 Comment(8)
Sadly, your best option is probably to write a plugin for ReSharper and use it's rich API to programmatically traverse all the declarations in your entire solution and apply an algorithm that can automatically perform the rename based on the name and perhaps contextual information like the declaration's type. Unfortunately, this is decidedly non-trivial and not for the feint of heart.Krems
What argument did you use to get rid of hungarian notation at your firm? I want to know, because I want to use it at mine. I'm so tired of putting xyzzy in front of every freaking variable!Superpower
Oh, hopefully my company is modern enough :-). We are outsourcing company, but before us our client used to work with another company which left some Orient flavor in the code, if you know what I mean.Flinders
Would you get false hits even if you use regex to search for all occurances of words starting with obj followed by a capital letter?Francis
@glowcoder IMO you don't need any better argument than a DO NOT quote from page 55 of seminal Framework Design Guidelines written by the .NET architects. The quote is "DO NOT use Hungarian notation." :)Francis
@kzen: I'm hesitant to use search&replace because it doesn't know about potential conflicts. Example: sometimes I have query and objQuery in the same scope. I hope for an automated tool that would let me know about potential conflict and ask for instructions.Flinders
@Kzen wow - it doesn't get clearer than that! I found a link to it: msdn.microsoft.com/en-us/library/ms229045.aspx and I'm going to remember it. I'm a Java guy, but I know my firm is Microsoft Friendly, so it might actually hold some weight.Superpower
Right click, refactor, rename variable.Lynnelynnea
F
2

Alas, but it seems like there is no bulk rename tool for C#.
Please correct me if I am wrong.

Flinders answered 1/3, 2011 at 4:4 Comment(0)
F
1

One solution would be to use something like Visual Assist's rename feature.

Other than that, a very careful search'n'replace (with a build between each modification followed by a check-in into source-control).

Frankel answered 22/2, 2011 at 18:47 Comment(3)
What does Visual Assist's rename feature do? Is it just like the built-in rename feature or is it more powerful?Krems
the rename (AFAIK) is context sensitive; it's not a search/replace.Frankel
Yup, but does it have bulk option? I don't want to run it on each variable separately.Flinders

© 2022 - 2024 — McMap. All rights reserved.