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

Weverton Timoteo
Weverton TimoteoCTO at SourceLevel
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
[Gophercon 2019] Analysing code quality with linters and static analysis
● 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
[Gophercon 2019] Analysing code quality with linters and static analysis
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
1 of 49

Recommended

Best Practices of Software Development by
Best Practices of Software DevelopmentBest Practices of Software Development
Best Practices of Software DevelopmentFolio3 Software
2.8K views22 slides
Software development best practices & coding guidelines by
Software development best practices & coding guidelinesSoftware development best practices & coding guidelines
Software development best practices & coding guidelinesAnkur Goyal
5K views34 slides
Coding standards and guidelines by
Coding standards and guidelinesCoding standards and guidelines
Coding standards and guidelinesbrijraj_singh
8.2K views7 slides
Coding standards by
Coding standardsCoding standards
Coding standardsMimoh Ojha
6.5K views46 slides
Coding standard by
Coding standardCoding standard
Coding standardFAROOK Samath
3.1K views71 slides
Euro python 2015 writing quality code by
Euro python 2015   writing quality codeEuro python 2015   writing quality code
Euro python 2015 writing quality coderadek_j
1.9K views53 slides

More Related Content

What's hot

Code review by
Code reviewCode review
Code reviewdqpi
2K views19 slides
Coding conventions by
Coding conventionsCoding conventions
Coding conventionssystemcrashed
2.7K views35 slides
Quality metrics and angular js applications by
Quality metrics and angular js applicationsQuality metrics and angular js applications
Quality metrics and angular js applicationsnadeembtech
6K views34 slides
Code Inspection by
Code InspectionCode Inspection
Code InspectionFáber D. Giraldo
651 views21 slides
Coding standard and coding guideline by
Coding standard and coding guidelineCoding standard and coding guideline
Coding standard and coding guidelineDhananjaysinh Jhala
449 views12 slides
Coding standards by
Coding standardsCoding standards
Coding standardsMark Reynolds
1.3K views42 slides

What's hot(20)

Code review by dqpi
Code reviewCode review
Code review
dqpi2K views
Quality metrics and angular js applications by nadeembtech
Quality metrics and angular js applicationsQuality metrics and angular js applications
Quality metrics and angular js applications
nadeembtech6K views
The pseudocode by Asha Sari
The pseudocodeThe pseudocode
The pseudocode
Asha Sari3.3K views
Reading Notes : the practice of programming by Juggernaut Liu
Reading Notes : the practice of programmingReading Notes : the practice of programming
Reading Notes : the practice of programming
Juggernaut Liu714 views
Data Generation with PROSPECT: a Probability Specification Tool by Ivan Ruchkin
Data Generation with PROSPECT: a Probability Specification ToolData Generation with PROSPECT: a Probability Specification Tool
Data Generation with PROSPECT: a Probability Specification Tool
Ivan Ruchkin116 views
Episode 1 - PathToCode.com by Jitendra Zaa
Episode 1 - PathToCode.comEpisode 1 - PathToCode.com
Episode 1 - PathToCode.com
Jitendra Zaa5.8K views
Fundamentals of programming) by jakejakejake2
Fundamentals of programming)Fundamentals of programming)
Fundamentals of programming)
jakejakejake2584 views
Elixir Brasil 2019 - Quality: A Panacéia para seu código Elixir by Weverton Timoteo
Elixir Brasil 2019 - Quality:  A Panacéia para seu código ElixirElixir Brasil 2019 - Quality:  A Panacéia para seu código Elixir
Elixir Brasil 2019 - Quality: A Panacéia para seu código Elixir
Weverton Timoteo532 views
Code quality by Provectus
Code qualityCode quality
Code quality
Provectus187 views
Standard Coding, OOP Techniques and Code Reuse by Rayhan Chowdhury
Standard Coding, OOP Techniques and Code ReuseStandard Coding, OOP Techniques and Code Reuse
Standard Coding, OOP Techniques and Code Reuse
Rayhan Chowdhury4.7K views
Ambiguous Requirements – Translating the message from C-level to implementation by Georgina Tilby
Ambiguous Requirements – Translating the message from C-level to implementationAmbiguous Requirements – Translating the message from C-level to implementation
Ambiguous Requirements – Translating the message from C-level to implementation
Georgina Tilby1.1K views

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

