Using the ?: operator as opposed to if…else…

Question:
Is there a problem with using the ?: operator instead of the if…else… construct? For example, see the following:

if(iNum1 > iNum2)    iNum3 = iNum1;else    iNum3 = iNum2;

The above requires 4 lines of code. Yes, it's readable, but it could be written with just one line of code, that in my opinion is just as readable, as in the following example?

iNum3 = (iNum1 > iNum2) ? iNum1 : iNum2;

In this instance, I would prefer to use the ?: operator. Is there a problem in doing this? I have heard the argument that it is difficult to spot, and that it can be mininterpreted. Also, because it produces identical executable code, it may be better to use just the if...else... construct. However, I disagree with these comments as the person who tried to get me to accept them was looking through code that uses the ?: operator and spotted the use of them instantly and knew exactly what they did. What do you think?

Answer:
As s rule, every language construct has a legitmate use (the only exception is deprecated features, which are listed in the Standard). The ? operator is therefore legit when used properly. The problem with ? is that many programmers tend to overuse or even misue it, just to save a few keystrokes, thereby creatinng indecipherable programs.

Now to you code snippet. There are three criteria to test the use of ?. First, is the code correct? In both cases, i.e., the if-else sequence or the ? shorthand, the code is correct. Next is readability. Usually, a sequence of if-else seems a little more readable but this is not always the case. In your code, the if-else sequence is rather long and therefore it may not be immedaitely clear what your intention was. In the ? version, the entire expression consists of one line of code so it's somewhat more intuitive. The third criterion is efficiency, but it's irrelevant in this case because you can see that the compiler expands the ? expression into an if-else sequence.

Thus, the ? has an advanatge of being a little bit more readable (in this case, but not necessarily in other cases). However, it's obvious that you're simply trying to extract the maximum value of a pair of numbers and assign it to another number. A much more readable choice, which is at least as efficient, and also shorter is this:

iNum3 = max(iNum1 > iNum2);

In general, you can almost without exception replace each ? expressions by either a max or min expression.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS