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

Java - How to represent the result of an operation

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

Problem

Introduction

In my Java application, I need to represent the result of an operation. This can either be OK or ERROR + an error message.

Background

I have a class to verify the integrity of a directory (e.g. to check that a few conditions hold). If it finds out the directory is corrupted, it should return a description of the error, such as:

  • Unexpected file - [filename].



  • Unexpected number of files - expected [X], actual [Y].



The callers of the class don't know anything about the directory and the conditions. They are only interested to know if the directory is OK or not, and in that case, to get an appropriate error message.

The Result class was intended as a way to transmit the results of the verification.

I chose not to throw exceptions as (to me) that seemed more convenient for reporting errors in the verification process. It seemed inappropriate to intermix those two situations (e.g. the directory was found invalid vs. an error occurred while verifying the directory).

The current solution

I have so far been able to come up with the following class.

public class Result
{
    private static enum State { OK, ERROR };

    private State state;
    private String errorMessage;

    private Result(State state, String errorMessage)
    {
        this.state = state;
        this.errorMessage = errorMessage;
    }

    public static Result ok()
    {
        return new Result(State.OK, null);
    }

    public static Result error(String errorMessage)
    {
        return new Result(State.ERROR, errorMessage);
    }

    public boolean isOK()
    {
        return (this.state == State.OK);
    }

    public String getErrorMessage()
    {
        return errorMessage;
    }
}


The client code then looks like this:

Result result = Result.ok();
Result result = Result.error("Could not ...");

if (result.isOK())
    ...
else
{
    String errorMessage = result.getErrorMessage();
    ...
}


This has the benefit that when creating a result, the clie

Solution

If you don't want to use exceptions you should know that in OOP there is a lot of ways to implement the fact that some object belongs to some specific class. It can be either some enum that enumerates all possible domains or in case of OOP languages it can be class.

So in case of Java (again if you don't want exceptions by some reason) you can use:

class Result
{
    public static final Result ok = new Result(); // I'm not sure this is correct in terms of Java
}

class Failure extends Result
{
    public final String errorMessage;
    public Faliure(String message)
    { this.errorMessage = message; }
}

if (result instanceof Failure)
{
    // do something
}

Code Snippets

class Result
{
    public static final Result ok = new Result(); // I'm not sure this is correct in terms of Java
}

class Failure extends Result
{
    public final String errorMessage;
    public Faliure(String message)
    { this.errorMessage = message; }
}

if (result instanceof Failure)
{
    // do something
}

Context

StackExchange Code Review Q#20285, answer score: 3

Revisions (0)

No revisions yet.