Floripa Gophers - Analysing Code Quality (Linters and Static Analysis) by
Floripa Gophers - Analysing Code Quality (Linters and Static Analysis)Floripa Gophers - Analysing Code Quality (Linters and Static Analysis)
Floripa Gophers - Analysing Code Quality (Linters and Static Analysis)Weverton Timoteo
320 views32 slides
Design Like a Pro: Scripting Best Practices by
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesInductive Automation
792 views52 slides
Design Like a Pro: Scripting Best Practices by
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesInductive Automation
1.4K views52 slides
Improving Code Quality Through Effective Review Process by
Improving Code Quality Through Effective  Review ProcessImproving Code Quality Through Effective  Review Process
Improving Code Quality Through Effective Review ProcessDr. Syed Hassan Amin
721 views32 slides
How to write good quality code by
How to write good quality codeHow to write good quality code
How to write good quality codeHayden Bleasel
882 views50 slides
Measuring Your Code by
Measuring Your CodeMeasuring Your Code
Measuring Your CodeNate Abele
1.6K views54 slides

Similar to [Gophercon 2019] Analysing code quality with linters and static analysis(20)

Floripa Gophers - Analysing Code Quality (Linters and Static Analysis) by Weverton Timoteo
Floripa Gophers - Analysing Code Quality (Linters and Static Analysis)Floripa Gophers - Analysing Code Quality (Linters and Static Analysis)
Floripa Gophers - Analysing Code Quality (Linters and Static Analysis)
Weverton Timoteo320 views
Improving Code Quality Through Effective Review Process by Dr. Syed Hassan Amin
Improving Code Quality Through Effective  Review ProcessImproving Code Quality Through Effective  Review Process
Improving Code Quality Through Effective Review Process
How to write good quality code by Hayden Bleasel
How to write good quality codeHow to write good quality code
How to write good quality code
Hayden Bleasel882 views
Measuring Your Code by Nate Abele
Measuring Your CodeMeasuring Your Code
Measuring Your Code
Nate Abele1.6K views
Measuring Your Code by Nate Abele
Measuring Your CodeMeasuring Your Code
Measuring Your Code
Nate Abele10 views
Measuring Your Code 2.0 by Nate Abele
Measuring Your Code 2.0Measuring Your Code 2.0
Measuring Your Code 2.0
Nate Abele1.4K views
How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018 by Mike Harris
How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018
How I Learned to Stop Worrying and Love Legacy Code - Ox:Agile 2018
Mike Harris32 views
Python - code quality and production monitoring by David Melamed
Python - code quality and production monitoringPython - code quality and production monitoring
Python - code quality and production monitoring
David Melamed3.3K views
NetWork - 15.10.2011 - Applied code generation in .NET by Dmytro Mindra
NetWork - 15.10.2011 - Applied code generation in .NET NetWork - 15.10.2011 - Applied code generation in .NET
NetWork - 15.10.2011 - Applied code generation in .NET
Dmytro Mindra318 views
EKON 23 Code_review_checklist by Max Kleiner
EKON 23 Code_review_checklistEKON 23 Code_review_checklist
EKON 23 Code_review_checklist
Max Kleiner259 views
How I Learned to Stop Worrying and Love Legacy Code..... by Mike Harris
How I Learned to Stop Worrying and Love Legacy Code.....How I Learned to Stop Worrying and Love Legacy Code.....
How I Learned to Stop Worrying and Love Legacy Code.....
Mike Harris473 views

More from Weverton Timoteo

