Features of the mql4 language, subtleties and techniques - page 36

 

I'm gonna contribute with something very basic, but since I see it over and over again in this forum I guess a lot of people might now know... and it really hurts my eyes.

Asking about a logical expression, function or variable is per se asking if that expression, function or variable is TRUE. You do NOT have to compare it to TRUE or FALSE, i.e.:

if(IsTradeAllowed() == true) // bad style
if(IsTradeAllowed())         // good style

if(isOKtoHedge == false)     // bad style
if(!isOKtoHedge)             // good style

Also, any numerical value equal to zero is considered from a logical point of view FALSE. Conversely, any value different from zero is considered logically TRUE. This allows you to do things like this in a nice and elegant way...:

int a = 2;
int b = 1;

if(a - b != 0) Print("They are different"); // Correct
if(a - b) Print("Yes, they are!");          // Correct and nicer, but it may compromise clarity (?)

I would argue, however, that sometimes you wanna do things kind of "redundant" from a syntax point of view for the sake of clarity, especially when you have thousands of lines, so that in a single look you understand better what the code is doing, i.e.,:

bool IsDifferent(int a, int b) {
        if(a == b)
                return(false);
        return(true); // if a is equal to b, then we're out of the function already, therefore this line is executed ONLY if a and b are NOT equal
}

bool IsDifferent(int a, int b) {
        if(a == b)
                return(false);
        else
                return(true); // this "else" is unnecessary (see function above), but adds clarity to the code
}

bool IsDifferent(int a, int b) {
        return!(a == b); // Or you can do this, of course since you've read my first example :/
}

Maybe I'm splitting hairs here, but I'm convinced that good coding style and habits are the sum of many tiny details.

 
Carlos Moreno Gonzalez #: I'm gonna contribute with something very basic,

I agree and usually post:

You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and “if long entry” is an incomplete sentence.

and
          Increase Order after stoploss - MQL4 programming forum #1.3 (2017)
 

I will just add something about function names that has helped me, at least.

Lets say you have these functions:

void countorders(){}
void getordermagicno(){}
void countordersinprofit(){}
void countobjects(){}
void getobjectname(){}

These function names are verb based: count, get. 

Instead, I would make the function names noun based: orders, objects. For example, not countobjects() but objectscount().

void orderscount(){}
void ordermagicnoget(){}
void ordersinprofitcount(){}
void objectscount(){}
void objectnameget(){}

That way, if you have dozens of functions you can use the handy drop down function locator in MQL4 editor to go straight to them.


Reason: