Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #35715
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Martin Gregorie <martin@address-in-sig.invalid> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Comparison structure? |
| Date | Tue, 4 Oct 2016 17:37:23 -0000 (UTC) |
| Organization | A noiseless patient Spider |
| Lines | 38 |
| Message-ID | <nt0pcj$r9u$1@dont-email.me> (permalink) |
| References | <59c17fe5-61a6-46dc-918f-ee5aa348e3fd@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | 8bit |
| Injection-Date | Tue, 4 Oct 2016 17:37:23 -0000 (UTC) |
| Injection-Info | mx02.eternal-september.org; posting-host="e5160e2be48f1ea550cf61512446f525"; logging-data="27966"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19cQ+POE2BPLqFtK/BQtfGwNPHdNMy1m1I=" |
| User-Agent | Pan/0.140 (Chocolate Salty Balls; GIT 3663720 git://git.gnome.org/pan2) |
| Cancel-Lock | sha1:dlxThBXbJnLrbhKJRn+wGNBAaFI= |
| Xref | csiph.com comp.lang.java.programmer:35715 |
Show key headers only | View raw
On Tue, 04 Oct 2016 07:25:22 -0700, Eric Douglas wrote:
> I used some hard coded values to make a test block that compiles.
> Suffice to say these are dynamically assigned. The 3-way IF block
> sounds messy. Is there a better structure?
>
> int compareMe = 1;
> int compareTo = 2;
> final int assignMe;
> if (compareMe == compareTo) {
> assignMe = 4;
> } else {
> if (compareMe > compareTo) {
> assignMe = compareMe + 2;
> } else {
> assignMe = compareMe;
> }
> }
>
Three ways is always a bit of a toss-up because it looks messy any way
you implement it.
Personally, I think a switch is more readable: with four or more branches
its the way to go but I might also use it for a three way branch,
especially if each branch is only a few lines of inline code.
I'd only ever assign constants to assignMe, preferably defining them as
an enum list or a set of named integer constants. Using a mix of
constants and calculated values is bound to trip a maintenance programmer
at some point.
You could also use an enum with class bodies.
--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Comparison structure? Eric Douglas <e.d.programmer@gmail.com> - 2016-10-04 07:25 -0700
Re: Comparison structure? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-10-04 10:46 -0400
Re: Comparison structure? Roedy Green <inquiry@mindprod.com> - 2016-10-04 09:53 -0700
Re: Comparison structure? Eric Douglas <e.d.programmer@gmail.com> - 2016-10-04 10:29 -0700
Re: Comparison structure? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-10-04 15:35 -0400
Re: Comparison structure? Arne Vajhøj <arne@vajhoej.dk> - 2016-10-04 20:16 -0400
Re: Comparison structure? Arne Vajhøj <arne@vajhoej.dk> - 2016-10-04 22:29 -0400
Re: Comparison structure? Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-10-05 08:08 -0400
Re: Comparison structure? Arne Vajhøj <arne@vajhoej.dk> - 2016-10-05 20:27 -0400
Re: Comparison structure? Martin Gregorie <martin@address-in-sig.invalid> - 2016-10-04 17:37 +0000
Re: Comparison structure? Eric Douglas <e.d.programmer@gmail.com> - 2016-10-04 12:02 -0700
Re: Comparison structure? j4n bur53 <janburse@fastmail.fm> - 2016-10-05 17:12 +0200
Re: Comparison structure? j4n bur53 <janburse@fastmail.fm> - 2016-10-05 17:14 +0200
Re: Comparison structure? Arne Vajhøj <arne@vajhoej.dk> - 2016-10-04 20:03 -0400
Re: Comparison structure? Marcel Mueller <news.5.maazl@spamgourmet.org> - 2016-10-05 22:41 +0200
csiph-web