[Ruby Summit Brasil 2020] Ruby 3 e Análise estática - O que esperar e o que s... by
[Ruby Summit Brasil 2020] Ruby 3 e Análise estática - O que esperar e o que s...[Ruby Summit Brasil 2020] Ruby 3 e Análise estática - O que esperar e o que s...
[Ruby Summit Brasil 2020] Ruby 3 e Análise estática - O que esperar e o que s...Weverton Timoteo
344 views47 slides
[Devs Cansados Evento 2020] Queria saber a qualidade do meu código: o que olho? by
[Devs Cansados Evento 2020] Queria saber a qualidade do meu código: o que olho?[Devs Cansados Evento 2020] Queria saber a qualidade do meu código: o que olho?
[Devs Cansados Evento 2020] Queria saber a qualidade do meu código: o que olho?Weverton Timoteo
89 views44 slides
[CodeBEAM BR 2020] Construindo e Distribuindo CLIs com Elixir by
[CodeBEAM BR 2020] Construindo e Distribuindo CLIs com Elixir[CodeBEAM BR 2020] Construindo e Distribuindo CLIs com Elixir
[CodeBEAM BR 2020] Construindo e Distribuindo CLIs com ElixirWeverton Timoteo
163 views27 slides
Ruby 3 e Análise estática - O que esperar e o que significa para o nosso código by
Ruby 3 e Análise estática - O que esperar e o que significa para o nosso códigoRuby 3 e Análise estática - O que esperar e o que significa para o nosso código
Ruby 3 e Análise estática - O que esperar e o que significa para o nosso códigoWeverton Timoteo
255 views42 slides
21o. RubyFloripa - Maintaining legacy Rails app and introducing Elixir by
21o. RubyFloripa - Maintaining legacy Rails app and introducing Elixir21o. RubyFloripa - Maintaining legacy Rails app and introducing Elixir
21o. RubyFloripa - Maintaining legacy Rails app and introducing ElixirWeverton Timoteo
522 views37 slides
How to use Ruby code inside Elixir by
How to use Ruby code inside ElixirHow to use Ruby code inside Elixir
How to use Ruby code inside ElixirWeverton Timoteo
182 views15 slides

More from Weverton Timoteo(16)

[Ruby Summit Brasil 2020] Ruby 3 e Análise estática - O que esperar e o que s... by Weverton Timoteo
[Ruby Summit Brasil 2020] Ruby 3 e Análise estática - O que esperar e o que s...[Ruby Summit Brasil 2020] Ruby 3 e Análise estática - O que esperar e o que s...
[Ruby Summit Brasil 2020] Ruby 3 e Análise estática - O que esperar e o que s...
Weverton Timoteo344 views
[Devs Cansados Evento 2020] Queria saber a qualidade do meu código: o que olho? by Weverton Timoteo
[Devs Cansados Evento 2020] Queria saber a qualidade do meu código: o que olho?[Devs Cansados Evento 2020] Queria saber a qualidade do meu código: o que olho?
[Devs Cansados Evento 2020] Queria saber a qualidade do meu código: o que olho?
Weverton Timoteo89 views
[CodeBEAM BR 2020] Construindo e Distribuindo CLIs com Elixir by Weverton Timoteo
[CodeBEAM BR 2020] Construindo e Distribuindo CLIs com Elixir[CodeBEAM BR 2020] Construindo e Distribuindo CLIs com Elixir
[CodeBEAM BR 2020] Construindo e Distribuindo CLIs com Elixir
Weverton Timoteo163 views
Ruby 3 e Análise estática - O que esperar e o que significa para o nosso código by Weverton Timoteo
Ruby 3 e Análise estática - O que esperar e o que significa para o nosso códigoRuby 3 e Análise estática - O que esperar e o que significa para o nosso código
Ruby 3 e Análise estática - O que esperar e o que significa para o nosso código
Weverton Timoteo255 views
21o. RubyFloripa - Maintaining legacy Rails app and introducing Elixir by Weverton Timoteo
21o. RubyFloripa - Maintaining legacy Rails app and introducing Elixir21o. RubyFloripa - Maintaining legacy Rails app and introducing Elixir
21o. RubyFloripa - Maintaining legacy Rails app and introducing Elixir
Weverton Timoteo522 views
How to draw a map - What is projections? by Weverton Timoteo
How to draw a map - What is projections?How to draw a map - What is projections?
How to draw a map - What is projections?
Weverton Timoteo410 views
Gerenciando dependências front-end no Rails by Weverton Timoteo
Gerenciando dependências front-end no RailsGerenciando dependências front-end no Rails
Gerenciando dependências front-end no Rails
Weverton Timoteo324 views
Definindo a quantidade de workers para sua app by Weverton Timoteo
Definindo a quantidade de workers para sua appDefinindo a quantidade de workers para sua app
Definindo a quantidade de workers para sua app
Weverton Timoteo274 views
Como definir a quantidade de workers para sua aplicação by Weverton Timoteo
Como definir a quantidade de workers para sua aplicaçãoComo definir a quantidade de workers para sua aplicação
Como definir a quantidade de workers para sua aplicação
Weverton Timoteo546 views
Swift language - A fast overview of some features by Weverton Timoteo
Swift language - A fast overview of some featuresSwift language - A fast overview of some features
Swift language - A fast overview of some features
Weverton Timoteo1.1K views

