Move method: some ideas

There were a number of discussions around moving method between types. I've got the following idea, please discuss.

Method's 'working set' consists of parameters and type instance (this). Actually, it consists of publically visible static members of types, but this won't change during move, so we ignore this. Local variables stay inplace, so we ignore them too. So, moving method is "changing method enclosing type while maintaining its working set".

1. Temporary forget about access - consider all members publis (for simplicity). After move is complete we can fix access as required or leave it 'red' for user to take care.
2. Convert method to static by introducing explicit parameter 'thisParam' of enclosing type, replace all "this." access (including implicit) into "thisParam." constructs.
Method calls of "instance.Method(param)" are converted to "SourceType.Method(instance, param)".
Now we have static method and can move it to whatever type we need. Remember, all things are temporary public.
3. Move method to designated class. Method calls of "SourceType.Method(instance, param)" are converted to "DestType.Method(instance, param)".
4. If one of the method parameters has destination type, optionally perform reverse action to #2 - convert parameter into implicit "this" and change body accordingly. Calls are converted obviously: "param.Method(instance)"
5. Fix any access issues.

Probably, target type should be limited to types in parameters for instance methods. Or may be not - then #4 is skipped.

What do you think?

5 comments
Comment actions Permalink

Hello Ilya,

as far as I know, this refactoring (that moves a method to one of its parameters'
type) has been implemented in IntelliJ IDEA. http://www.jetbrains.com/idea/features/refactoring.html

seems to not mention it explicitly, but I think it can be expressed as a
combination of
'Move static member' and 'Convert to Instance Method'.

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

There were a number of discussions around moving method between types.
I've got the following idea, please discuss.

Method's 'working set' consists of parameters and type instance
(this). Actually, it consists of publically visible static members of
types, but this won't change during move, so we ignore this. Local
variables stay inplace, so we ignore them too. So, moving method is
"changing method enclosing type while maintaining its working set".

1. Temporary forget about access - consider all members publis (for
simplicity). After move is complete we can fix access as required or
leave it 'red' for user to take care.

2. Convert method to static by introducing explicit parameter
'thisParam' of enclosing type, replace all "this." access (including
implicit) into "thisParam." constructs.

Method calls of "instance.Method(param)" are converted to
"SourceType.Method(instance, param)".

Now we have static method and can move it to whatever type we need.
Remember, all things are temporary public.

3. Move method to designated class. Method calls of
"SourceType.Method(instance, param)" are converted to
"DestType.Method(instance, param)".

4. If one of the method parameters has destination type, optionally
perform reverse action to #2 - convert parameter into implicit "this"
and change body accordingly. Calls are converted obviously:
"param.Method(instance)"

5. Fix any access issues.

Probably, target type should be limited to types in parameters for
instance methods. Or may be not - then #4 is skipped.

What do you think?



0
Comment actions Permalink

Hello Dmitry,

As far as I see, this is not on the plan for Resharper 2.0. Are you going
to do method moving in 2.0?

DS> Hello Ilya,
DS>
DS> as far as I know, this refactoring (that moves a method to one of
DS> its parameters'
DS>
DS> type) has been implemented in IntelliJ IDEA.
DS> http://www.jetbrains.com/idea/features/refactoring.html
DS>
DS> seems to not mention it explicitly, but I think it can be expressed
DS> as a combination of 'Move static member' and 'Convert to Instance
DS> Method'.
DS>
DS> Regards,
DS> Dmitry Shaporenkov
DS> JetBrains, Inc
DS> http://www.jetbrains.com
DS> "Develop with pleasure!"
>> There were a number of discussions around moving method between
>> types. I've got the following idea, please discuss.
>>
>> Method's 'working set' consists of parameters and type instance
>> (this). Actually, it consists of publically visible static members of
>> types, but this won't change during move, so we ignore this. Local
>> variables stay inplace, so we ignore them too. So, moving method is
>> "changing method enclosing type while maintaining its working set".
>>
>> 1. Temporary forget about access - consider all members publis (for
>> simplicity). After move is complete we can fix access as required or
>> leave it 'red' for user to take care.
>>
>> 2. Convert method to static by introducing explicit parameter
>> 'thisParam' of enclosing type, replace all "this." access (including
>> implicit) into "thisParam." constructs.
>>
>> Method calls of "instance.Method(param)" are converted to
>> "SourceType.Method(instance, param)".
>>
>> Now we have static method and can move it to whatever type we need.
>> Remember, all things are temporary public.
>>
>> 3. Move method to designated class. Method calls of
>> "SourceType.Method(instance, param)" are converted to
>> "DestType.Method(instance, param)".
>>
>> 4. If one of the method parameters has destination type, optionally
>> perform reverse action to #2 - convert parameter into implicit "this"
>> and change body accordingly. Calls are converted obviously:
>> "param.Method(instance)"
>>
>> 5. Fix any access issues.
>>
>> Probably, target type should be limited to types in parameters for
>> instance methods. Or may be not - then #4 is skipped.
>>
>> What do you think?
>>
Sincerely,
Ilya Ryzhenkov


0
Comment actions Permalink

OK, I forgot 'Convert Instance Method to Static' that should
be the first on the chain of refactorings.

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

Hello Ilya,

as far as I know, this refactoring (that moves a method to one of its
parameters'

type) has been implemented in IntelliJ IDEA.
http://www.jetbrains.com/idea/features/refactoring.html

seems to not mention it explicitly, but I think it can be expressed as
a combination of 'Move static member' and 'Convert to Instance
Method'.

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

>> There were a number of discussions around moving method between
>> types. I've got the following idea, please discuss.
>>
>> Method's 'working set' consists of parameters and type instance
>> (this). Actually, it consists of publically visible static members of
>> types, but this won't change during move, so we ignore this. Local
>> variables stay inplace, so we ignore them too. So, moving method is
>> "changing method enclosing type while maintaining its working set".
>>
>> 1. Temporary forget about access - consider all members publis (for
>> simplicity). After move is complete we can fix access as required or
>> leave it 'red' for user to take care.
>>
>> 2. Convert method to static by introducing explicit parameter
>> 'thisParam' of enclosing type, replace all "this." access (including
>> implicit) into "thisParam." constructs.
>>
>> Method calls of "instance.Method(param)" are converted to
>> "SourceType.Method(instance, param)".
>>
>> Now we have static method and can move it to whatever type we need.
>> Remember, all things are temporary public.
>>
>> 3. Move method to designated class. Method calls of
>> "SourceType.Method(instance, param)" are converted to
>> "DestType.Method(instance, param)".
>>
>> 4. If one of the method parameters has destination type, optionally
>> perform reverse action to #2 - convert parameter into implicit "this"
>> and change body accordingly. Calls are converted obviously:
>> "param.Method(instance)"
>>
>> 5. Fix any access issues.
>>
>> Probably, target type should be limited to types in parameters for
>> instance methods. Or may be not - then #4 is skipped.
>>
>> What do you think?
>>


0
Comment actions Permalink

Hello Ilya,

as mentioned at http://www.jetbrains.net/confluence/display/ReSharper/ReSharper2.0Plan
we're going to support all the three refactorings comprising the chain needed
to
get 'Move Instance Method' functionality. There is no plan to support move
instance
method as a single operation though.

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

Hello Dmitry,

As far as I see, this is not on the plan for Resharper 2.0. Are you
going to do method moving in 2.0?

DS>> Hello Ilya,
DS>>
DS>> as far as I know, this refactoring (that moves a method to one of
DS>> its parameters'
DS>>
DS>> type) has been implemented in IntelliJ IDEA.
DS>> http://www.jetbrains.com/idea/features/refactoring.html
DS>> seems to not mention it explicitly, but I think it can be expressed
DS>> as a combination of 'Move static member' and 'Convert to Instance
DS>> Method'.
DS>>
DS>> Regards,
DS>> Dmitry Shaporenkov
DS>> JetBrains, Inc
DS>> http://www.jetbrains.com
DS>> "Develop with pleasure!"
>>> There were a number of discussions around moving method between
>>> types. I've got the following idea, please discuss.
>>>
>>> Method's 'working set' consists of parameters and type instance
>>> (this). Actually, it consists of publically visible static members
>>> of types, but this won't change during move, so we ignore this.
>>> Local variables stay inplace, so we ignore them too. So, moving
>>> method is "changing method enclosing type while maintaining its
>>> working set".
>>>
>>> 1. Temporary forget about access - consider all members publis (for
>>> simplicity). After move is complete we can fix access as required or
>>> leave it 'red' for user to take care.
>>>
>>> 2. Convert method to static by introducing explicit parameter
>>> 'thisParam' of enclosing type, replace all "this." access (including
>>> implicit) into "thisParam." constructs.
>>>
>>> Method calls of "instance.Method(param)" are converted to
>>> "SourceType.Method(instance, param)".
>>>
>>> Now we have static method and can move it to whatever type we need.
>>> Remember, all things are temporary public.
>>>
>>> 3. Move method to designated class. Method calls of
>>> "SourceType.Method(instance, param)" are converted to
>>> "DestType.Method(instance, param)".
>>>
>>> 4. If one of the method parameters has destination type, optionally
>>> perform reverse action to #2 - convert parameter into implicit
>>> "this" and change body accordingly. Calls are converted obviously:
>>> "param.Method(instance)"
>>>
>>> 5. Fix any access issues.
>>>
>>> Probably, target type should be limited to types in parameters for
>>> instance methods. Or may be not - then #4 is skipped.
>>>
>>> What do you think?
>>>

Sincerely,
Ilya Ryzhenkov



0
Comment actions Permalink

Hello Dmitry,

Nice, thank you. Waiting for EAP.

DS> Hello Ilya,
DS>
DS> as mentioned at
DS> http://www.jetbrains.net/confluence/display/ReSharper/ReSharper2.0
DS> Plan
DS> we're going to support all the three refactorings comprising the
DS> chain needed
DS> to
DS> get 'Move Instance Method' functionality. There is no plan to
DS> support move
DS> instance
DS> method as a single operation though.
DS> Regards,
DS> Dmitry Shaporenkov
DS> JetBrains, Inc
DS> http://www.jetbrains.com
DS> "Develop with pleasure!"
Sincerely,
Ilya Ryzhenkov


0

Please sign in to leave a comment.