502045
Software Engineering
Chapter 07
Lesson 09: Code Quality
July 30, 2020 502045 -Code Quality 1
Code Quality
• Agenda
– Review Process
– Common Defects
July 30, 2020 502045 -Code Quality 2
Review Process - Where the Coding is?
July 30, 2020 502045 -Code Quality 3
Review Process – The Quality
Triangle
Product’s View
Product
User Requirements Requirements Specification
Manufacturer’s View
User’s View
Figure 5: The Quality Triangle
July 30, 2020 502045 -Code Quality 4
Review Process - The UR-RS gaps
• The gap is likely to include:
– Wrong interpretation of requirement and
ambiguity in the specification.
– Requirements identified after development
commenced [started].
– Changes to specified requirements identified
after development commenced
– Requirement ignored by the developers
because they were too difficult to implement.
July 30, 2020 502045 -Code Quality 5
Review Process - The Software-
UR Gap
• The gap occurs because the software
doesn’t satisfy the user requirements
• The size of the gap is directly dependent
on the side of other 2 sides of triangle
July 30, 2020 502045 -Code Quality 6
Review Process - Closing gaps
between 3 views
• Static test:
– Review: online/offline
– Inspection
• Dynamic test:
– Unit Test
– Integration Test
– System Test
– Acceptance Test
July 30, 2020 502045 -Code Quality 7
Common Defects & Practices
Hard code constants
• Issue with giving a fixed value in codes, for
example:
[Link] = 10;
The problem occurs when you should change
these values multiple times!!!
• Preventive Action: define constants in the
common constant module or in a configure
files
July 30, 2020 502045 -Code Quality 8
Common Defects & Practices
Array Index Start from 0
• Issue with below C-Language codes?
int i, a[10];
for (i=1; i<=10; i++) a[i] = 0;
July 30, 2020 502045 -Code Quality 9
Common Defects & Practices
The Dangling else Problem
• Issue with below C-Language codes?
if (x == 0)
if (y == 0) error();
else {
z = x + y;
f (&z);
}
July 30, 2020 502045 -Code Quality 10
Common Defects & Practices
Null Pointer Exception
• Issue: the developer got Null-Pointer-Exception run-time
error, while he/she did not detect that when compiling the
codes
pPointer->member = 1;
strReturn = [Link](strName);
• Cause: the developer does not check null or think about
null object before accessing object's value.
• Preventive: Should check null before accessing object or
pointer before using its member
If ( pPointer != NULL ) pPointer->member = 1;
If (objDoc != NULL)
strReturn = [Link](strName);
July 30, 2020 502045 -Code Quality 11
Common Defects & Practices
Detect Common Defects Sample
• public bool IsValidLogin(string userName, string password) {
• SqlConnection con = null;
• SqlCommand cmd = null;
• bool result = false;
• try {
• con = new SqlConnection(DB_CONNECTION);
• [Link]();
• string cmdtext = [Link]("SELECT * FROM [Users] WHERE [Account]='{0}' AND
[Password]='{1}' “, userName, password);
• cmd = new SqlCommand(cmdtext);
• [Link] = con;
• [Link] = [Link];
• result= [Link]().HasRows;
• [Link]();
• [Link]();
• return result;
• }
• catch (SqlException) {
• return false;
• }
• }
July 30, 2020 502045 -Code Quality 12
Common Defects & Practices
Programming Practices 1
• Issue with variables or create objects in Loop?
for (int i=0; i<[Link]-1; i++)
{
string strName;
strName = [Link][i]
["Name"].ToString();
//do something here
}
Impact to the application performance!!!
• Cause: memory is allocated repeatedly.
July 30, 2020 502045 -Code Quality 13
Common Defects & Practices
Programming Practices 2
• Code redundant issues:
– Create new Object while we can reuse the object in previous
command:
BeanXXX bean = new BeanXXX();
bean = [Link]();
– Variables are declared in based class but it is not used
July 30, 2020 502045 -Code Quality 14
Common Defects & Practices
Programming Practices 3
• Avoid using an object to access a static variable or
method. Use a class name instead.
• Avoid assigning several variables to the same
value in a single statement.
[Link] = [Link] = 'c'; // AVOID!
July 30, 2020 502045 -Code Quality 15
Common Defects & Practices
Programming Practices 4
• Do not use the assignment operator in a place
if (c++ = d++) { // AVOID!
...
}
should be written as:
if ((c++ = d++) != 0) {
...
}
• Do not use embedded assignments in an
attempt to improve run-time performance.
d = (a = b + c) + r;
July 30, 2020 502045 -Code Quality 16
Common Defects & Practices
Programming Practices 5
• File operations: file read operations must be restricted to a minimum
• Clear content of big structure after use: always clear() the content of
Collection/Map objects after use
• Be economical when creating new objects
• In program language that has no garbage collector (i.e C, C++): free
allocated memory after use:
{
double* A = malloc(sizeof(double)*M*N);
for(int i = 0; i < M*N; i++){
A[i] = i;
} memory leak: forgot to
} call free(A);
common problem in C, C+
+
July 30, 2020 502045 -Code Quality 17
Common Defects & Practices
Programming Practices 6
• Use parentheses liberally in expressions involving mixed operators
to avoid operator precedence problems
if (a == b && c == d) // AVOID!
if ((a == b) && (c == d)) // RIGHT
• Try to make the structure of your program match the intent [goal], for
example:
if (booleanExpression) {
return true;
} else {
return false;
}
should instead be written as
return booleanExpression;
July 30, 2020 502045 -Code Quality 18
July 30, 2020 502045 -Code Quality 19