Recently uploaded

Upskilling the Evolving Workforce with Digital Fluency for Tomorrow's Challen... by
Upskilling the Evolving Workforce with Digital Fluency for Tomorrow's Challen...Upskilling the Evolving Workforce with Digital Fluency for Tomorrow's Challen...
Upskilling the Evolving Workforce with Digital Fluency for Tomorrow's Challen...NUS-ISS
23 views70 slides
TE Connectivity: Card Edge Interconnects by
TE Connectivity: Card Edge InterconnectsTE Connectivity: Card Edge Interconnects
TE Connectivity: Card Edge InterconnectsCXL Forum
96 views12 slides
Throughput by
ThroughputThroughput
ThroughputMoisés Armani Ramírez
32 views11 slides
Architecting CX Measurement Frameworks and Ensuring CX Metrics are fit for Pu... by
Architecting CX Measurement Frameworks and Ensuring CX Metrics are fit for Pu...Architecting CX Measurement Frameworks and Ensuring CX Metrics are fit for Pu...
Architecting CX Measurement Frameworks and Ensuring CX Metrics are fit for Pu...NUS-ISS
32 views54 slides
Business Analyst Series 2023 - Week 3 Session 5 by
Business Analyst Series 2023 -  Week 3 Session 5Business Analyst Series 2023 -  Week 3 Session 5
Business Analyst Series 2023 - Week 3 Session 5DianaGray10
165 views20 slides
MemVerge: Past Present and Future of CXL by
MemVerge: Past Present and Future of CXLMemVerge: Past Present and Future of CXL
MemVerge: Past Present and Future of CXLCXL Forum
110 views26 slides

Recently uploaded(20)

