Tech talk on code quality
-
Upload
alexander-osin -
Category
Software
-
view
54 -
download
0
Transcript of Tech talk on code quality
Code quality.
Why code quality?
Development team• Velocity
Product
• Bugs count• Time to fix• Performance• Security
Software Engineer
• Personal comfort• Motivation
Why it matters?
Average mood
understandabilityis what we need.
What we can do?
• Write better new code
• Improve existing code
print(“Hello World!”)cut.
do not increase.
Program size
• Function size
• Class size
• File size
• Package size
cut.do not increase.
Code style
style #1
var result = [];
for (var i = 0; i < l; i += 1) {
if (i % 2) {
result.push(data[i]);
}
}
style #2
data.filter(filters.modal)
Unclear responsibility
function hakuna_matata(a, b, next) {
if (!b) {
return a;
}
if (!a && !next) {
return b;
}
return “Hakuna matata!”;
}
Architectural issues
Why it happens?qualification, dude.
We don’t do what is needed!
What was estimated
What we wanna built
What we have done
Manage your time.
Improve existing code.80 - 20
Pareto principle.
Measure!
Lines of code - LOC
for (i = 0; i < 100; i++) printf("hello"); /* How many lines of code is this? */
Logical lines of code - LLOC
/* Now how many lines of code is this? */
for (i = 0; i < 100; i++)
{
printf("hello");
}
Number of lines of code
Simple code
function sayHello() {
print(“Hello”)
}
Difficult code
function say(message) {
if (!message) {
return false;
}
if (message == “Hello”) {
print(“Hello World!”);
return;
}
message && print(message);
}
Cyclomatic complexity
Difficult code
function say(message) {
if (!message) {
return false;
}
if (message == “Hello”) {
print(“Hello World!”)
return;
}
print(message)
}
✓ 3 tests passed
100% coverage
say() == false
say(“Hello”) == “Hello World!”
say(“Text”) && output == “Text”
Code coverage
Other metrics• Cohesion• Coupling• Connascence• DSQI• Maintainability index• Comment density• Halstead complexity• Number of classes• Number of parameters• Number of dependencies• Execution time• Load time…
SonarQube
Plato
Code analysis platforms
node install complexity-report
LLOC Complexity Density
5440 703 13 %
5864 1448 25 %
11739 2132 18 %
965 275 28 %
Metrics example
Metrics example #2
git log --name-only -- *.js | grep .js | sort | uniq -c | sort -n
git diff --stat `git rev-list HEAD | tail -n 1` HEAD | grep .js | awk '{ print $3 " "$4 " "
$1}' | sort -n
Version control system
Let’s make our life better!that’s all.
Average mood