static values in methods

 
hello

I'm told that static values in class methods should absolutely be avoided, because if the class is instantiated multiple times, the values of these static values will be shared between all instances... a real bug to find.

Therefore, the only way to do this is to re-upload this static element to a member.

Your opinion on the matter
THANKS
 
I have a habit of using static variables because I think it can make the code more readable compared to only using global variables instead of static variables. 
Static vars essentially work the exact same way as global variables just that the static variables scope is only within the function/block where it is used.
 
Conor Mcnamara # :
I have a habit of using static variables because I think it can make the code more readable compared to only using global variables instead of static variables. 
Static vars essentially work the exact same way as global variables just that the static variables scope is only within the function/block where it is used.
Thanks for your reply, but that's not my question.
 
It's a matter of object-oriented programming
And it seems that using it in a method isn't the smartest thing to do, especially if the class is instantiated multiple times.
 
Gerard William G J B M Dinh Sy #:
It's a matter of object-oriented programming
And it seems that using it in a method isn't the smartest thing to do, especially if the class is instantiated multiple times.

everything has its purpose. For some tasks, it is THE ONLY solution. For other things it is not necessary. No one is forcing you to use them :). Static methods in classes are more widely used. Static variables more rare.

One example of proper usage of static variable could be saving the environment state. Something like this:

static bool cTerminal::VisualMode(void)
  {
//===============
   static const bool visual=(bool)::MQLInfoInteger(MQL_VISUAL_MODE);
//===============

//===============
   return(visual);
//===============
  }

Since this state won't change, you only get it once at EA start and later share among all instances of the class. 

The same works for a member

static const bool m_isvisual;
which will be calculated only once during class construction.
 
Andrey Barinov #:

everything has its purpose. For some tasks, it is THE ONLY solution. For other things it is not necessary. No one is forcing you to use them :). Static methods in classes are more widely used. Static variables more rare.

One example of proper usage of static variable could be saving the environment state. Something like this:

Since this state won't change, you only get it once at EA start and later share among all instances of the class. 

The same works for a member

which will be calculated only once during class construction.

This is not what is discussed.

This is what is discussed :

class C
 {
public:
  void               CallAMethodWithAStatic(void)
   {
    static int MyStaticCounter = 0;
    MyStaticCounter++;
   }
 };
 
Alain Verleyen #:

This is not what is discussed.

This is what is discussed :

Well, I thought 

I'm told that static values in class methods should absolutely be avoided
are discussed. And TS is told to absolutely avoid them :). I gave an example of when static values in class methods can be used without problems. Why is it not what is discussed?
 
If you want a single shared value across multiple classes, using a static variable is the simplest approach in my opinion, and it can be efficient. If you don't use static vars, then you would need to use an instance of the class object, and get the value exposed through getters and setters. Even inside class methods, you might want to share a value such as a counter number or something.
 
Andrey Barinov #:

I gave an example of when static values in class methods can be used without problems. Why is it not what is discussed?

Probably Alain spotted the thing that you showed a static method, whereas the OP asked about static variable in object methods, because he mentioned instances.
 
Gerard William G J B M Dinh Sy #:
It's a matter of object-oriented programming
And it seems that using it in a method isn't the smartest thing to do, especially if the class is instantiated multiple times.
This depends from your task. For example, a static variable is excellent in "describing" its own context (function signature, initialization/factory of objects with private constructor) - this allows to implement a custom tools for tracing calls and stacks of MQL5 program - just to name one smart usage.
 
Stanislav Korotky #:
Probably Alain spotted the thing that you showed a static method, whereas the OP asked about static variable in object methods, because he mentioned instances.

Exactly.

Stanislav Korotky #:
This depends from your task

Exactly.