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#).