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

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

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?

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:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as