presentation

Writing Quality Code
Hitesh A. Bosamiya
Technical Architect – Software Expert
HP Software - Big Data, India
April 23, 2015
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
Who is the Bravest?
Client
Test
Team
Q.A. Manager
Team Lead /
Scrum Master
P.M.
Developer
BUG!!!
2
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
Source: Solid Private and Commercial Security Services
Confident
3
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
Introspective
4
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
Take Away
Writing Quality Code
5
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
About Me
6
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
Dictionary Definitions
Quality
• the character in a logical proposition of being affirmative or
negative
7
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
Dictionary Definitions
Quality
• the character in a logical proposition of being affirmative or
negative
Code
• a set of instructions for a computer
8
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
Dictionary Definitions
Quality
• the character in a logical proposition of being affirmative or
negative
Code
• a set of instructions for a computer
Program
• a plan or system under which action may be taken toward a goal
9
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
Dictionary Definitions
Quality
• the character in a logical proposition of being affirmative or
negative
Code
• a set of instructions for a computer
Program
• a plan or system under which action may be taken toward a goal
Programming
• to work out a sequence of operations to be performed by (a
mechanism)
10
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
A Quick Glance at Quality Process
Code Review Process & It’s Effectiveness
11
Developer
Reviewer
Quality
0
0
Suffers
0
1
May Suffer
1
0
May Suffer
1
1
Good
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
A Quick Glance at Quality Metrics
Defect Density
Artifact
Original Code
Modified Code
12
KLO
Defect Density
Defects
C
(Defects/KLOC)
16.5
2.0
33
9.3
3.5
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
Myth vs. Reality
Myth
The code is not seen by the customer
Reality
The product is seen by the customer
• Which in-turn made out of code, not of the design document!!!
• Does this mean that the design is not important?
13
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
“Know one level below”
i.e.
“Know what is under the hood”
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
Why One Level Below?
Experience 1 – Self Modifying Code
CMP AX, 0001H
JNE instr_to_change
MOV WORD PTR [instr_to_change + 1], 0B0CH
instr_to_change:
MOV CX, 0000H ; or MOV CX, 0B0CH
NOP
NOP
NOP
NOP
NOP
NOP
Experience 2 – C Compiler Release Mode Optimization
Assembly
Language
Microprocessor
Architecture
C/C++
Assembly
Language
15
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
Applicability
16
Assembly
Language
C/C++
Java
NodeJS
Microprocessor
Architecture
Assembly
Language
JVM
Event Loop
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
“Let the dirty work be done by the
computer” - Unknown
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
A Simple Example
Date selector in dynamically generated HTML page
// repetition of code
print("<OPTION>1</OPTION>");
print("<OPTION>2</OPTION>");
print("<OPTION>3</OPTION>");
...
...
...
print("<OPTION>31</OPTION>");
// Using logic
final int MAX_DAYS_IN_MONTH = 31;
for (int i = 1;
i <= MAX_DAYS_IN_MONTH;
i++) {
print("<OPTION>" +
i +
"</OPTION>");
}
Advantages
Programming Pride, Extensibility, Maintainability
18
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
Limit function/method size
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
An Example
Original Code
20
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
An Example
Modified Code – One Screen Full!
21
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
“Any fool can write code that a
computer can understand.
Good programmers write code that
humans can understand.”
Martin Fowler
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
Don’t State the Obvious
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
No “Divide by 2” Comments
public void myMethod(int foo) {
...
if (foo == 2) {
for (int i = 0; i < myMax; i++) {
while (true) {
...
} // end while
} // end for
} // end if
} // end myMethod(int)
// TODO: Add
// Following
// commented
/*
...
...
String s =
*/
Comment Here
code has been
out
“Hello World!”;
DIV AX, 2 ; Divide by 2
24
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
Readable Code
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
Less Comments in the Readable Code
•
•
•
•
•
•
26
Know the problem domain
Normal conditions should follow error conditions
Refactor
Remove unused code
Learn multiple languages
Comments only for complex logic and generating the document
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
An Example
Original Code
27
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
An Example
Original Code
28
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
An Example
Original Code – With Comments
29
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
An Example
Original Code – With Comments
30
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
An Example
Modified Code
31
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
An Example
Modified Code
32
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
Connecting the Dots
Writing Quality Code
33
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
Q&A
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.
Thank you
© Copyright 2015 Hewlett-Packard Development Company, L.P. Other trademarks are registered trademarks and the properties of their respective owners.