What are some good coding habits and practices to develop with regard to MQL programming?

 

I'll start...

I've been chasing a bug for hours and felt I'd share my new-found wisdom...

In case you aren't familiar with scope resolution - you might have run across some code where the function name is prepended with two colons and wondered why...

string symbol = ::Symbol();

This resolves function to the global namespace (where the built-in function is found) which is critical when working within a class; ESPECIALLY when you're inheriting from a parent class - because a lot of classes tend to redefine the built in MQL global functions.

eg.

class CPositionInfo ...
   string            Symbol(void) const;

Here is where an issue will develop if you aren't in the habit of using scope resolution. Let's say I make my own class that inherits from CPositionInfo

class MyPosition : public CPositionInfo
{
public:
   void Run() { Print("RUN");}
};

Now let's imagine for a moment that we forgot that our parent class has implemented its own version of an MQL function name, Symbol() for example, and somewhere in your class code you call to the API to get the chart symbol.

...
   string chartSymbol = Symbol();

Since your class has redefined the Symbol function you are no longer calling the function from the global namespace and could therefore get the wrong result. This is the correct way to call the global Symbol function

...
   string chartSymbol = ::Symbol();

Get in the habit of using the global scope resolution when calling global functions (especially for the Symbol and Period or learn the hard way, like I did. 

 
This should be used with caution, taking in mind that many classes may be designed to deliberately override system methods for purpose. For example, I used this technique for implementing on-the-fly optimization of EAs.