Extract Method: Create aggregate inner class for multiple returns

Would something like this be useful to others?

Often when I try to extract a method, it complains "you've got multiple
returns, can't do it sorry", which whilst highlighting I should refactor
the code to return only one item, isn't necessarily what I want to do at
this stage.

I was thinking about an addition to this refactoring, still complain about
multiple returns, but offer to cancel/abort as normal, but to also create
an inner class that contains the multiple returns.

Such that extracting:

// Do Something with which return two assignments
fooString = "foo";
barInt = 5;


would extract to say:

private class MyNewReturn {
private fooString;
private barInt;
public MyNewReturn( String fooString, int barInt ) {
this.fooString = fooString;
this.barInt = barInt;
}
public String getFooString() { return fooString; }
public int getBarInt() { return barInt; }
}

public MyNewReturn theExtractedMethod() {
fooString = "foo";
barInt = 5;
return new MyNewReturn(fooString, barInt);
}


Or something like this?

3 comments
Comment actions Permalink

I've always wanted this. I heard that the next change to the Java language
will be multiple return values. I think there's a request for this, because
I can't imagine I didn't search for one or file one if none existed when
I first wanted it, but I can't think of what it would be called.

Would something like this be useful to others?

Often when I try to extract a method, it complains "you've got
multiple returns, can't do it sorry", which whilst highlighting I
should refactor the code to return only one item, isn't necessarily
what I want to do at this stage.

I was thinking about an addition to this refactoring, still complain
about multiple returns, but offer to cancel/abort as normal, but to
also create an inner class that contains the multiple returns.

Such that extracting:

// Do Something with which return two assignments
fooString = "foo";
barInt = 5;
would extract to say:

private class MyNewReturn {
private fooString;
private barInt;
public MyNewReturn( String fooString, int barInt ) {
this.fooString = fooString;
this.barInt = barInt;
}
public String getFooString() { return fooString; }
public int getBarInt() { return barInt; }
}
public MyNewReturn theExtractedMethod() {
fooString = "foo";
barInt = 5;
return new MyNewReturn(fooString, barInt);
}
Or something like this?




0
Comment actions Permalink

This is a good idea (at least for private extracted methods).

Franklin.

Mark Derricutt wrote:

Would something like this be useful to others?

Often when I try to extract a method, it complains "you've got multiple
returns, can't do it sorry", which whilst highlighting I should refactor
the code to return only one item, isn't necessarily what I want to do at
this stage.

I was thinking about an addition to this refactoring, still complain about
multiple returns, but offer to cancel/abort as normal, but to also create
an inner class that contains the multiple returns.

Such that extracting:

// Do Something with which return two assignments
fooString = "foo";
barInt = 5;


would extract to say:

private class MyNewReturn {
private fooString;
private barInt;
public MyNewReturn( String fooString, int barInt ) {
this.fooString = fooString;
this.barInt = barInt;
}
public String getFooString() { return fooString; }
public int getBarInt() { return barInt; }
}

public MyNewReturn theExtractedMethod() {
fooString = "foo";
barInt = 5;
return new MyNewReturn(fooString, barInt);
}


Or something like this?

0
Comment actions Permalink

On Tue, 01 Mar 2005 at 19:11 GMT, Keith Lea wrote:

I've always wanted this. I heard that the next change to the Java language
will be multiple return values. I think there's a request for this, because
I can't imagine I didn't search for one or file one if none existed when
I first wanted it, but I can't think of what it would be called.


I entered this in JIRA as http://jetbrains.net/jira/browse/IDEA-723 -
watch and vote for if you wish :)

Mark

0

Please sign in to leave a comment.