Why you should not use Ternary Operators in C
Godbolt.org for seeing what your compiler actually does!

Why you should not use Ternary Operators in C

C Ternary Operators and are they better

This month's newsletter is short and sweet.

Just before you read on, my advice on Ternary Operators is

  1. Don't use them and
  2. Exclude them from your code by making your code standard exclude them.

This story below is to discourage you from using them!!! So here is the story!


Recently this was posted in a C for Embedded Programming?LinkedIn?group.

Attention C programmers!

Here’s a quick trick that can make your code more efficient and save you time. Did you know that you can use the ternary operator to simplify if-else statements?

For example, instead of writing:

if(x > 0){

y = 1;

}

else{

y = 0;

}

You can simplify it to:

y = (x > 0) ? 1 : 0;

This is particularly useful when you have multiple if-else statements within a loop. By using the ternary operator, you can make your code more readable and reduce the number of lines, leading to improved performance.

?

Give it a try and let me know what you think!?

No alt text provided for this image

Before you get all enthusiastic about this it is important to note that it is unlikely that the compiler will produce different or better machine executable code in the suggested case. It is really worth every software engineer spending time looking at the assembly code to see how little effect code shortcuts have. And this ternary construct is not allowed in some code standards. This paragraph was what I posted to the discussion.

The LinkedIn thread continued.

One person commented?

“A neat trick, although I doubt that better performance would be a benefit. One should beware of premature optimization and overusing this technique. Code readability is king!”

Another person added?

“In my opinion, the use of ternary operator is often used and abused in places where it actually makes the code less readable. Some coding rules actually prohibit the use of the ternary operator altogether.”

One other person said politely?

“Sorry to give you this information, but even without any optimization the compiler will produce the same assembly, you can try by yourself using one of the different tools online such as?https://godbolt.org/.”

It looks like godbolt.org might be a fun tool to allow software people to look at assembler for different compilers.

No alt text provided for this image
Godbolt output - see godbolt.org to check it out.


It is always good to look at the compiled output assembler code. It shows you what you have actually done.

?

Enrico Passoni

fw&sw&dsp engineer

1 年

the ternary operator is straightforward and I will continue to use it, as it permits to compress lines of code. Who cares about performance when you have arm mcus or multicore cpus? We are not anymore in the '80.

Hi Hamish There is no right and no wrong on this one, it comes down to style. I always push for the long hand so that it is readable and obvious. I am not too proud to keep it simple for the non academics to follow. It generally makes no difference to the compiled code, I don't need to prove I am clever, so my style is always long hand and easy/ obvious to read.

Arief Noor Rahman

PhD Power Electronic, HW/FW Generalist. Experienced with 2kW - 30kW design. Looking to grow power electronic RnD in Indonesia

1 年

Still never use it ever

要查看或添加评论,请登录

Hamish Laird的更多文章

社区洞察

其他会员也浏览了