Writing rules of MQL4 program code


As an excellent programmer, the standard of programming code writing is very important. If an MQL4 programmer doesn't write standard books, he won't be a good MQL4 programmer. MQL4 is consistent with the specification of C language and C + +, so the following has high reference value.
Rules for writing C programhttp://c.biancheng.net/cpp/html/10.html
From the perspective of clear writing, easy to read, understand and maintain, the following rules should be followed when writing procedures:
1. A description or statement occupies one line.
2. The part enclosed by {} usually represents a certain hierarchy of the program. {} is usually aligned with the first letter of the structure statement and occupies a single line.
3. The lower level statement or instruction can be indented into a number of spaces and then written. In order to look clearer, increase the readability of the program.
In order to develop good programming style, we should follow these rules. Below to share a software engineering programming specification [part].1. Basic requirements 1.1 the program structure is clear and easy to understand, and the number of program lines of a single function shall not exceed 100 lines.
1.2 what you plan to do should be simple, straightforward, concise code, and avoid garbage programs.
1.3 use standard library functions and common functions as much as possible.
1.4 do not define global variables randomly, and try to use local variables.
1.5 use brackets to avoid ambiguity.2. Readability requirements 2.1 readability first, efficiency second.
2.2 keep the comments exactly the same as the code.
2.3 each source program file has a file header description, and the specification is shown in the specification.
2.4 each function has a function header description, and the specification is shown in the specification.
2.5 when the main variables (structure, union, class or object) are defined or referenced, the annotation can reflect its meaning.
2.7 the definition of constant (define) has corresponding description.
2.8 each stage of the process is annotated.
2.9 there are notes before typical algorithms.
2.10 indent is used to display the logical structure of the program. The indent is consistent and takes the tab key as the unit. The tab is defined as 6 bytes.
2.11 loop and branch levels should not exceed five levels.
2.12 comments can be on the same line as the statement or on the line.
2.13 blank lines and white space characters are also special comments.
2.14 clear statements are not annotated.
2.15 the scope of annotation can be: definition, reference, conditional branch and a piece of code.
2.16 the number of comment lines (excluding program header and function header description) should account for 1 / 5 to 1 / 3 of the total number of lines.3. Structural requirements 3.1 prohibit two equivalent branches.
3.2 goto statements are prohibited.
3.3 use if statements to emphasize that only one of the two sets of statements is executed. Else goto and else return are prohibited.
3.4 use case to realize multi-channel branching.
3.5 avoid multiple outlets from circulation.
3.6 the function has only one exit.
3.7 do not use conditional assignment statements.
3.8 avoid unnecessary branching.
3.9 do not easily replace logical expressions with conditional branches.4. Correctness and fault tolerance require 4.1 the program is correct first, and then beautiful
4.2 it is impossible to prove that your program is correct, so you should check back after writing a section of program.
4.3 when changing an error, new errors may occur. Therefore, the influence on other programs should be considered before modification.
4.4 all variables must be initialized before calling.
4.5 the validity of all user input must be checked.
4.6 do not compare the equality of floating-point numbers, such as: 10.0 * 0.1 = = 1.0, unreliable
4.7 when the program is related to the environment or state, it must take the initiative to deal with the unexpected events, such as whether the file can be locked logically, whether the printer is online, etc.
4.8 unit test is also a part of programming. The program submitted for joint commissioning test must pass unit test.5. Reusability requires that 5.1 reusable algorithms or codes that complete relatively independent functions should be abstracted as public controls or classes.
5.2 public control or class should consider OO idea, reduce external contact, consider independence or encapsulation.
5.3 common control or class should establish use template.

    Was this article helpful?

    1 out of 1 found this helpful