Code
Quality
Automatizando a Revisão do Código
Monica Lent


?
O que?
O Problema?
Como Melhorar?
Andrei Calazans



@AndreiCalazans
@Andrei_Calazans
g2i.co





O QUE VOCÊ REVISA?
Google's Engineering Practices
Design
Functionality
Complexidade
Testes
Nomeclatura
Comentários & Documentação
Linha por linha & Contexto & Boas coisas
- Testes
- Legibilidade & Complexidade
- Errors
- Regras & Padrões
Qual o Problema?


















Emocional
Lento
Fragil
Informação do codebase retrista à pessoas.
1
TESTES
Cobertura

coverageThreshold
coverageThreshold: {
global: {
branches: 100,
functions: 100,
lines: 100,
statements: -10
}
},
2
LEGIBILIDADE & COMPLEXIDADE
Thomas J. McCabe’s Cyclomatic Complexity - 1976
Sonar JS
- Cognitive Complexity of functions should not be too high (cognitive-complexity)
- "switch" statements should not have too many "case" clauses (max-switch-cases)
- Collapsible "if" statements should be merged (no-collapsible-if)
- String literals should not be duplicated (no-duplicate-string)
- Two branches in a conditional structure should not have exactly the same implementation (no-duplicated-branches)
- Functions should not have identical implementations (no-identical-functions)
- Boolean checks should not be inverted (no-inverted-boolean-check)
- Boolean literals should not be redundant (no-redundant-boolean)
- "switch" statements should have at least 3 "case" clauses (no-small-switch)
- "catch" clauses should do more than rethrow (no-useless-catch)
- Local variables should not be declared and then immediately returned or thrown (prefer-immediate-return) (🔧 fixable)
- Object literal syntax should be used (prefer-object-literal)
- Return of boolean expressions should not be wrapped into an "if-then-else" statement (prefer-single-boolean-return)
- A "while" loop should be used instead of a "for" loop (prefer-while) (🔧 fixable)
Sonar JS - No Identical functions

Sonar JS

Cognitive complexity
Cognitive complexity


Declarative code
No loops?
Enforce Functional Style? (eslint-plugin-functional)
// eslint-disable-next-line no-loops/no-loops
for (let i = 0; i < arr.length; i++) {
// ...
}
Spell Checking


JSDOC
Docz
StoryBook
3
ERRORS
ESLINT
STATIC TYPER

Impact Surface
4
REGRAS & PADRÕES
Testando Arquitetura
Estrutura & Nome
Forbidden Dependecy test



{
"name": "from-svod-to-avod",
"comment": "we can not import from avod when in svod",
"severity": "error",
"from": {
"path": "svod"
},
"to": {
"path": "avod"
}
},
dependency-cruiser
Dependency Graph

Reutilizando & Duplicação
JSCP

jscpd apps/svod/src/mobile



ACABOU?

INDO ALÉM
ESLINT CUSTOM PLUGINS
AST

function foo(d) {
d += 3;
return d+999
}
function bar(d) {
return d*100
}
ESPREE-BASED AST

ESLINT =
ESPREE + ESTRAVERSE


ASTEXPLORER
Custom Plugin







ESLINT SIMPLE


ONDE TEM MAIS?
O que o programa não pega?
- Over-engineering
- Otimização prematura
- É uma mudança necessária?
- Está completo?
- Esta bem feito?
Referências
- Kent writing custom Babel and ESLint plugins with ASTs(Open West 2017).
- Yonatan Mevorach's Master the Art of the AST and Take Control of Your JS.
- SonarJS: How To Build a Static Code Analyzer - Elena Vilchik & Carlo Bottiglieri
- Automated Code Analysis in Salesforce
- eslint-plugin-spellcheck
- Verfication vs validation
- eslint-simple-rule
- Static versus Dynamic code analysis
- JSCPD
- JSINSPECT
- What color is your function?
Referências
12. AST picture: https://sites.psu.edu/robertbcolton/tag/abstract-syntax-tree/
14. Parser API: https://developer.mozilla.org/en-US/docs/Mozilla/Projects/SpiderMonkey/Parser_API
15. estree https://github.com/estree/estree
16. eslint simple https://github.com/humanwhocodes/eslint-simple-rule#readme
Obrigado!

Code Quality: Automatizando a revisão do código.
By Andrei Calazans
Code Quality: Automatizando a revisão do código.
- 1,686