OOP vs procedural programming - page 6

 
Dmitry Fedoseev:

And how are you going to make trailing adapt to different parameters?

Let's try it on a specific task. If you have one, please provide it.
 
Реter Konow:
Preferably lead to a specific task. Such a description is not very clear. In my practice, the algorithm does not change from changing external parameters. It is made universal in advance for any values of these parameters. Therefore, it is not very clear what you mean. Describe it on a concrete example.
class Ордер
{
  public: int SELL;

  Ордер(void) // Конструктор имеет то же имя, что и класс. Выполняется при инициализации переменной класса
  {
    SELL=0;
    int k=OrdersHistoryTotal()-1;
    for(; k>=0; k--)
    {
      if(!OrderSelect(k, SELECT_BY_POS, MODE_HISTORY)) continue;
      if(OrderType()==OP_SELL)SELL++;
    }
  }
}x;

void OnStart()
{
  Alert(x.SELL);
}

Thanks to the OOP, the main program is very brief and clear. This is just the first example that comes to mind. If you need to calculate the number of orders frequently, the advantages are obvious. It is hard to understand it at the first time. But functions, even with parameters, were also once a difficulty

 
Реter Konow:
Let's try on some particular task. If you have, please provide.

Specific task. The customer has ordered an Expert Advisor for two MAs, which would include all trailing variants available in the code base, but would not slow down in the tester.

The EA should also have the prospect to be updated with new trailing variants in the future (at low cost).

 
STARIJ:

Thanks to OOP, the main program is very short and clear. This is just the first example that comes to mind. If we need to calculate the number of orders frequently, the advantages are obvious. It is hard to understand it at the first time. But functions, even with parameters, were also once a difficulty

I don't understand, why not make a function "int Number_orders()" that will always do the above loop and return the counter value "SELL"?

For example:

 int Количество_ордеров()
  {
    SELL=0;
    int k=OrdersHistoryTotal()-1;
    for(; k>=0; k--)
    {
      if(!OrderSelect(k, SELECT_BY_POS, MODE_HISTORY)) continue;
      if(OrderType()==OP_SELL)SELL++;
    }
   return(SELL);
  }


Why do we need a class here?

 
Dmitry Fedoseev:

For example, 100 trailing stop variants need to be crammed into one Expert Advisor. When programming procedurally, you get a mess like this:

100 identical code fragments. When the program is running, it will usually include only one trailing stop. The remaining 99 ifs will just consume resources.

Now for the OOP variant. During Expert Advisor initialization, we scale the array with pointers according to the number of trails, we create objects only for the included trails. As a result, the following code will work all the time:

If one trailing bar is enabled, then cnt=1, i.e. there is nothing unnecessary.

Make an array of function names, choose the name from the index and access.


OOP has nothing to do with it. This is a language limitation they are trying to solve with OOP

 

I haven't tried it, but for example you can't make a non-typical language without an OOP.

 
Реter Konow:

I don't understand why not make a function "int Number_orders()" which will always do the above loop and return the counter value "SELL"?

Having finalized the class, I'll get x.SELL x.BUY x.ALL and everything else I need. And it will be very easy to address them. OOP classes - for simplicity
 
Dmitry Fedoseev:

Specific task. The customer has ordered an Expert Advisor for two MAs, which would include all trailing variants available in the code base, but would not slow down in the tester.

And with a prospect of adding new trailing variants in the future (not expensive).

I see. This is an indisputable argument in favor of OOP. Stupidity of the customer and lack of time to fight it and unwillingness to improve somebody else's algorithm.) Yes, you need OOP in this case. I agree).
 
STARIJ:
By refining the class, I will get x.SELL x.BUY x.ALL and whatever else is needed. And it will be very easy to address them. OOP classes - for simplicity
By fine-tuning the function, you can get all these same variables in an array, which you will pass into this function. Then use it as intended...
 
СанСаныч Фоменко:

Make an array of function names, pick a name from the index and call it.


It has nothing to do with OOP. This is a language limitation they are trying to solve with the help of OOP.

There is no function call by the name defined as a string. And in those languages where it exists, it's done through a hash of a table, that is, it's a terrible brake.

A normal way to solve this problem would be to use pointers to functions (about this I wrote here). But why use pointers alone, there is a more convenient way - OOP, which allows not only to take advantage of function pointers, but also conveniently structure data and code.