Analysing code quality
Linters and Static Analysis
Hello!
Weverton Timoteo
@wevtimoteo
“
Any fool can write code that a
computer can understand.
Good programmers write code that
humans can understand.
– Martin Fowler
How Do You Define
“Good Code” ?
maintainability?
Efficiency Modularity Elegance
What's the first thing that comes to your
mind?
Proper design
decision
Error rate
Comprehensibility
Reviewability
Debugging
External quality
Modifiability
Development time
You write readable code because it helps
other people to read your code
“
Code Complete, Steve McConnel
Simplicity
is it about the number of lines?
Readability
comments, conventions, naming
Modularity
its all about reusability
Layering
attention to the boundaries of each layer
Design
plan before build
Efficiency
too many connections?
Elegance
solve problems with proper solutions
Clarity
do you really know what you have to do?
Which one matters
most?
Balance
How to achieve
this?
is a tool that analyzes source code to flag
programming errors, bugs, stylistic errors,
and suspicious constructs
- Wikipedia
What is a linter?
“
Lexer and Parser
● Lexer: converts the statements in code into various
categories of like keywords, constants, variable in a
sentence and produce token
● Parser: takes the tokens produced by the lexer and tries
to determine whether these statements are semantically
correct
Packages - Std lib
scanner token astparser
Abstract Syntax Tree (AST)
Abstract Syntax Tree (AST)
Abstract Syntax Tree (AST)
Abstract Syntax Tree (AST)
Abstract Syntax Tree (AST)
Homoiconicity
● The code used to express a program is
written using the data structures of
that language
● The Lisp family languages (Common
Lisp, Scheme, Clojure, etc)
● Code is Data, Data is Code
● gofmt - Must have for every project
● goimports - Additionally to gofmt, checks unused imports
● unindent - Report code that is unnecessarily indented
Code Formatting
gofmt -s (Simplify)
goimports
● abcgo - ABC metrics for Go source code.
● gocyclo - Computes and checks the cyclomatic
complexity of functions.
● splint - It finds any functions that are too long or have too
many parameters or results.
Code Complexity
ABC Metrics - Calculate Score
● Assignment count when:
=, *=, /=, %=, +=, <<=, >>=,
&=, ^=
++, --
● Branch count when:
Function call
● Condition count when:
<, >, <=, >=, ==, !=
else, case
ABC Metrics (abcgo)
● GoLint - Golint is a linter for Go source code
● revive - Drop-in replacement of golint
● go-namecheck - helps you to maintain variable/field
naming conventions inside your project
● lll - Line length linter, used to enforce line length in files
Style and Patterns Checking
golint
Revive
● Allows to enable, disable, configure
rules using a TOML file
● Allows disabling a specific rule or
the entire linter for a file or a range
of lines
● Provides multiple formatters which
let us customize the output
Revive - Configuration
Revive - Custom Rule
Revive - Custom Rule
Revive - Custom Rule
● govet - checks correctness: a vet check identifies real or
potential bugs that could cause incorrect compilation or
execution
● badtime - Badtime is a Golang linter that detects
inappropriate usage of the time.Time struct.
● safesql - Static analysis tool for Golang that protects
against SQL injections.
Bugs
govet
● Performance
● Unused code
● Reports
● Outdated packages
● Code without tests (no coverage)
● Magic number detector
What else?
How should I start?
Thanks!
Any questions?
Which linters are you
using?
Tell me! @wevtimoteo
References
● Code Complete: A Practical Handbook of Software Construction
● Effective Go
● golang/lint Repository
● SourceLevel: Team Metrics and Automated Code Review
● How do you define “Good Code”?
● Awesome Go Linters
● scanner Package
● Basic AST Manipulation in Go
● Internal vs External Software Quality
References
● Code Complete: A Practical Handbook of Software Construction
● Lexical Scanning in Go - Rob Pike

[Gophercon 2019] Analysing code quality with linters and static analysis