Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #31595
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Abuse of AssertionError? |
| Date | Tue, 21 Apr 2015 08:44:10 -0400 |
| Organization | A noiseless patient Spider |
| Lines | 51 |
| Message-ID | <mh5gl0$96t$1@dont-email.me> (permalink) |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=utf-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| Injection-Date | Tue, 21 Apr 2015 12:43:12 +0000 (UTC) |
| Injection-Info | mx02.eternal-september.org; posting-host="858a9d65d95f4ea4d355bd116aa2cdbf"; logging-data="9437"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX180mNd1S8nOWhW5Bj7MPwrK" |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.6.0 |
| Cancel-Lock | sha1:PmYNKvKAFKGDRXtrLppBEDyOjck= |
| Xref | csiph.com comp.lang.java.programmer:31595 |
Show key headers only | View raw
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?
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
Back to comp.lang.java.programmer | Previous | Next — Next in thread | Find similar | Unroll 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