About coding style - page 5

 

Where are the classes in MQL4, C-4? And I think the developers said that there will be no inheritance in 5 for the time being.

 
Mathemat >> :

Where are the classes in MQL4, C-4? And I think the developers said that there will be no inheritance in 5.

There already is, with all its attendant niceties. And you can see it even in Tetris (in mq5 sources).

 

Wow, that's interesting. Three months since the toy was published, and I've only just found out...

 
Mathemat >> :

Wow, that's interesting. Three months since the toy was published and I've only just found out...

I think I might have missed it....

what's the point of this exclamation "wow"? is there any mt5 out there to test the sources? if so, don't mind giving me a link to download it.

 

Well, no, the MT5 is not yet visible (to mere mortals), but at least you can look at the code. It's roughly clear what to expect.

 

Can anyone suggest an application that can parse MQL-source and output in some form the names of used variables/functions and number of their "mentions" in the text?

for example to catch the rest of unused variables from the thrown out texts, and not to accidentally rename a severely abbreviated name to something longer and clearer, which turns out to be the name of a global variable that you forgot about a long time ago and that it exists somewhere in the code.

 
YuraZ >> :

I prefer

if ( a > c )

codret = 3;

if ( l < b )

codret = 5;

...

...

if ( h != b )

codret = 100;


// especially if there are some additional operations common for all outputs before return

... e.g. here we do something else

retrun( codret);

}

You just gave a prime example of why I use multiple return outputs. In your example, the function will return the last triggered if(), not the first. In my example, the function will return the first match. In practice, I often find that I need to return exactly the first value satisfying the condition, and I almost never have to return the last triggered check. In this example, having just one return does not make it easier to trace the values returned by the function, on the contrary, you can expect everything from just one return.

By the way, I personally dislike separating the crucial ";" character from the expression:

extern double max_price[100] ; //stores prices of maxima of last hundred bars

extern double max_price[100] ; //Holds prices of the last hundred bars maximum - I only write it this way

 
In the 70s there was a very fashionable trend called 'Structured Programming'. Many books were written about it. The most remembered sentences were: function should fit on one sheet (there were no displays then), no goto, and so the whole book. Then, supposedly, it all turned into object-oriented programming. Maybe we are reinventing the wheel again?
 

No, we're just talking about style here. By the way, in some cases I don't mind goto at all.

 
C-4 >> :

Why declare a function within a function? Personally, as I grew up only in C and C++ I don't understand this method.

Encapsulation... When you write templates and want to hide a very local function or functor or class, it's very useful.

And most of the time you can get by with an unnamed namespace.

2. I hate very common style of putting curly brackets like this:

Very much in vain. A breeding ground for all sorts of bloopers with braces. In the past, the justification was space saving and the rule of function code size.

But now in the age of terabytes, why should we be sorry for lack of space?

5. I use functions inside functions:

Inefficient (with several calls of one), smeared out and long. Especially one of UB, which is a hotbed of malicious nontrivial errors.


Further -- about the copypastes. Very annoying thing :). I confess, I use it and make mistakes very often. For instance, I forgot to rename a variable, change a comment or whatever.

That is why code review is a very useful thing.


About Pascal :) . Up to now, it's still the main Olympiad language. For one simple reason - few keywords, logical structure.

For a long time I didn't believe that Pascal (or rather Delphi) was worse than C++, until I found out C++ was better.

Now I think that Pascal is better in one way: it's harder to write binary code in it.


>> Then, about the comments, it's all bullshit, especially about 30%.

Write self-documenting code, and you will be happy. Since there are no classes in MQL4 it is rather difficult to do. But with classes it should get easier.


Everything else -- indentation, naming principles, alignment -- is nothing more than recommendations.

By the way, speaking of functions. What should be the font? What if it's one line longer? The main thing is to make it clear what it does, and the rest is just recommendations.


The most important thing is to make the code as understandable as possible for those who might maintain it.

Reason: