Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #31596

Re: Abuse of AssertionError?

From Jerry Stuckle <jstucklex@attglobal.net>
Newsgroups comp.lang.java.programmer
Subject Re: Abuse of AssertionError?
Date 2015-04-21 10:21 -0400
Organization A noiseless patient Spider
Message-ID <mh5mbn$vju$1@dont-email.me> (permalink)
References <mh5gl0$96t$1@dont-email.me>

Show all headers | View raw


On 4/21/2015 8:44 AM, Eric Sosman wrote:
> 
>     A recent thread on `assert' prompts me to ask for opinions
> about something I've been doing that may not be entirely kosher.
> Now and then I write a method that computes and returns a value,
> but whose computation relies on a data structure built elsewhere.
> If that data structure is messed up the computation may go off
> the rails, and I'd like to detect that.
> 
>     For example, here's a (paraphrase of a) method that finds the
> marginal income tax bracket for a stated income level:
> 
>     private static double marginalTaxRate(double income) {
>         income = Math.max(income, 0);
>         for (int i = BRACKETS.length; --i >= 0; ) {
>             if (income >= BRACKETS[i].floor) {
>                 return BRACKETS[i].rate;
>             }
>         }
>         // What goes here?
>     }
> 
> If everything has been set up correctly, the BRACKETS are arranged
> in increasing order by their income floors and the floor of the
> zero'th BRACKET is zero, so the `if' will eventually be satisfied
> and the method will return a value.  But if something's amiss the
> loop will run to completion -- and then the compiler will complain
> about falling off the end of a value-returning method.
> 
>     My solution has been to write
> 
>     throw new AssertionError("BUG!");
> 
> where the comment stands, and I'd like others to comment on whether
> this is an acceptable practice, or suggest a better approach.
> 
>     Red herrings that are of little interest:
> 
>     - Using `double' for monetary calculations.  I solemnly swear that
>       in the context from which this example was adapted, `double' is
>       an entirely suitable choice.  Sue me.
> 
>     - Rewriting the loop to remove the `i >= 0' test would silence
>       the compiler's complaint.  But then if the BRACKETS were messed
>       up I'd eventually get ArrayIndexOutOfBoundsException instead --
>       even less informative than AssertionError, IMHO.
> 
>     Thoughts?  Ideas?
> 

This is one way to do it, and I wouldn't say it's wrong.  But it doesn't
catch the error (a bad array) where it occurs; the code here may be a
long way from the code that created the array.

I would make a class Brackets.  In the constructor I would ensure the
array is created properly, then add a member such as

public double getRate(double income);

Then just call it with

  double rate = bracket.getRate(income);

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Abuse of AssertionError? Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-04-21 08:44 -0400
  Re: Abuse of AssertionError? Jerry Stuckle <jstucklex@attglobal.net> - 2015-04-21 10:21 -0400
    Re: Abuse of AssertionError? Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-04-21 11:21 -0400
      Re: Abuse of AssertionError? Jerry Stuckle <jstucklex@attglobal.net> - 2015-04-21 11:31 -0400
  Re: Abuse of AssertionError? j4n bur53 <janburse@fastmail.fm> - 2015-04-21 16:59 +0200
    Re: Abuse of AssertionError? Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-04-21 11:26 -0400
  Re: Abuse of AssertionError? Thomas Richter <thor@math.tu-berlin.de> - 2015-04-21 17:05 +0200

csiph-web