[Insight-developers] The Evil Ternary operator.

Frank Miller frankmiller at jhmi.edu
Mon Aug 30 11:43:27 EDT 2010


I feel compelled to express some support for the ternary operator.
Consider the following

  const int step = stepBackward ? -1 : 1;

The "three lines of code and four brackets" equivalent would be

  int step = 1;
  if ( stepBackward )
    step = -1;

It is not possible to make step const this way. Because of this I would
argue that the former is easier to maintain because the reader knows
that the value of step will never change. The less context the reader
has to remember the easier the code will be to understand.

Just wanted to throw that out there.

Frank

On Sun, 2010-08-29 at 11:10 -0400, David Cole wrote:
> On Sat, Aug 28, 2010 at 4:27 PM, Luis Ibanez <luis.ibanez at kitware.com>
> wrote:
>                 
>                 As for the style of a trinary operator vs seven lines
>                 of code, I just found seven to be too many.
>                 
>                 
>                 
>         ---------
>         
>         Well, 
>         they are three lines of code and four brackets.      :-)
>         
>         The real difference comes when you run this in a debugger,
>         and when a new reader of the code has to come and figure
>         out what the code is doing. 
>         
>         Human readers will mentally translate that ternary operator 
>         into an "if", and it will simply increase the cost of
>         maintenance 
>         of the code (most of the time of debugging code is spent 
>         reading code, trying to figure out what it was supposed to
>         do).
>         
>         
>         It is a lot better to have three lines of code and four
>         brackets
>         than having a single line of code with a ternary operator that
>         is not easily debuggable nor maintainable.
>         
>         
>         At the end, 
>         the code is intended for human readers.
>         
>         
>             Luis
>         
>          
>         
>         
>         _______________________________________________
>         Powered by www.kitware.com
>         
>         Visit other Kitware open-source projects at
>         http://www.kitware.com/opensource/opensource.html
>         
>         Kitware offers ITK Training Courses, for more information
>         visit:
>         http://kitware.com/products/protraining.html
>         
>         Please keep messages on-topic and check the ITK FAQ at:
>         http://www.itk.org/Wiki/ITK_FAQ
>         
>         Follow this link to subscribe/unsubscribe:
>         http://www.itk.org/mailman/listinfo/insight-developers
>         
> 
> 
> 
> Definitely. This issue is most definitely *NOT* a style issue. It is
> absolutely a code maintainability issue. Always use if/else and
> multiple lines in a source file for ease of debugging.
> 
> 
> David
> 
> 
> _______________________________________________
> Powered by www.kitware.com
> 
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
> 
> Kitware offers ITK Training Courses, for more information visit:
> http://kitware.com/products/protraining.html
> 
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
> 
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-developers




More information about the Insight-developers mailing list