8.coding Standards and Code Review Techniques
8.coding Standards and Code Review Techniques
1
Coding Standards
• Good software development
organizations require their
programmers to:
–adhere some standard style of
coding
–called coding standards.
2
Coding Standards
• Many software development
organizations:
–formulate their own coding standards
that suits them most,
–require their engineers to follow
these standards.
3
Coding Standards
• Advantage of adhering to a
standard style of coding:
–it gives a uniform appearance to the
codes written by different engineers,
–it enhances code understanding,
–encourages good programming
practices.
4
Coding Standards
• A coding standard
–sets out standard ways of doing
several things:
•the way variables are named,
•code is laid out,
•maximum number of source lines
allowed per function, etc.
5
Coding guidelines
• Provide general suggestions
regarding coding style to be
followed.
6
Code inspection and code walk throughs
7
Code inspection and code walk
throughs
• Detect as many errors as possible
during inspection and walkthrough:
–detected errors require less effort for
correction
• much higher effort needed if errors were
to be detected during integration or
system testing.
8
Coding Standards and Guidelines
9
Representative Coding Standards
10
Representative Coding Standards
11
Representative Coding Standards
• Header data:
– Name of the module,
– date on which the module was created,
– author's name,
– modification history,
– synopsis of the module,
– different functions supported, along with their
input/output parameters,
– global variables accessed/modified by the module.
12
• Naming conventions for global variables, local
variables, and constant identifiers:
• A possible naming convention can be that global
variable names always start with a capital letter
(GlobalData), local variable names start with small
letters (localData), and constant names are always
capital letters (CONSTDATA).
13
Representative Coding Standards
14
Representative Coding Guidelines
15
Representative Coding Guidelines
• Clever coding can obscure meaning of the code:
–hampers understanding.
–makes later maintenance difficult.
• Avoid obscure side effects
–modifications to the parameters passed by
reference, modification of global variables.
16
Representative Coding Guidelines
17
Representative Coding Guidelines
• Lengthy functions:
–usually very difficult to understand
–probably do too many different
things.
18
Representative Coding Guidelines
19
Code review
20
Code Walk Through
• An informal code analysis technique.
– undertaken after the coding of a module is
complete.
• A few members of the development team select some
test cases:
– simulate execution of the code by hand using these
test cases.
• Discussion should focus on discovery of errors:
– and not on how to fix the discovered errors.
21
Code Walkthrough
22
Code Inspection
• In contrast to code walk through, the aim of code
inspection is to discover some common types of errors
caused due to oversight and improper programming.
• In addition to the commonly made errors, adherence
to coding standards is also checked during code
inspection.
• Good software development companies collect
statistics regarding different types of errors commonly
committed by their engineers and identify the type of
errors most frequently committed.
• Such a list of commonly committed errors can be used
during code inspection to look out for possible errors.
23
Commonly made errors
• Use of uninitialized variables.
• Nonterminating loops.
• Array indices out of bounds.
• Improper storage allocation and deallocation.
• Actual and formal parameter mismatch in procedure calls.
• Jumps into loops.
24
Code Inspection
• Use of incorrect logical operators
– or incorrect precedence among operators.
• Improper modification of loop variables.
• Comparison of equality of floating point values, etc.
• Also during code inspection,
– adherence to coding standards is checked.
25
Programming (Coding) Style & Conventions
26