advanced refactoring: transform method to have single return
I have some truly bad code that has returns all over the place making tracing through a nightmare. I would like some way of transforming a method so that it has exactly one point of return. I'm not sure how exceptions should be handled (but I think they could be left as they are).
Consider the following method:
public int blah() {
if (System.currentTimeMillis() > 12L) {
return 4;
}
return 7;
}
Is there a refactoring to transform this to:
public int blah() {
int ret;
if (System.currentTimeMillis() > 12L) {
ret = 4
} else {
ret = 7
}
return ret;
}
int ret;
if (System.currentTimeMillis() > 12L) {
ret = 4
} else {
ret = 7
}
return ret;
}
请先登录再写评论。
Hi Gregory,

There is an existing intention for the simple case you provided:
However, there is no available generic facility to minimize exit points number
Denis
Sorry, the example I posted was too simple but you got my gist.
Do you think it's possible in general to do such a transformation? Or, if not possible for certain structures, tell the user can't be done as written?
This would be a fantastic feature when working with existing codebases or doing security analysis.
+1 for this feature