Ludvig W
Ludvig W

Reputation: 824

Throw exception or return a value

My question is pretty straightforward. I have a class called Triangle. This class has a method;

    public boolean isAnyAngleGreaterThan(double deg);

My question is the following:

Should i throw illegalArgumentException if 'deg' is not inside the range 0 < 'deg' < 180 or should i simply return TRUE if deg is <= 0, and FALSE if deg is >= 180?

Or which one is the most preferable?

1

public boolean isAnyAngleGreaterThan(double deg) {
    if (Double.compare(0, deg) >= 0) {
        throw new IllegalArgumentException("Angle (deg) can't be negative.");
    } else if (Double.compare(180, deg) <= 0) {
        throw new IllegalArgumentException("Angle (deg) can't be greater or equal than 180.");
    }

    return Double.compare(aDeg, deg) > 0
            || Double.compare(bDeg, deg) > 0
            || Double.compare(cDeg, deg) > 0;
}

2

public boolean isAnyAngleGreaterThan(double deg) {
    return Double.compare(aDeg, deg) > 0
            || Double.compare(bDeg, deg) > 0
            || Double.compare(cDeg, deg) > 0;
}

where 'aDeg', 'bDeg' and 'cDeg' are the angles a, b and c in the triangle.

Upvotes: 0

Views: 4197

Answers (4)

OldCurmudgeon
OldCurmudgeon

Reputation: 65793

I presume from the name of the object this object is intended to represent a triangle. As such it will reside in an area of your code generally called the model which usually contains the state of the system.

I would therefore almost never perform validity checks here. Parameter validation should be done at the shallowest level possible, i.e. as close to the location where the values are entered/read as possible. This is the Fail Fast paradigm.

This is a core object. It should not be responsible for parameter checking any more than it should be responsible for security of access to the result or encryption of the result it returns.

The only time you should validate parameters in model code is when some parameters might result in other problems. In your case as all you are comparing with is other double values then there should not be any problems.

If, for example, some calculation is to be performed that may result in an exception (such as a divide by zero) or some other bad effect then in that case it would be correct to Fail Fast and check for zero values and throw an exception.

Upvotes: 2

alirabiee
alirabiee

Reputation: 1306

You might throw IllegalArgumentException if the degree is less than or equal to 0 and return false if it is greater than 180.

The reason for the former is that angles in a polygon is always positive in the mathematical sense. And the reason for the latter is that a polygon could possibly have an angle greater than 180, not in a triangle of course, but in a general mathematical sense it is valid to have an angle greater than 180 degrees.

Upvotes: 0

Alex Naish
Alex Naish

Reputation: 120

Personally, your function implies a basic check - does the triangle have any angles greater than the parameter. This doesn't state that theres restrictions on the angle so should therefore only return a true / false value. However, if this function is called with a user defined parameter, then it might make validation / error feedback easier to return an exception indicating the parameter is invalid.

This is really a question of your semantics / your personal preferences. It really depends on what you want your function to do.

Upvotes: 0

Timothy Truckle
Timothy Truckle

Reputation: 15622

The general rule is: Do not use Exceptions as flow control.

Therefore:

  • If the result of that method leads to something else then trowing an Exception later then you should return the boolean.

  • If the result of this method is used to find out if an Exception needs to be thrown then the method could throw the exception itself (and should be void).

Upvotes: 0

Related Questions