Quantcast
Channel: JetBrains Developer Community : Thread List - IntelliJ IDEA Users
Viewing all articles
Browse latest Browse all 5661

advanced refactoring: transform method to have single return

$
0
0

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;
    }

Viewing all articles
Browse latest Browse all 5661

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>