"Promote field to parameter" Refactoring?

Is there a refactoring to promote a field to a parameter? After using "Extract
Method", I often want to make the code even more reusable by being able to
pass in different values rather than accessing a single field directly.


3 comments
Comment actions Permalink

You can use Encapsulate Field to create a property accessor (getter and
setter) for a field, and this should automatically change all references to
the field to reference the property instead. I don't remember the shortcut,
though, since I always remap Encapsulate Field to CtrlR, CtrlE (much more
intuitive I think).

--


"flipdoubt" <flipdoubt@nospam.com> wrote in message
news:da27a8e454ede8c95553c2174cbe@news.jetbrains.com...

Is there a refactoring to promote a field to a parameter? After using
"Extract Method", I often want to make the code even more reusable by
being able to pass in different values rather than accessing a single
field directly.



0
Comment actions Permalink

You can use Encapsulate Field to create a property accessor (getter
and setter) for a field, and this should automatically change all
references to the field to reference the property instead. I don't
remember the shortcut, though, since I always remap Encapsulate Field
to CtrlR, CtrlE (much more intuitive I think).


Thanks for the tip, but I don't want to be stuck using just one value, even
if it is a property. Let's say I have code like this:
// Code 1:
decimal mb = ( Decimal.Parse( _LongBytesField.ToString() ) ) / ( 1024 *
1024 );

When I use "Encapsulte Method" on Code 1, I get this:
// Code 2
private decimal BytesToMegaBytes()
{
return ( Decimal.Parse( _LongBytesField.ToString() ) ) / ( 1024 * 1024 );
}

I want to pass _LongBytesField in as a parameter rather than access the field
or property, like so:
// Code 3
private decimal BytesToMegaBytes( long bytes )
{
return ( Decimal.Parse( long.ToString() ) ) / ( 1024 * 1024 );
}

I suppose the "Make Method Static" refactoring should work, but it would
be a nice option in the Extact Method dialog to replace fields and properties
with parameters in the extracted method.


0
Comment actions Permalink

Hello flipdoubt,

Select _LongBytesField in the extracted method and use Introduce Parameter
refactoring.

Sincerely,
Ilya Ryzhenkov

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


>> You can use Encapsulate Field to create a property accessor (getter
>> and setter) for a field, and this should automatically change all
>> references to the field to reference the property instead. I don't
>> remember the shortcut, though, since I always remap Encapsulate Field
>> to CtrlR, CtrlE (much more intuitive I think).
>>
f> Thanks for the tip, but I don't want to be stuck using just one
f> value, even
f> if it is a property. Let's say I have code like this:
f> // Code 1:
f> decimal mb = ( Decimal.Parse( _LongBytesField.ToString() ) ) / (
f> 1024 *
f> 1024 );
f> When I use "Encapsulte Method" on Code 1, I get this:
f> // Code 2
f> private decimal BytesToMegaBytes()
f> {
f> return ( Decimal.Parse( _LongBytesField.ToString() ) ) / ( 1024 *
f> 1024 );
f> }
f> I want to pass _LongBytesField in as a parameter rather than access
f> the field
f> or property, like so:
f> // Code 3
f> private decimal BytesToMegaBytes( long bytes )
f> {
f> return ( Decimal.Parse( long.ToString() ) ) / ( 1024 * 1024 );
f> }
f> I suppose the "Make Method Static" refactoring should work, but it
f> would be a nice option in the Extact Method dialog to replace fields
f> and properties with parameters in the extracted method.
f>


0

Please sign in to leave a comment.