include files use each other

 
I'm new to MT4.

I have 2 include files (file_1.mqh & file_2.mqh). They are both general purpose functions, but for organizational purposes I would like to keep them in 2 files. Each file uses a function in the other, so I always get an error saying "some_function() doesn't exist" when I try to compile one of them.

To get around this I put "#include <file_1.mqh>" in file_2 and "#include <file_2.mqh>" in file_1. I get a warning that the file has already been processed when I compile each of them, but no error. Is there any reason that I should not do this?

Or is there a better solution?
 

It is best to avoid circular dependency in software design, because it is confusing and can cause runtime problems (unlikely in MQL4, I think). For the first reason, grouping the functions differently might be better.

 
Elroch:

It is best to avoid circular dependency in software design, because it is confusing and can cause runtime problems (unlikely in MQL4, I think). For the first reason, grouping the functions differently might be better.

I agree that would be better if possible. I'm still dealing with the differences between MT4 and other programming languages. I have trouble accepting the fact that I can't do things in MT4 that are standard techniques in other languages.

In the past I have always been able to find a way to get something done that I want to do, but I keep running up against things in MT4 that people tell can't be done, but I'm going to keep asking my questions. I frequently discover that someone else has found a way to get it done.

 

It is possible to avoid cyclic dependency of files if you have no cyclic dependence among the functions themselves. Even if they do, you can avoid it by ensuring any cyclically dependent functions are in the same file.

eg suppose functions A1, A2 are in include file A and B1, B2 are in file B, with A1 using B1 and B2 using A2. You can regroup the functions by dependence, with B1 and A2 in a file for functions that don't rely on any includes and A1 and B2 in a second file that includes the first one.

However, if function A1 depends on function B1 and B1 depends on A1, you obviously can't get rid of the cyclic include without having both of these functions in the same file. And if you do, this situation is prone to cause problems.

In fact, contrary to my earlier statement, you can have the most nonsensical code that just gets warnings at compile time and is silent at run time.

Eg I wrote two include files. The first include file has this code:

#include <test2.mqh>

bool test1()
{
return(test2());

}

and the second include file has this code:

#include <test1.mqh>

bool test2()
{
return(test1());

}

Pretty silly, huh? And finally an expert has this code:

#include <test1.mqh>

int start()

{
Alert(test1());
Alert("this does not display");
return(0);

}

So you have two functions that use each other in their definitions (in a very silly way that never terminates in theory), and an expert that uses one of the functions.

If you execute this expert it does nothing visible. What happens is that the start() code gets called on a tick, but never displays the first alert because it can't calculate test1() and never gets to the second Alert.

Of course if you remove the line Alert(test1()); from the expert, the message displays.

There is nothing wrong with two or more functions calling each other in a cyclic fashion in some cases (it's no worse than recursion, where one function calls itself). It just means there is one more way for code to not terminate if it's written badly (this can be a silent error in MT4). But in my opinion, if functions are inseparably interdependent, they should go in the same include file.

 
Elroch:

It is possible to avoid cyclic dependency of files if you have no cyclic dependence among the functions themselves. Even if they do, you can avoid it by ensuring any cyclically dependent functions are in the same file.

eg suppose functions A1, A2 are in include file A and B1, B2 are in file B, with A1 using B1 and B2 using A2. You can regroup the functions by dependence, with B1 and A2 in a file for functions that don't rely on any includes and A1 and B2 in a second file that includes the first one.

However, if function A1 depends on function B1 and B1 depends on A1, you obviously can't get rid of the cyclic include without having both of these functions in the same file. And if you do, this situation is prone to cause problems.

In fact, contrary to my earlier statement, you can have the most nonsensical code that just gets warnings at compile time and is silent at run time.

Eg I wrote two include files. The first include file has this code:

#include <test2.mqh>

bool test1()
{
return(test2());

}

and the second include file has this code:

#include <test1.mqh>

bool test2()
{
return(test1());

}

Pretty silly, huh? And finally an expert has this code:

#include <test1.mqh>

int start()

{
Alert(test1());
Alert("this does not display");
return(0);

}

So you have two functions that use each other in their definitions (in a very silly way that never terminates in theory), and an expert that uses one of the functions.

If you execute this expert it does nothing visible. What happens is that the start() code gets called on a tick, but never displays the first alert because it can't calculate test1() and never gets to the second Alert.

Of course if you remove the line Alert(test1()); from the expert, the message displays.

There is nothing wrong with two or more functions calling each other in a cyclic fashion in some cases (it's no worse than recursion, where one function calls itself). It just means there is one more way for code to not terminate if it's written badly (this can be a silent error in MT4). But in my opinion, if functions are inseparably interdependent, they should go in the same include file.




HI Elroch;

I don't have 2 functions that are dependent on each other. I just have a function in a.mqh that depends on a function in b.mqh, and a function in b.mqh that depends on a function in a.mqh.

But I want to keep the functions in the include files they are in. It helps me keep things straight in my head. Eventually when I'm positive that they are completely error free, I may put them all in the same file, but I'm just learning MT4 and it helps to keep them separate for now. I'm having a little trouble adapting to MT4 from other languages. I'm still thinking in VBA, so it helps to organize things as close to VBA as I can.

I'm hoping that it doesn't make any difference in the script or indicator when I get errors during compiling the include files but no errors when I compile the script file.

 
FoxGuy:

HI Elroch;

I don't have 2 functions that are dependent on each other. I just have a function in a.mqh that depends on a function in b.mqh, and a function in b.mqh that depends on a function in a.mqh.

But I want to keep the functions in the include files they are in. It helps me keep things straight in my head. Eventually when I'm positive that they are completely error free, I may put them all in the same file, but I'm just learning MT4 and it helps to keep them separate for now. I'm having a little trouble adapting to MT4 from other languages. I'm still thinking in VBA, so it helps to organize things as close to VBA as I can.

I'm hoping that it doesn't make any difference in the script or indicator when I get errors during compiling the include files but no errors when I compile the script file.


A warning is not an error. I think you have no problem.

Reason: