Posts Tagged CodeRush
CodeRush features that I’ve been spoiled by: String.Format and Introduce Local
Posted by claudiolassala in Productivity, Software Development, Uncategorized on July 27, 2007
Ok, some of the features may not be CodeRush; they may be Refactor! Pro instead. Since I have both installed, I never know which one is which… anyway, whoever buys CodeRush should definitely go with Refactor! Pro as well. 🙂
This is refactoring that you can use to replace pieces of a string with variables. For instance, say I have the following string:
And I want to replace the text highlighted with variables, I can select the first part ("Claudio"), and then hover over the selection. CodeRush then shows me a smart tag, and if I hover over the smart tag, I get the following:
Notice that it shows a preview of how it is going to change my code, in case actually click on the "Introduce Format Item" option. Next, I can select the word "Houston", and do the same as I just did above:
So the code after the refactoring look like this:
As you may have noticed, "Introduce Format Item" is the only option available on the smart tag under that context. As a shortcut, instead of hovering over the selected text, wait for the smart tag to show up, click on it, and click on the option, you could also just select the text, and press Ctrl+~; that action performs the refactoring automatically. And you can keep selecting text within the string and hitting the shortcut, and CodeRush will keep adding variables to the String.Format call. Ain’t that pretty cool?
In case you want to do the opposite (take the variable and put it inline within the formatted text), just select the variable (for instance, the "Claudio" one), and select "Inline Format Item":
And this is the final result of that refactoring (notice that when it was done creating the variable, I typed "myName" to rename the variable to something more appropriate, and CodeRush fixed both the name of the variable and the place where it was being used):
By the way, the graphic elements on the snapshots (such as the arrows and the strike-through) were rendered in Visual Studio by CodeRush; I didn’t add that in any image editor.
Expanding on the previous topic, we may want to replace the variables on the String.Format call by actual local variables.
Same deal here: select the string that you want to store to a local variable, press Ctrl+~ (or hover over the selection to get the smart tag), and select "Introduce Local". That does the job of creating a local variable for you and assigning the value to it.
That’s it for now. There’s more to come in the next few days.