KnowDotNet Visual Organizer

Refactoring Multiple Instances of Duplicate Strings

Use Insert Const Methodolgy

by Les Smith
Print this Article Discuss in Forums

Refactoring Multiple Instances of Duplicate Strings improves performance and reduces resource allocation.  No doubt, you find yourself having coded the exact same embedded string constant in multiple statements in a method and throughout a whole class.  

In the first place, it would be better to use a
const (C#) or Const (VB.NET).  Constants reside in the intern pool and once referenced, the CLR will keep them around so they don't have to be recreated for each use.  Secondly, it is much better to have only one copy of the constant, rather than having it allocated anew for each usage.  The problem is that, although we may know that we have created the string somewhere else in the Class or maybe even in the same method, but in the heat of the battle, think, "I don't know where that string is and I don't have time to go looking for it."

Well, you need to take the time, for performance purposes, resource usage purposes, and general good coding purposes.  If you don't take pride in your code, others maintaining it may not think good thoughts about your ability.  Maybe, you are one that does not worry about that.  I don't worry about it, but I do care, and I try to keep my code clean.

Knowing that you should clean these situations up is half the battle.  Now, to do it, manually.  First, find instances where you are repeating embedded strings that are duplicated elsewhere in the method or even in the class.  Second, copy an instance of the quoted string and make a const out of it.  Place it either at the top of the method, or if it is being used over the class, place it at the top to the class.  Next, use the Find and Replace Dialog and replace the instances of the string with the name of the new const.  

Sounds like a lot of work?  Ok, try
NET Refactor's Insert Const or String feature.  Figure 1 shows a code window (purely demonstration code) with multiple uses of a message embedded string (extreme case).  You will see that I have selected one of them for usage by the tool.

Figure 1 - Multiple Duplicate Embedded Strings.

Multiple String Usage


Next, I will click on the Insert String or Const menu option, found under the Rename Menu of NET Refactors main menu.  Figure 2 shows the Dialog that will be displayed when I click this menu item.

Figure 2 - Insert Const or String Dialog.

Insert Const Dialog


I can choose to make either a const or string varaible from the selection.  I am going to build a const, but I am going to check the Class Level option because the string is in use in multiple methods.  I will enter a new name, "longMsg", and click the Save Button on the dialog.  Figure 3 shows the new const that has been created at the top of the Class.

Figure 3 - New Class Level Const.

New Const


The tool automatically changes all references in the Class to point to the new const, instead of creating three instances of the embedded string.  Figure 4 shows the three lines of code where the embedded strings once resided.  They now all point to the const.  Just one of the many features of NET Refactor.

Figure 4 - Embedded Strings Replaced with Const.

Replaced Strings


NET Refactor has 23 powerful tools that aid in refactor, cleaning up, reorganizing your code.  To try it free for 20 days, click here.

Top of Page

Writing Add-Ins for Visual Studio .NET
Writing Add-ins for Visual Studio .NET
by Les Smith
Apress Publishing