Você está na página 1de 14

Coding guidelines

Rules vs guidelines
Guidelines
any guide or indication of a future course of action: guidelines on the government's future policy. a principle put forward to set standards or determine a course of action

Rule
a principle or regulation governing conduct, action, procedure, arrangement, An authoritative, prescribed direction for conduct

Why guidelines
Practical programming in c
From 1980 to 1990, the average number of lines in a typical application went from 23,000 to 1,200,000. The average system age went from 4.75 to 9.4 years. What's worse, 74% of the managers surveyed at the 1990 Annual Meeting and Conference of the Software Maintenance Association reported that they "have systems in their department, that have to be maintained by specific individuals because no one else understands thems.

Why guidelines
90% of the time the documentation is lost. Of the remaining 10%, 9% of the time the revision of the documentation is different from the revision of the program and therefore completely useless. The 1% of the time you actually have documentation and the correct revision of the documentation, the information will be written in Chinese.

Metric
Quality metric
Lines of code Cyclomatic complexity Halstead Complexity

Cyclomatic Complexity V(G)


Computing the cyclomatic Complexity

number of simple decisions + 1

or
number of enclosed areas + 1 In this case, V(G) = 4

Clycomatic complexity
howComplex() { int i=20; while (i<10) { System.out.printf("i is %d", i); if (i%2 == 0) { System.out.println("even"); } else { System.out.println("odd"); } } }

V(G) = 2 enclosed area + 1 = 3

The National Institute of Standards and Technology


Structured Testing: A Testing Methodology Using the Cyclomatic Complexity Metric
The original limit of 10 as proposed by McCabe has significant supporting evidence, but limits as high as 15 have been used successfully as well. Limits over 10 should be reserved for projects that have several operational advantages over typical projects, for example experienced staff, formal design, a modern programming language, structured programming, code walkthroughs, and a comprehensive test plan

Cylco continue
Cyclomatic complexity gives the number of tests, which for a multiway decision statement is the number of decision branches

TATA CODING GUIDELINES GOOD PARTS


Defined in the document

Visual
A printed page should be no more than 80 columns. Number of statements in a file should not exceed 1000 (thumb-rule, but Compiler dependent) and the number of lines 2000. Number of statements in a function should not exceed 100 (thumb-rule, but Compiler dependent) in a function and the number of lines 200.

Lines of code
Only one statement should exist per line. Avoid use of numeric values (Magic Numbers) in code; use symbolic values instead. Use a space before and after a binary operator (for e.g. in C use - if ( value == 0 ) and not - if ( value==0 ) ). Inaccessible code should be avoided. Care should be taken when using GOTO or return statement. Multiple assignments are not recommended. e.g. a=b=c ; Maximum 4 Levels of Control Structure Nesting : Nesting of statements, "if", "for", "while", etc., should go no more than 4 levels. If more levels appear to be needed, consider use of a function at one of the higher levels .

Tata coding guidelines Bad parts


80 columns Lines of code
Functions Lines of code Complexity 151 LTE_RLCTx_Encode_Re 1602 Trans_PDU
LTE_RRM_ARM_UpdtP 1287 DSCHAndPDCCHInfo LTE_RRM_ARM_Resou 923 rceBlockreq

107

84

Você também pode gostar