Conditional assignments (?then:else)

 

Hello,

I discover (watching on some EA/Indicators) that there is a conditional assignment command that is not written in documentation (or maybe I've not found that). Very probably most skilled people know it and use it everyday.

int a = 1;
string Test = a>=0 ? "Positive" : "Negative";

I understood that this method consists in assign to Test string the value "Positive" in case of condition (a>=0) is true, otherwise string will be set to "Negative".

I'm starting to use this method in a lot of projects because it's good and can save a lot of code rows and time, but I want to understand:

  • Is it a deprecated method that will be removed in next compiler versions?
  • Is it safe to use?
  • Does it also save execution time or its only pro is to save code rows?

Thanks!

 
https://www.mql5.com/en/docs/basis/operators/ternary
 
Soewono Effendi:
https://www.mql5.com/en/docs/basis/operators/ternary

Thanks!! I never found that! Probably I searched it with wrong keywords.

 
I'm starting to use this method in a lot of projects because it's good and can save a lot of code rows and time

It can save code rows, surely.

But often this is at the expense of readability.

Therefore, I use the ternary construct only when it's unmistakably obvious what the code is doing.

I don't mind a few extra code rows if, 2-years later, I don't have to mentally decipher what the code is doing.

 

I have always known about it but i never use it for the same reasons @Anthony Garot mentioned.

In fact i will add many more lines like comments and blocks to isolate or divide code into sections to keep track of everything.

The code exists in two places, in the compiler, but also in my head and these two need to be 'synchronized' and if that fails i abandon the project and start over to make a better one.

Anthony Garot
Anthony Garot
  • www.mql5.com
Added topic Jamming a very large long into a double ChartID() returns a long. In general, it seems to be a rather large number. When I put it into a double, e.g. into a Global Variable of the Client Terminal, it gets changed due to loss of precision. So . . . I was thinking . . . I don't really need Added topic Determine if three doubles have...
 
Anthony Garot:

It can save code rows, surely.

But often this is at the expense of readability.

Therefore, I use the ternary construct only when it's unmistakably obvious what the code is doing.

I don't mind a few extra code rows if, 2-years later, I don't have to mentally decipher what the code is doing.

About readability, it all depends how you use it and when. Using the ternary operator, if done properly, can be more readable then a cascade of "if" (well that remains subjective of course).

Original from MQ Standard Library.

//+------------------------------------------------------------------+
//| Original : Using if/else                                         |
//+------------------------------------------------------------------+
int Compare(const short x,const short y)
  {
   if(x>y)
      return(1);
   else if(x<y)
      return(-1);
   else
      return(0);
  }
//+------------------------------------------------------------------+
//| Using ternary operator                                           |
//+------------------------------------------------------------------+
int Compare(const short x,const short y)
  {
   return x>y ?  1
        : x<y ? -1
        : 0;
  }
At the reverse if used in excess it can lead to unreadable code (example from the Codebase) :
      Res = (!FillingMode || (Type >= ORDER_FILLING_RETURN) || ((FillingMode & (Type + 1)) != Type + 1)) ?
            (((ExeMode == SYMBOL_TRADE_EXECUTION_EXCHANGE) || (ExeMode == SYMBOL_TRADE_EXECUTION_INSTANT)) ?
             ORDER_FILLING_RETURN : ((FillingMode == SYMBOL_FILLING_IOC) ? ORDER_FILLING_IOC : ORDER_FILLING_FOK)) :
            (ENUM_ORDER_TYPE_FILLING)Type;

What I mean is, as with any "tool", by itself there is nothing good or bad, but only the usage can be good or bad.

Nested Ternaries are Great
Nested Ternaries are Great
  • Eric Elliott
  • medium.com
Note: This is part of the “Composing Software” series on learning functional programming and compositional software techniques in JavaScript ES6+ from the ground up. Stay tuned. There’s a lot more of this to come! < Previous | < Conventional wisdom would have you believe that nested ternaries are unreadable, and should be avoided. The...
 
Alain Verleyen:

Excellent examples of both sides of the argument.


Marco vd Heijden:

In fact i will add many more lines like comments and blocks to isolate or divide code into sections to keep track of everything.

Well said. I agree.

 

Thanks to your experience, now I have clear ideas about how and why use it.

Thanks to all!

Reason: