HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Is using return appropriate here?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
returnhereusingappropriate

Problem

I wanted to get the boolean status from a function. Here is my sample code, wherein I used return in try and catch, to get the status of function.

public static void main(String[] args) {
        System.out.println(getState() ? "successful" : "failed");
}

public static boolean getState() {
        SAXBuilder builder = new SAXBuilder();
        File xmlFile = new File("tile.xml");
        Set set = new TreeSet();
        try {
            Document document = (Document) builder.build(xmlFile);
            Element rootNode = document.getRootElement();
            System.out.println(rootNode);
            return true;
        } catch (IOException io) {
            System.out.println(io.getMessage());
            return false;
        } catch (JDOMException jdomex) {
            System.out.println(jdomex.getMessage());
            return false;
        }
    }


Does it makes sense ? Can I improve it ?
Assuming, that there might be more than 10 catch blocks, then should I use 10 return statements, in each catch block.

Solution

For only 2 or 3 instances, I may use multiple return statements, but only in the case of if-else chains or switches. Returns within a try catch block can be done, but also can become complicated when you start mixing in finally with it. In the case of try-catch blocks or several possible 'return' paths, I use a variable and set it as necessary, and return it at the end. If you want an early return to bypass some code, I tend to take that code and wrap it up in a separate method, and use an if statement with my value to return to determine whether to execute that method.

public static void main(String[] args) {
        System.out.println(getState() ? "successful" : "failed");
}

public static boolean getState() {
        boolean success = false;
        SAXBuilder builder = new SAXBuilder();
        File xmlFile = new File("tile.xml");
        Set set = new TreeSet();
        try {
            Document document = (Document) builder.build(xmlFile);
            Element rootNode = document.getRootElement();
            System.out.println(rootNode);
            success = true;
        } catch (IOException io) {
            System.out.println(io.getMessage());
        } catch (JDOMException jdomex) {
            System.out.println(jdomex.getMessage());
        }

        return success;
}

Code Snippets

public static void main(String[] args) {
        System.out.println(getState() ? "successful" : "failed");
}

public static boolean getState() {
        boolean success = false;
        SAXBuilder builder = new SAXBuilder();
        File xmlFile = new File("tile.xml");
        Set<String> set = new TreeSet<String>();
        try {
            Document document = (Document) builder.build(xmlFile);
            Element rootNode = document.getRootElement();
            System.out.println(rootNode);
            success = true;
        } catch (IOException io) {
            System.out.println(io.getMessage());
        } catch (JDOMException jdomex) {
            System.out.println(jdomex.getMessage());
        }

        return success;
}

Context

StackExchange Code Review Q#19264, answer score: 6

Revisions (0)

No revisions yet.