Path: csiph.com!aioe.org!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Eric Sosman Newsgroups: comp.lang.java.programmer Subject: Re: Comparison structure? Date: Tue, 4 Oct 2016 10:46:20 -0400 Organization: A noiseless patient Spider Lines: 35 Message-ID: References: <59c17fe5-61a6-46dc-918f-ee5aa348e3fd@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 4 Oct 2016 14:46:09 -0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="22e5ab429c5a4bb738666b9c52e40d69"; logging-data="27618"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19bMXZK1rxf3KU57kL4vWv4" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.3.0 In-Reply-To: <59c17fe5-61a6-46dc-918f-ee5aa348e3fd@googlegroups.com> Cancel-Lock: sha1:eQoFcf9bDaq4phKNINvbhAeQ/sg= Xref: csiph.com comp.lang.java.programmer:35712 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.