[ajug-members] Ternary Operator - simplification or complication?
Brian Lee
brian_a_lee at hotmail.com
Thu Jun 1 14:25:01 EDT 2006
This is exactly why I don't like the ternary operator.
Which is cleaner to read/parse?
String action = (cond ? "Success" : ( (input == null ) ? "Edit" : "Confirm"
) );
or
String action = null;
if (cond){
action = "Success";
}else{
if (input == null){
action = "Edit";
}else{
action= "Confirm";
}
}
The ternary operator takes up fewer lines. But the expanded version has a
much less change of being misinterpreted and causing bugs. Which means less
time for me debugging peoples code after they misuse/misinterpret. So it's
cheaper not to use the ternary operator.
Of course, there are always uses for it. But I've seen it misused a lot more
than properly used.
BAL
>From: "Dan Marchant" <driedtoast at gmail.com>
>Reply-To: "General AJUG membership forum (100-200
>messages/month)"<ajug-members at ajug.org>
>To: "General AJUG membership forum (100-200
>messages/month)"<ajug-members at ajug.org>
>Subject: Re: [ajug-members] Ternary Operator - simplification or
>complication?
>Date: Thu, 1 Jun 2006 14:03:05 -0400
>MIME-Version: 1.0
>Received: from server01.ajug.org ([66.45.18.180]) by
>bay0-mc1-f11.bay0.hotmail.com with Microsoft SMTPSVC(6.0.3790.1830); Thu, 1
>Jun 2006 11:15:03 -0700
>Received: from server01.ajug.org (server01.ajug.org [127.0.0.1])by
>server01.ajug.org (8.12.11.20060308/8.12.11) with ESMTP id
>k51IjNjG030864;Thu, 1 Jun 2006 14:45:23 -0400
>Received: from nz-out-0102.google.com (nz-out-0102.google.com
>[64.233.162.203])by server01.ajug.org (8.12.11.20060308/8.12.11) with ESMTP
>idk51IjLS6030861for <ajug-members at ajug.org>; Thu, 1 Jun 2006 14:45:21 -0400
>Received: by nz-out-0102.google.com with SMTP id 9so545152nzofor
><ajug-members at ajug.org>; Thu, 01 Jun 2006 11:03:05 -0700 (PDT)
>Received: by 10.37.20.73 with SMTP id x73mr1359865nzi;Thu, 01 Jun 2006
>11:03:05 -0700 (PDT)
>Received: by 10.36.50.7 with HTTP; Thu, 1 Jun 2006 11:03:05 -0700 (PDT)
>X-Message-Info: LsUYwwHHNt2o+s7oLEvbxwbb83ytlm5lsMCja/Q7Tic=
>DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=beta;
>d=gmail.com;h=received:message-id:date:from:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references;b=epkxYVdObe3HGKCGMe6do8QGuatFjR7NxMl22rk1gPOhS0oh7SEQLYiiHj2hVLEiL+GY0kY/zptcRiTz4LxqCUKpK87uFrZBKvyIaFUg/te3sqyLOA+hBA3QaxevqvcDdYBcvCRJP2VNIC7vDJ8K5yAed9L4gdEixwrMu5yQs4o=
>References:
><3E475912BEFC1C47B9C2304DB682CC6F2A5F89 at BFEXCH1.benefitfocus.com>
>X-BeenThere: ajug-members at ajug.org
>X-Mailman-Version: 2.1.5
>Precedence: list
>List-Id: "General AJUG membership forum (100-200
>messages/month)"<ajug-members.ajug.org>
>List-Unsubscribe:
><http://www.ajug.org/mailman/listinfo/ajug-members>,<mailto:ajug-members-request at ajug.org?subject=unsubscribe>
>List-Archive: <http://www.ajug.org/pipermail/ajug-members>
>List-Post: <mailto:ajug-members at ajug.org>
>List-Help: <mailto:ajug-members-request at ajug.org?subject=help>
>List-Subscribe:
><http://www.ajug.org/mailman/listinfo/ajug-members>,<mailto:ajug-members-request at ajug.org?subject=subscribe>
>Errors-To: ajug-members-bounces at ajug.org
>Return-Path: ajug-members-bounces at ajug.org
>X-OriginalArrivalTime: 01 Jun 2006 18:15:04.0676 (UTC)
>FILETIME=[4E536A40:01C685A7]
>
>It has it's place.
>I use for simple conditioning when there is only 2 outcomes (you could
>get crazy had have chained ternaries for more outcomes).
>
>Example:
>
>String action = (cond ? "Success" : ( (input == null ) ? "Edit" : "Confirm"
>) );
>
>Nested ternary operators can be a bit confusing for some.
>
>I think people should take advantage of it more for certain logic.
>
>My 2 cents.
>
>- Dan
>
>
>
>On 6/1/06, Jonathan Komorek <jonathan.komorek at benefitfocus.com> wrote:
>>
>>
>>
>>I just opened up a can of worms within our development community by
>>stating
>>that in certain cases, I find that the Ternary Operator simplifies code.
>>I've been given the impression that I'm the last Java developer alive that
>>believes the ternary operator should ever be used. I'm interested in
>>hearing
>>the opinions of users on this group.
>>
>>Has the ternary operator helped you? Caused you great grief? Should it
>>even
>>be in the language?
>>
>>
>>Jonathan Komorek
>> Development Security Officer
>> Benefitfocus.com, Inc.
>> (843) 849-7476 ext. 254 (Main)
>> (843) 849-9485 (Fax)
>> E-mail: jonathan.komorek at benefitfocus.com
>>
>>
>>
>>***************************************************************************************
>>BENEFITFOCUS.COM CONFIDENTIALITY NOTICE: This electronic message is
>>intended
>>only for the individual or entity to which it is addressed and may contain
>>information that is confidential and protected by law. Unauthorized
>>review,
>>use, disclosure, or dissemination of this communication or its contents in
>>any way is prohibited and may be unlawful. If you are not the intended
>>recipient or a person responsible for delivering this message to an
>>intended
>>recipient, please notify the original sender immediately by e-mail or
>>telephone, return the original message to the original sender or to
>>bfpostmaster at benefitfocus.com, and destroy all copies or derivations of
>>the
>>original message. Thank you. (BFeComNote Rev. 08/01/2005)
>>***************************************************************************************
>>_______________________________________________
>>ajug-members mailing list
>>ajug-members at ajug.org
>>http://www.ajug.org/mailman/listinfo/ajug-members
>>
>>
>>
>_______________________________________________
>ajug-members mailing list
>ajug-members at ajug.org
>http://www.ajug.org/mailman/listinfo/ajug-members
More information about the ajug-members
mailing list