Help me learn how to program. - page 3

 
Tio Nisla:
I wanted to add earlier that if somebodyfunc() function does some other manipulations besides returning some amount or something, calculates trade parameters, for example, such usage causes hard-to-catch artifacts and might lead a code's author into a stupor: "Ht? How come 4 times??? O_o F$#@!!! Isn't it supposed to be there three times? Why is my EA lying to me?". That's what I called "shithcod," which an expert was outraged to the roots of his hair. I didn't bring it up, for it's obvious, but you did it for me. But you failed to take into account that addressing by an index dynamically calculated without array reinitialization is something else. In bare sys this usually leads to GPF, in pluses with smart pointers and arrays to an exception and its handler. In mql it is not clear what it leads to.

What is "elusive artefacts"? Is it a religious thing? You make it up, you believe it?

 
Aleksandr Slavskii:

Gentlemen, personalities, but let's find out the truth.

I see on the example thatPositionsTotal()is called at each loop iteration.

But Dimitri, on the contrary, you are saying that the compiler does it in a different way - I don't understand it. Explain.

If you want to say that thePositionsTotal() function does not recalculate positions each time, but simply returns the value of a variable containing the number of open positions, then yes, you are right, there is no sense in declaring one more variable, but then what does the compiler have to do with it?

And if this function recalculates open positions every time, then it turns out that the compiler has to understand if the value of this function affects further calculations and uses either a function or a variable.

I don't seem to get it right.

It recalculates each time or doesn't recalculate it, but the call of PositionTotal() is definitely costly. And this is easily proved by a simple experiment. But instead of giving a killer experiment that would smash me to bits, they come up with fancy arguments with "hard-to-find artefacts". You can find that example code from the first page you know where and see how it looks there))) It was deliberately changed before it was inserted into the forum (great applause). And it was done only to prevent unnecessary burdens for a beginner.

And the compiler, I meant that it optimizes the code and calls the variable directly instead of through a function:

int x;

for(;i<x;)

и

for(;i<F();)

int F(){return(x);}

This can be clearly seen with the ArraySize() function; you cannot tell the difference between calling a function and using a variable with it. But with PositionsTotsl() this is not the case, alas.

By the way, you can also get to the bottom of declaring variables inside a loop, which also reduces speed. What is not getting to the root? You don't know about it? Although we can argue here too, there is a difference between 4 and 5.

 

Surely PositionsTotal() asks for the number of open positions every time? Or maybe it is optimized and saves the value into a constant with an update mark and just returns always the same number if it does not change)?

There must be some internal optimization of this function. Developers are not fools to make such an important and potentially costly function for "it will do anyway".

Those who are indignant, please check how much time and resources it takes to perform both variants of the same loop.

There's no need to throw poop.


I would like to see the source code of PositionsTotal() purely for academic interest.


Well, when you think about it, the simplest thing would be to make a global variable of the terminal storing this value. And return only this value. And to update it at opening or closing of positions - to make the variable safe and to synchronize it, so that God forbid something was written there.

I think that this is how it is done.

Well, the data about the positions themselves are probably stored in some data structures, so that it was possible to get them without turning to servers unnecessarily. In general I think there everything is normal with productivity in any variant of call for/.


And the code style is aesthetically beautiful or not, everyone decides for himself)

 
Nikolay Mitrofanov:

Whoever resents it, please check how much time and resources it takes to run both variants of the same cycle.

There is no need to throw poop.

I hope it was not directed at me, because I don't doubt Dmitriy's authority.Tio Nislatoo seems to have been coding for a long time, but


I am learning, that is why I ask.

 

PositionsTotal() is tricky, it must always return the correct number, and change its value as soon as the number of positions changes. Therefore it is unlikely to ever be as fast as a simple variable or ArraySize().

 
Aleksandr Slavskii:

...


I'm learning, that's why I'm asking.

If you're learning, don't get bogged down by these little things at all. Concentrate on the ability to translate your idea into code (or some ill-defined process into a sequence of actions). And then the code can be tweaked as you like.

 
Dmitry Fedoseev:

If you're learning, don't worry about these details at all. Concentrate on being able to translate your idea into code (or some ill-defined process into a sequence of actions). And then the code can be tweaked as you like.

I think it's good that a person tries to understand it and digs deeper.

By not paying attention to small things the coder gets into the habit of writing the code as he sees fit. And then combing the code means double work and often not only for the author but for those who are still lucky to work with the code.

Why bother writing code when you can do it properly and write it well and understand the details?)

Your advice is... well... IMHO


Your advice is good for an experienced programmer (strongly self-confident) who can then clean up, because he or she knows where and what must be cleaned.

 
Nikolay Mitrofanov:

For me, I think it's good that a person tries to understand and digs deeper...

By not paying attention to small things, the coder then gets into the habit of writing code in the wrong way. And then combing the code means double work and often not only for the author but for those who are still lucky to work with the code.

Why bother writing code when you can do it properly and write it well and understand the details?)

Your advice is... well... IMHO


Your advice is good for an experienced programmer (strongly self-confident) who can then comb through, because he knows where and what must be combed.

For beginners all the more - it is better at least somehow, but to solve a problem, instead of getting stuck at a level of forum bickering such as "how you can write here without const, and here without static, etc., etc. or - oh man, if you ask this question, then you better not come close to programming at all".

Not a week goes by that someone does not send me an indicator to fix some code like this: for(int i=0;i<Bars;i++). And you're rubbing problems here, which may give you speed... well, 30%.

Even efficient EAs (in the sense of speed of testing) are problems of their algorithm, which every time is reborn for each specific strategy, rather than a problem of syntax intricacies.

 
Dmitry Fedoseev:

For beginners all the more - it is better to solve the problem at least somehow, rather than get stuck at a level of forum bickering such as "how you can write here without const, and here without static, etc., etc. or - oh man, if you ask this question, then you better not approach to programming at all".

Not a week goes by that someone does not send me an indicator to fix some code like this: for(int i=0;i<Bars;i++). And you're rubbing problems here, which may give you speed... well, 30%.

If a novice programmer asks something on the forum, it means he wants to know the answer, it means he is interested and needs it. Even created a thread to get advice.

If I got the meaning of the subject right, the question of implementation of his idea wasn't "just do it right but make it work" but as best and as correctly as possible. That's good. This is right.

"Just somehow" for any activity is a terrible approach.

First to figure it out, then to do it with knowledge - the best approach.

 
Nikolay Mitrofanov:

If a novice programmer asks something on the forum, it means he wants to know the answer, it means he is interested and needs it. He even created the thread to get advice.

If I understood the author of the thread correctly, then the question of implementation of his idea is no longer "do as you please just to make it work", but immediately as best as possible and as correctly as possible. That's good. This is right.

"Just somehow" for any activity is a terrible approach.

First to understand, then to do with knowledge is the best.

With knowledge of what? If this algorithm does not exist in nature, but has to be invented on its own, and it is the algorithm that defines everything.

Reason: