https://www.mql5.com/en/docs/basis/operators/ternary
Thanks!! I never found that! Probably I searched it with wrong keywords.
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.
- www.mql5.com
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.
- Eric Elliott
- medium.com
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!
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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.
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:
Thanks!