if (condition)
avar = value, anothervar = anothervalue;
The comma operator
if (condition)
avar = value; anothervar = anothervalue;
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.
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
So what are the top 5 issues?
A list of top issues for TypeScript
TypeScript
1.
Unnecessary imports should be removed
2.
Sections of code should not be commented out
3.
Cognitive Complexity of functions should not be too high
4.
Disallow .bind() and => functions in JSX props
5.
Track uses of "TODO" tags
Cognitive 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;
}
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";
}
}
Complexity
Cyclomatic complexity measures the number of paths through a
function
Cognitive complexity measures how easy it is to read a function
Cognitive Complexity
Creates a score by:
Incrementing for breaks in flow (loops/conditionals)
Incrementing a nesting score
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;
}
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";
}
}
Cognitive Complexity of functions should not be too high
Example
Cognitive Complexity
So what are the top 5 issues?
A list of top issues for JavaScript
JavaScript
1.
Use "let" or "const" instead of "var"
2.
Cognitive Complexity of functions should not be too high