[Insight-developers] The Evil Ternary operator.

David Cole david.cole at kitware.com
Sun Aug 29 11:10:02 EDT 2010


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/mailman/private/insight-developers/attachments/20100829/75c4e10a/attachment.htm>


More information about the Insight-developers mailing list