Upskilling the Evolving Workforce with Digital Fluency for Tomorrow's Challen... by NUS-ISS
Upskilling the Evolving Workforce with Digital Fluency for Tomorrow's Challen...Upskilling the Evolving Workforce with Digital Fluency for Tomorrow's Challen...
Upskilling the Evolving Workforce with Digital Fluency for Tomorrow's Challen...
NUS-ISS23 views
TE Connectivity: Card Edge Interconnects by CXL Forum
TE Connectivity: Card Edge InterconnectsTE Connectivity: Card Edge Interconnects
TE Connectivity: Card Edge Interconnects
CXL Forum96 views
Architecting CX Measurement Frameworks and Ensuring CX Metrics are fit for Pu... by NUS-ISS
Architecting CX Measurement Frameworks and Ensuring CX Metrics are fit for Pu...Architecting CX Measurement Frameworks and Ensuring CX Metrics are fit for Pu...
Architecting CX Measurement Frameworks and Ensuring CX Metrics are fit for Pu...
NUS-ISS32 views
Business Analyst Series 2023 - Week 3 Session 5 by DianaGray10
Business Analyst Series 2023 -  Week 3 Session 5Business Analyst Series 2023 -  Week 3 Session 5
Business Analyst Series 2023 - Week 3 Session 5
DianaGray10165 views
MemVerge: Past Present and Future of CXL by CXL Forum
MemVerge: Past Present and Future of CXLMemVerge: Past Present and Future of CXL
MemVerge: Past Present and Future of CXL
CXL Forum110 views
Combining Orchestration and Choreography for a Clean Architecture by ThomasHeinrichs1
Combining Orchestration and Choreography for a Clean ArchitectureCombining Orchestration and Choreography for a Clean Architecture
Combining Orchestration and Choreography for a Clean Architecture
ThomasHeinrichs168 views
"AI Startup Growth from Idea to 1M ARR", Oleksandr Uspenskyi by Fwdays
"AI Startup Growth from Idea to 1M ARR", Oleksandr Uspenskyi"AI Startup Growth from Idea to 1M ARR", Oleksandr Uspenskyi
"AI Startup Growth from Idea to 1M ARR", Oleksandr Uspenskyi
Fwdays26 views
Beyond the Hype: What Generative AI Means for the Future of Work - Damien Cum... by NUS-ISS
Beyond the Hype: What Generative AI Means for the Future of Work - Damien Cum...Beyond the Hype: What Generative AI Means for the Future of Work - Damien Cum...
Beyond the Hype: What Generative AI Means for the Future of Work - Damien Cum...
NUS-ISS28 views
CXL at OCP by CXL Forum
CXL at OCPCXL at OCP
CXL at OCP
CXL Forum208 views
How to reduce cold starts for Java Serverless applications in AWS at JCON Wor... by Vadym Kazulkin
How to reduce cold starts for Java Serverless applications in AWS at JCON Wor...How to reduce cold starts for Java Serverless applications in AWS at JCON Wor...
How to reduce cold starts for Java Serverless applications in AWS at JCON Wor...
Vadym Kazulkin70 views
Microchip: CXL Use Cases and Enabling Ecosystem by CXL Forum
Microchip: CXL Use Cases and Enabling EcosystemMicrochip: CXL Use Cases and Enabling Ecosystem
Microchip: CXL Use Cases and Enabling Ecosystem
CXL Forum129 views
Understanding GenAI/LLM and What is Google Offering - Felix Goh by NUS-ISS
Understanding GenAI/LLM and What is Google Offering - Felix GohUnderstanding GenAI/LLM and What is Google Offering - Felix Goh
Understanding GenAI/LLM and What is Google Offering - Felix Goh
NUS-ISS39 views
Samsung: CMM-H Tiered Memory Solution with Built-in DRAM by CXL Forum
Samsung: CMM-H Tiered Memory Solution with Built-in DRAMSamsung: CMM-H Tiered Memory Solution with Built-in DRAM
Samsung: CMM-H Tiered Memory Solution with Built-in DRAM
CXL Forum105 views
"Ukrainian Mobile Banking Scaling in Practice. From 0 to 100 and beyond", Vad... by Fwdays
"Ukrainian Mobile Banking Scaling in Practice. From 0 to 100 and beyond", Vad..."Ukrainian Mobile Banking Scaling in Practice. From 0 to 100 and beyond", Vad...
"Ukrainian Mobile Banking Scaling in Practice. From 0 to 100 and beyond", Vad...
Fwdays40 views
PharoJS - Zürich Smalltalk Group Meetup November 2023 by Noury Bouraqadi
PharoJS - Zürich Smalltalk Group Meetup November 2023PharoJS - Zürich Smalltalk Group Meetup November 2023
PharoJS - Zürich Smalltalk Group Meetup November 2023
Noury Bouraqadi113 views
AI: mind, matter, meaning, metaphors, being, becoming, life values by Twain Liu 刘秋艳
AI: mind, matter, meaning, metaphors, being, becoming, life valuesAI: mind, matter, meaning, metaphors, being, becoming, life values
AI: mind, matter, meaning, metaphors, being, becoming, life values
Micron CXL product and architecture update by CXL Forum
Micron CXL product and architecture updateMicron CXL product and architecture update
Micron CXL product and architecture update
CXL Forum27 views
"Quality Assurance: Achieving Excellence in startup without a Dedicated QA", ... by Fwdays
"Quality Assurance: Achieving Excellence in startup without a Dedicated QA", ..."Quality Assurance: Achieving Excellence in startup without a Dedicated QA", ...
"Quality Assurance: Achieving Excellence in startup without a Dedicated QA", ...
Fwdays33 views
Photowave Presentation Slides - 11.8.23.pptx by CXL Forum
Photowave Presentation Slides - 11.8.23.pptxPhotowave Presentation Slides - 11.8.23.pptx
Photowave Presentation Slides - 11.8.23.pptx
CXL Forum126 views

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