Code quality is not an exact science and is rather subjective, which brings the need of well defined rules and principles to follow.
Clean code is all about readability, furthermore, principles like GRASP or techniques like fluent APIs makes the code even cleaner and maintainable.
Design Patterns on the other hand are typical solutions to common problems in software design.
You will walk away with a taste of quality principles and metrics for building quality software.
Also previews of some useful books like Martin Fowler's Clean Code and Kent Beck's Implementation Patterns are presented.
1. 1Caring About Code Quality
Caring About Code
Quality
EL MAHDI BENZEKRI | elmahdi.benzekri@gmail.com
2019
2. 2Caring About Code Quality
Clean Code: A Handbook of Agile Software Craftsmanship
by Robert C. Martin
The Book
3. 3Caring About Code Quality
Does it work?
What is Clean Code?
Does it scale?
Is it esthetic?
Is it maintainable?
Is it testable?
Is it short?
Is it clever?
Does it have explanatory
comments?
Is it easy to
understand?
Is it well-structured?
Is it object-oriented?
4. 4Caring About Code Quality
• Focused
• Single-minded attitude
• Undistracted and unpolluted
• Readable, simple and direct
• Compact and literate
• Contains only what is necessary
• Makes it easy for other developers to enhance it
• Tests should be also clean
• Looks like it’s author cares
• FoContains no duplicates
• undations are established on tiny abstractions
What is Clean Code after all?
5. 5Caring About Code Quality
• Subjective
• If you can’t measure, you can’t improve
• Identify what matters
• Defend and justify decisions.
The need for measurement
6. 6Caring About Code Quality
Cyclomatic complexity
public void foo() {
if (c1) {
f1();
} else {
f2();
}
if (c2) {
f3();
} else {
f4();
}
}
• Upper bound for the number of test cases
that are necessary to achieve a complete
branch coverage
• Lower bound for the number of paths
through the control flow graph
• Desirable value: below 10
Cyclomatic complexity: 3
7. 7Caring About Code Quality
Always leave the campground
cleaner than you found it.
The Boy Scout Rule
8. 8Caring About Code Quality
• Came from city crime researchers
• A broken window will trigger a building into a
smashed and abandoned derelict
• So does the software
Broken Window Theory
9. 9Caring About Code Quality
Composed method example
public void add(Object element) {
if (!readOnly) {
int newSize = size + 1;
if (newSize > elements.length) {
Object[] newElements = new Object[elements.length + 10];
for (int index = 0; index < size; index++) {
newElements[index] = elements[index];
elements = newElements;
}
}
elements[size++] = element;
}
}
11. 11Caring About Code Quality
Composed method example
public void add(Object element) {
if (!isReadOnly()) {
if (atCapacity()) {
grow();
}
addElement(element);
}
}
12. 12Caring About Code Quality
• Instant feedback
• Allows you to make changes to code quickly
• Help you understand the design of the code you
are working on
• Writing testable code helps to achieve better
code quality
• Unit tests are a form of sample code
• Test-first forces you to plan before you code
Advantages of Unit tests
13. 13Caring About Code Quality
• Whether the unit you're testing should be
isolated from its collaborators
• Mockist approach
– Isolate from dependencies
– More flexible in what you can test
• Classic approach
– No attempt to isolate unless communicating
with the collaborator is awkward
– Less brittle tests
Unit tests collaborator isolation
14. 14Caring About Code Quality
• Change Risk Analysis and Prediction
• Help you identify code that might be particularly difficult to
understand, test, or maintain
• 𝐶𝑅𝐴𝑃 𝑚 = cycl_comp(m)2
∗ 1 − 𝑐𝑜𝑣𝑒𝑟𝑎𝑔𝑒
3
+ cycl_comp(m)
• A method with a CRAP score over 30 is considered
unacceptable
C.R.A.P. metric
Cyclomatic
Complexity
Coverage
required
0 – 5 0%
10 42%
15 57%
20 71%
25 80%
30 100%
31+ Not possible
15. 15Caring About Code Quality
• Don’t build complex machines
• Don’t build frameworks
• Make the software so simple that there are
obviously no deficiencies
• Keep it simple, stupid (KISS)
Rube Goldberg Machines
16. 16Caring About Code Quality
• Avoid Cargo cult programming
• Question authority
• Accept feedback and give feedback
Angry monkeys experiment
26. 26Caring About Code Quality
• State Pattern’s intent is to manage states of the object along with object’s behavior which changes
with its state.
• State typically involves a getStatus method and a reference back to the context
• state, behavior is autodeterminated, strategy, behavior is determinated by caller.
• Strategy Pattern is to have a family of interchangeable algorithms
• Relation between individual states/individual strategies
State vs Strategy
31. 31Caring About Code Quality
Effective Java
• Level of access ? Minimum
• Classpath ?
• Composition vs inheritance
• Avoid side effects
• Minimize Mutability
• Check methods/constructosr parameters for validity
• Always override hashCode when you override equals
• …
• https://github.com/HugoMatilla/Effective-JAVA-Summary
32. 32Caring About Code Quality
• “This” keyword
• Chained
• Popular java Fluent API’s:
– Java Object Oriented Querying (jOOQ) API
– Java 8’s Date Time API
– Java 9’s Money and Currency API
Fluent API
33. 33Caring About Code Quality
• Handle UI interractions
GRASP - The Controller
34. 34Caring About Code Quality
• Information expert
• Encapsulating information
GRASP - The Expert
35. 35Caring About Code Quality
• Creation responsibility
• Have enough information to instantiate an object
GRASP - The Creator
36. 36Caring About Code Quality
• it is a measure of how strongly related each
piece of functionality expressed by
the source code of a software module is.
GRASP - High cohesion
37. 37Caring About Code Quality
• The smallest number of references
between objects as possible
• Reason of change
GRASP - Low coupling
38. 38Caring About Code Quality
EMBZENZEKRI@SQLI.COM
THANK YOU FOR ATTENTION!
QUESTIONS?