The top 5 JavaScript
issues in all our codebases
The top 5 JavaScript
issues in all our codebases
The top 5 JavaScript
issues in all our codebases
Phil Nash
Developer Advocate for Sonar
@philnash
Phil Nash
Phil Nash
Phil Nash
twitter.com/philnash
@philnash@mastodon.social
linkedin.com/in/philnash
https://philna.sh
@philnash
The data
The data
The data
@philnash
JavaScript
Issues
JavaScript
Issues
JavaScript
Issues
@philnash
Clean Code
Clean Code
Clean Code
@philnash
John F. Woods - 1991 on
Always code as if the guy who ends up
maintaining your code will be a violent
psychopath who knows where you live.
Code for readability.
comp.lang.c++
@philnash
The comma operator
The comma operator
The comma operator
if (condition)
avar = value, anothervar = anothervalue;
@philnash
The comma operator
The comma operator
The comma operator
if (condition)
avar = value; anothervar = anothervalue;
@philnash
Raymond Chen -
Design for readability. Even if you don't
intend anybody else to read your code,
there's still a very good chance that
somebody will have to stare at your
code and figure out what it does: That
person is probably going to be you,
twelve months from now.
The Old New Thing Microsoft Blog
@philnash
Clean Code
Clean Code
Clean Code
It runs better in production and
it is better to work with in development
@philnash
So what are the top 5
issues?
So what are the top 5
issues?
So what are the top 5
issues?
@philnash
So what are the top 5 issues?
So what are the top 5 issues?
So what are the top 5 issues?
JavaScript
1.
2.
3.
4.
5.
@philnash
So what are the top 5 issues?
So what are the top 5 issues?
So what are the top 5 issues?
JavaScript
1.
2.
3.
4.
5. Unused assignments should be removed
@philnash
Unused assignments should be
removed
Unused assignments should be
removed
Unused assignments should be
removed
foo = bar + baz;
foo = otherFunction();
1
2
foo = bar + baz;
1
foo = otherFunction();
2
@philnash
Unused assignments should be
removed
Unused assignments should be
removed
Unused assignments should be
removed
function add(a, b) {
const result = a + b;
const foo = "hello";
return result;
}
@philnash
Unused assignments should be
removed
Unused assignments should be
removed
Unused assignments should be
removed
function add(a, b) {
return result = a + b;
}
@philnash
So what are the top 5 issues?
So what are the top 5 issues?
So what are the top 5 issues?
JavaScript
1.
2.
3.
4.
5. Unused assignments should be removed
@philnash
So what are the top 5 issues?
So what are the top 5 issues?
So what are the top 5 issues?
JavaScript
1.
2.
3.
4. "===" and "!==" instead of "==" and "!="
5. Unused assignments should be removed
@philnash
==
==
==
==
https://dorey.github.io/JavaScript-Equality-Table/
===
===
===
===
https://dorey.github.io/JavaScript-Equality-Table/
So what are the top 5 issues?
So what are the top 5 issues?
So what are the top 5 issues?
JavaScript
1.
2.
3.
4. "===" and "!==" instead of "==" and "!="
5. Unused assignments should be removed
@philnash
So what are the top 5 issues?
So what are the top 5 issues?
So what are the top 5 issues?
JavaScript
1.
2.
3. Sections of code should not be commented out
4. "===" and "!==" instead of "==" and "!="
5. Unused assignments should be removed
@philnash
Sections of code should not be
commented out
Sections of code should not be
commented out
Sections of code should not be
commented out
@philnash
So what are the top 5 issues?
So what are the top 5 issues?
So what are the top 5 issues?
JavaScript
1.
2.
3. Sections of code should not be commented out
4. "===" and "!==" instead of "==" and "!="
5. Unused assignments should be removed
@philnash
So what are the top 5 issues?
So what are the top 5 issues?
So what are the top 5 issues?
JavaScript
1. Use "let" or "const" instead of "var"
2.
3. Sections of code should not be commented out
4. "===" and "!==" instead of "==" and "!="
5. Unused assignments should be removed
@philnash
const and let
communicate
const and let
communicate
const and let
communicate
@philnash
So what are the top 5 issues?
So what are the top 5 issues?
So what are the top 5 issues?
JavaScript
1. Use "let" or "const" instead of "var"
2.
3. Sections of code should not be commented out
4. "===" and "!==" instead of "==" and "!="
5. Unused assignments should be removed
@philnash
So what are the top 5 issues?
So what are the top 5 issues?
So what are the top 5 issues?
JavaScript
1. Use "let" or "const" instead of "var"
2. Cognitive Complexity of functions should not
be too high
3. Sections of code should not be commented out
4. "===" and "!==" instead of "==" and "!="
5. Unused assignments should be removed
@philnash
Cognitive Complexity
Cognitive Complexity
Cognitive Complexity
🤷‍♂️
🤷‍♂️
🤷‍♂️
@philnash
Cyclomatic Complexity
Cyclomatic Complexity
Cyclomatic Complexity
function sumOfPrimes(max) { // +1
let total = 0;
for (let i = 2; i <= max; ++i) { // +1
let prime = true;
for (let j = 2; j < i; ++j) { // +1
if (i % j == 0) { // +1
prime = false;
}
}
if (prime) { // +1
total += i;
}
}
return total;
}
@philnash
Cyclomatic Complexity
Cyclomatic Complexity
Cyclomatic Complexity
export function getWords(number) { // +1
switch (number) {
case 1: // +1
return "one";
case 2: // +1
return "a couple";
case 3: // +1
return "a few";
case 4: // +1
return "many";
default:
return "lots";
}
}
@philnash
Complexity
Complexity
Complexity
Cyclomatic complexity measures the number of paths
through a function
Cognitive complexity measures how easy it is to read a
function
@philnash
Cognitive Complexity
Cognitive Complexity
Cognitive Complexity
Creates a score by:
Incrementing for breaks in flow (loops/conditionals)
Incrementing a nesting score
@philnash
Cognitive Complexity
Cognitive Complexity
Cognitive Complexity
function sumOfPrimes(max) {
let total = 0;
for (let i = 2; i <= max; ++i) { // +1
let prime = true;
for (let j = 2; j < i; ++j) { // +2
if (i % j == 0) { // +3
prime = false;
}
}
if (prime) { // +2
total += i;
}
}
return total;
}
@philnash
Cognitive Complexity
Cognitive Complexity
Cognitive Complexity
function getWords(number) {
switch (number) { // +1
case 1:
return "one";
case 2:
return "a couple";
case 3:
return "a few";
case 4:
return "many";
default:
return "lots";
}
}
@philnash
Cognitive Complexity of
functions should not be
too high
Cognitive Complexity of
functions should not be
too high
Cognitive Complexity of
functions should not be
too high
@philnash
Example
Example
Example
@philnash
Cognitive Complexity
Cognitive Complexity
Cognitive Complexity
@philnash
So what are the top 5 issues?
So what are the top 5 issues?
So what are the top 5 issues?
JavaScript
1. Use "let" or "const" instead of "var"
2. Cognitive Complexity of functions should not
be too high
3. Sections of code should not be commented out
4. "===" and "!==" instead of "==" and "!="
5. Unused assignments should be removed
@philnash
Tools
Tools
Tools
/
SonarCloud SonarQube
SonarLint
ESLint
eslint-plugin-sonarjs
@philnash
Other links
Other links
Other links
- Raymond Chen
Cognitive Complexity paper
Code is read much more often than it is written, so
plan accordingly
Sonar JavaScript Rules
🎉Reveal.js Confetti 🎉
@philnash
Clean Code
Clean Code
Clean Code
Clean as you code
Refactor complex functions
Code for readability
Code for future self
@philnash
Thank you
Thank you
Thank you
twitter.com/philnash
@philnash@mastodon.social
linkedin.com/in/philnash
https://philna.sh

The top 5 JavaScript issues in all our codebases

  • 1.
    The top 5JavaScript issues in all our codebases The top 5 JavaScript issues in all our codebases The top 5 JavaScript issues in all our codebases Phil Nash Developer Advocate for Sonar
  • 2.
  • 3.
    Phil Nash Phil Nash PhilNash twitter.com/philnash @philnash@mastodon.social linkedin.com/in/philnash https://philna.sh @philnash
  • 4.
    The data The data Thedata @philnash
  • 5.
  • 6.
  • 7.
    John F. Woods- 1991 on Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. Code for readability. comp.lang.c++ @philnash
  • 8.
    The comma operator Thecomma operator The comma operator if (condition) avar = value, anothervar = anothervalue; @philnash
  • 9.
    The comma operator Thecomma operator The comma operator if (condition) avar = value; anothervar = anothervalue; @philnash
  • 10.
    Raymond Chen - Designfor readability. Even if you don't intend anybody else to read your code, there's still a very good chance that somebody will have to stare at your code and figure out what it does: That person is probably going to be you, twelve months from now. The Old New Thing Microsoft Blog @philnash
  • 11.
    Clean Code Clean Code CleanCode It runs better in production and it is better to work with in development @philnash
  • 12.
    So what arethe top 5 issues? So what are the top 5 issues? So what are the top 5 issues? @philnash
  • 13.
    So what arethe top 5 issues? So what are the top 5 issues? So what are the top 5 issues? JavaScript 1. 2. 3. 4. 5. @philnash
  • 14.
    So what arethe top 5 issues? So what are the top 5 issues? So what are the top 5 issues? JavaScript 1. 2. 3. 4. 5. Unused assignments should be removed @philnash
  • 15.
    Unused assignments shouldbe removed Unused assignments should be removed Unused assignments should be removed foo = bar + baz; foo = otherFunction(); 1 2 foo = bar + baz; 1 foo = otherFunction(); 2 @philnash
  • 16.
    Unused assignments shouldbe removed Unused assignments should be removed Unused assignments should be removed function add(a, b) { const result = a + b; const foo = "hello"; return result; } @philnash
  • 17.
    Unused assignments shouldbe removed Unused assignments should be removed Unused assignments should be removed function add(a, b) { return result = a + b; } @philnash
  • 18.
    So what arethe top 5 issues? So what are the top 5 issues? So what are the top 5 issues? JavaScript 1. 2. 3. 4. 5. Unused assignments should be removed @philnash
  • 19.
    So what arethe top 5 issues? So what are the top 5 issues? So what are the top 5 issues? JavaScript 1. 2. 3. 4. "===" and "!==" instead of "==" and "!=" 5. Unused assignments should be removed @philnash
  • 20.
  • 21.
  • 22.
    So what arethe top 5 issues? So what are the top 5 issues? So what are the top 5 issues? JavaScript 1. 2. 3. 4. "===" and "!==" instead of "==" and "!=" 5. Unused assignments should be removed @philnash
  • 23.
    So what arethe top 5 issues? So what are the top 5 issues? So what are the top 5 issues? JavaScript 1. 2. 3. Sections of code should not be commented out 4. "===" and "!==" instead of "==" and "!=" 5. Unused assignments should be removed @philnash
  • 24.
    Sections of codeshould not be commented out Sections of code should not be commented out Sections of code should not be commented out @philnash
  • 25.
    So what arethe top 5 issues? So what are the top 5 issues? So what are the top 5 issues? JavaScript 1. 2. 3. Sections of code should not be commented out 4. "===" and "!==" instead of "==" and "!=" 5. Unused assignments should be removed @philnash
  • 26.
    So what arethe top 5 issues? So what are the top 5 issues? So what are the top 5 issues? JavaScript 1. Use "let" or "const" instead of "var" 2. 3. Sections of code should not be commented out 4. "===" and "!==" instead of "==" and "!=" 5. Unused assignments should be removed @philnash
  • 27.
    const and let communicate constand let communicate const and let communicate @philnash
  • 28.
    So what arethe top 5 issues? So what are the top 5 issues? So what are the top 5 issues? JavaScript 1. Use "let" or "const" instead of "var" 2. 3. Sections of code should not be commented out 4. "===" and "!==" instead of "==" and "!=" 5. Unused assignments should be removed @philnash
  • 29.
    So what arethe top 5 issues? So what are the top 5 issues? So what are the top 5 issues? JavaScript 1. Use "let" or "const" instead of "var" 2. Cognitive Complexity of functions should not be too high 3. Sections of code should not be commented out 4. "===" and "!==" instead of "==" and "!=" 5. Unused assignments should be removed @philnash
  • 30.
    Cognitive Complexity Cognitive Complexity CognitiveComplexity 🤷‍♂️ 🤷‍♂️ 🤷‍♂️ @philnash
  • 31.
    Cyclomatic Complexity Cyclomatic Complexity CyclomaticComplexity function sumOfPrimes(max) { // +1 let total = 0; for (let i = 2; i <= max; ++i) { // +1 let prime = true; for (let j = 2; j < i; ++j) { // +1 if (i % j == 0) { // +1 prime = false; } } if (prime) { // +1 total += i; } } return total; } @philnash
  • 32.
    Cyclomatic Complexity Cyclomatic Complexity CyclomaticComplexity export function getWords(number) { // +1 switch (number) { case 1: // +1 return "one"; case 2: // +1 return "a couple"; case 3: // +1 return "a few"; case 4: // +1 return "many"; default: return "lots"; } } @philnash
  • 33.
    Complexity Complexity Complexity Cyclomatic complexity measuresthe number of paths through a function Cognitive complexity measures how easy it is to read a function @philnash
  • 34.
    Cognitive Complexity Cognitive Complexity CognitiveComplexity Creates a score by: Incrementing for breaks in flow (loops/conditionals) Incrementing a nesting score @philnash
  • 35.
    Cognitive Complexity Cognitive Complexity CognitiveComplexity function sumOfPrimes(max) { let total = 0; for (let i = 2; i <= max; ++i) { // +1 let prime = true; for (let j = 2; j < i; ++j) { // +2 if (i % j == 0) { // +3 prime = false; } } if (prime) { // +2 total += i; } } return total; } @philnash
  • 36.
    Cognitive Complexity Cognitive Complexity CognitiveComplexity function getWords(number) { switch (number) { // +1 case 1: return "one"; case 2: return "a couple"; case 3: return "a few"; case 4: return "many"; default: return "lots"; } } @philnash
  • 37.
    Cognitive Complexity of functionsshould not be too high Cognitive Complexity of functions should not be too high Cognitive Complexity of functions should not be too high @philnash
  • 38.
  • 39.
  • 40.
    So what arethe top 5 issues? So what are the top 5 issues? So what are the top 5 issues? JavaScript 1. Use "let" or "const" instead of "var" 2. Cognitive Complexity of functions should not be too high 3. Sections of code should not be commented out 4. "===" and "!==" instead of "==" and "!=" 5. Unused assignments should be removed @philnash
  • 41.
  • 42.
    Other links Other links Otherlinks - Raymond Chen Cognitive Complexity paper Code is read much more often than it is written, so plan accordingly Sonar JavaScript Rules 🎉Reveal.js Confetti 🎉 @philnash
  • 43.
    Clean Code Clean Code CleanCode Clean as you code Refactor complex functions Code for readability Code for future self @philnash
  • 44.
    Thank you Thank you Thankyou twitter.com/philnash @philnash@mastodon.social linkedin.com/in/philnash https://philna.sh