Questions on OOP (Object Oriented Programming) - page 11

 

I would like the opinion of more experienced programmers on some of the nuances. Take declarations of variables in classes. For example, I have variables in classes that are declared inside different methods of a certain class. They are declared in each method most often. Is it worth doing this, or is it better to declare them once in the private section and then use it?

I think it would be more logical to use counter variables, if they are used in loops, right? After all, the counter is explicitly set to a value at the moment of calculation, for example, if we use a loop:

for (i = OrdersTotal() - 1; i >= 0; i--)
{

}

In each method, it would not be reasonable to declare a variable i, because this is probably an unnecessary waste of memory on memory cells. In general, I'm interested in such things.

This also concerns not only loops but other options as well. There are enough different variables which have to be declared more than once, but they play the same role... The only thing I can see is that some of them will have to be zeroed in value, so that they don't contain "rubbish" from previous operations. Is it reasonable to do it or is it easier to redeclare such a variable... In general, I would like to hear about it from professionals.

 
hoz:

I would like the opinion of more experienced programmers on some of the nuances. Take declarations of variables in classes. For example, I have variables in classes that are declared inside different methods of a certain class. They are declared in each method most often. Is it worth doing this, or is it better to declare them once in the private section and then use it?

I think it would be more logical to use counter variables, if they are used in loops, right? After all, the counter is explicitly set to a value at the moment of calculation, for example, if we use a loop:

In each method, it would not be reasonable to declare a variable i, because this is probably an unnecessary waste of memory on memory cells. In general, I am interested in such things.

This also concerns not only loops but other options as well. There are enough different variables which have to be declared more than once, but they play the same role... The only thing I can see is that some of them will have to be zeroed in value, so that they don't contain "rubbish" from previous operations. Is it reasonable to do it or is it easier to redeclare such a variable... In general, I would like to hear about it from professionals.

Whether or not you should do so depends on the purpose of creating the variable. If an entity is encapsulated by a class, e.g. an order, then we will need variables defined in the class to express its properties, e.g. a ticket for the order. These variables will be shared by all methods of that class for the same object in that class.

If a variable is created to provide a loop within a method, it should be defined inside the method as automatic (i.e. without using the static keyword). In this case, even if one method calls another method inside its own loop, which also has a loop, and the variable with the same name is used, you won't have any problems. When using a variable declared in a class, the variable will be overwritten in the loop of the second method.

For automatic variables, memory is allocated automatically when the function is entered and automatically deleted when it is exited, with minimal overhead. Moreover, if the functions are simple, the compiler can generate code that does not allocate and free automatic memory at all.

Saving memory cells of this kind used to be a sensible thing to do some time ago, but it finally lost its meaning about 20 years ago...

 

There's a class. It has such a simple method, all unbranded to hell, because I was looking for where the bug is:

// 1.5 Определяем минимальную разрядность лота. ===========================================================================================
int BaseInfo::LotPrecision()
{
   static int li_LotPrecision = 0;     // устанавливаем минимальную разрядность лота в ноль
   Print ("LotPrecision()_Before: li_LotPrecision = ", li_LotPrecision);
   Print ("LotPrecision()_BeforeCircle: SLots.gd_LotStep = ", SLots.gd_LotStep);
   while (SLots.gd_LotStep < 1)
   {
      Print ("LotPrecision(): SLots.gd_LotStep < 1");
      SLots.gd_LotStep *= 10;
      Print ("SLots.gd_LotStep *= 10");
      Print ("LotPrecision(): SLots.gd_LotStep = ", SLots.gd_LotStep);
      li_LotPrecision++;
      Print ("LotPrecision(): li_LotPrecision = ", li_LotPrecision);
   }
   Print ("LotPrecision(): LOTPRES = ", li_LotPrecision);
   return (li_LotPrecision);
}

Variableli_LotPrecision used to be without static modifier, then I saw that it shouldn't lose value and changed it. It was noticed that even though I added the modifier, theli_LotPrecisionvariabledoes not retain its value.

I print it to OnInite():

void OnTick()
{
   int Lot = CBase.LotPrecision();
   Print ("Lot = ", Lot);
}

In the log:

17:26:03 2014.07.22 14:10  RSI EURUSD,M5: SLots.gd_LotStep = MarketInfo (_Symbol, MODE_LOTSTEP); = 0.01
17:26:03 2014.07.22 14:10  RSI EURUSD,M5: LotPrecision()_Before: li_LotPrecision = 0
17:26:03 2014.07.22 14:10  RSI EURUSD,M5: LotPrecision()_BeforeCircle: SLots.gd_LotStep = 0.01
17:26:03 2014.07.22 14:10  RSI EURUSD,M5: LotPrecision(): SLots.gd_LotStep < 1
17:26:03 2014.07.22 14:10  RSI EURUSD,M5: SLots.gd_LotStep *= 10
17:26:03 2014.07.22 14:10  RSI EURUSD,M5: LotPrecision(): SLots.gd_LotStep = 0.1
17:26:03 2014.07.22 14:10  RSI EURUSD,M5: LotPrecision(): li_LotPrecision = 1
17:26:03 2014.07.22 14:10  RSI EURUSD,M5: LotPrecision(): SLots.gd_LotStep < 1
17:26:03 2014.07.22 14:10  RSI EURUSD,M5: SLots.gd_LotStep *= 10
17:26:03 2014.07.22 14:10  RSI EURUSD,M5: LotPrecision(): SLots.gd_LotStep = 1.0
17:26:03 2014.07.22 14:10  RSI EURUSD,M5: LotPrecision(): li_LotPrecision = 2
17:26:03 2014.07.22 14:10  RSI EURUSD,M5: LotPrecision(): LOTPRES = 2
17:26:03 2014.07.22 14:10  RSI EURUSD,M5: LotPrecision()_Before: li_LotPrecision = 0
17:26:03 2014.07.22 14:10  RSI EURUSD,M5: LotPrecision()_BeforeCircle: SLots.gd_LotStep = 1.0
17:26:03 2014.07.22 14:10  RSI EURUSD,M5: LotPrecision(): LOTPRES = 0
17:26:03 2014.07.22 14:10  RSI EURUSD,M5: Lot = 0
17:26:03 2014.07.22 14:10  RSI EURUSD,M5: LotPrecision()_Before: li_LotPrecision = 0
17:26:03 2014.07.22 14:10  RSI EURUSD,M5: LotPrecision()_BeforeCircle: SLots.gd_LotStep = 1.0
17:26:03 2014.07.22 14:10  RSI EURUSD,M5: LotPrecision(): LOTPRES = 0
17:26:03 2014.07.22 14:10  RSI EURUSD,M5: Lot = 0
17:26:03 2014.07.22 14:10  RSI EURUSD,M5: LotPrecision()_Before: li_LotPrecision = 0
17:26:03 2014.07.22 14:10  RSI EURUSD,M5: LotPrecision()_BeforeCircle: SLots.gd_LotStep = 1.0

This clearly shows that after exiting the LotPrecision() function, the value of theli_LotPrecision variableis lost. How to make it remain constant?

I tried adding thisli_LotPrecision variable to private class section, in order to assign it to static modifier later, but the compiler insists that it is impossible.

unresolved static variable 'BaseInfo::li_LotPrecision'  BaseInfo.mqh    60      10

Throwing the variable out of the class has no sense, because it is not needed anywhere else.

What to do?

Reason: