R# Refactoring Steps For Introducing a Const

I’m currently writing some pipeline components for SSIS and I’m curious as to whether there is a commonly followed refactoring pattern for extracting constants.  For example, consider the following.

var authenticationType = ComponentMetaData.CustomPropertyCollection.New();
authenticationType.Name = C_AUTHENTICATIONTYPE;

There’s nothing unique to this code.  In fact, if you looked further at the surrounding code, you would see a whole bunch of similar declarations – and that’s where it can get a bit annoying.  If C_AUTHENTICATIONTYPE were anything other than a const, I wouldn’t think twice about simply typing in the above code and letting ReSharper prompt me to create a variable/field/etc.  However, I haven’t found a similar pattern for creating a const – and I *hate* the idea of manually entering the const declaration as it throws me out of my rhythm. 

So here’s what I’m currently doing – let me know if there’s a better way.


First, I’m having R# create a new local variable (standard alt+enter).  Next I’m executing the “Introduce Field” refactoring on the new variable and selecting the “Introduce constant” option (note that if you haven’t initialized your local variable, this option will be unavailable).


This works pretty well – certainly better than having to context switch in order to go declare a const.  I did see that there is a “to const” option for variables.  However, this option creates a local const which doesn’t help all that much, and I couldn’t find a secondary refactoring to extract the const from the method to the class scope (at least not in R#).

About Howard Dierking

I like technology...a lot...
This entry was posted in R#, Refactoring. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://codebetter.com/members/hdierking/default.aspx hdierking

    method 2 (which I like better and initially missed because it requires an initialization statement before “to const” is an option):

    – alt+enter -> “create field”
    – add an initialization statement
    – alt+enter -> “to const”

  • http://thelimberlambda.com/ Eric Smith

    I’m heartened to see other professionals using stuff like C_MYCONSTANTNAME without being called out for using an “arcane” underscores-with-capitals naming convention for constants.

  • http://darkmethod.com alear

    If you first do a “to const” on the variable, then the “Introduce constant” option will be automatically selected for you in the “Introduce Field” dialog.

  • fschwiet

    I usually add a class shameless named “Magic Constants” somewhere in project. When I need to add a constant, I’ll just type as if it already exists (a = MagicConstants.Apples) then with Resharper hit alt-enter and it suggests I create “Apple” as a static property.

    Extract local variable might be nice if the constant is already embedded in a bunch of places and want to replace them all at once. Extract, assign the constant value where the variable is declared, then inline variable.