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


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

Re: Comparison structure?

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: Comparison structure?
Date 2016-10-04 10:46 -0400
Organization A noiseless patient Spider
Message-ID <nt0fbh$qv2$1@dont-email.me> (permalink)
References <59c17fe5-61a6-46dc-918f-ee5aa348e3fd@googlegroups.com>

Show all headers | View raw


On 10/4/2016 10:25 AM, 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;
>                }
>           }

     You could rearrange it in many ways, but you're still stuck
with selecting among three distinct outcomes.  Therefore, at least
two tests are necessary.

     Some people frown on the ?: operator -- maybe they're just
grumpy by nature, I dunno -- but in situations of this kind I feel
it can reduce visual clutter and thereby improve readability.

	if (compareMe == compareTo) {
	    assignMe = 4;
	} else {
	    assignMe = compareMe + (compareMe > compareTo ? 2 : 0);
	}

Further condensation is certainly possible, but I'd likely not go there.

-- 
esosman@comcast-dot-net.invalid
The Tooth Fairy has gone high-tech, now pays in Bitecoin.

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


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