Road cases 2

From reader Frank comes this delightful Java-flavored head-scratcher:

This is from the main request-handling method for a webservice. Counting all of its interesting features is left as an exercise for the reader, but the request data is never modified and the error handling in both catch blocks is the same .... The kicker is that doCalculation() has all its logic inside try {} catch (Exception ex) anyway.


Response response = new Response();
try {
    if (request.includeFooFlag()) {
        response = doCalculation(request);
    }
    if (! request.includeFooFlag()) {
        long fooCount = getCountOfFoo();
        if (fooCount == 0) {
            response = doCalculation(request);
        } else {
            try {
                response = doCalculation(request);
            } catch (Exception ex) {
                response = createErrorResponse("Error details");
                return response;
            }
        }
    }
    return response
} catch (Exception ex) {
    response = createErrorResponse("Error details");
    return response;
}
return response;

[Advertisement] Manage IT infrastructure as code across all environments with Puppet. Puppet Enterprise now offers more control and insight, with role-based access control, activity logging and all-new Puppet Apps. Start your free trial today!


from The Daily WTF

0 Comments