Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

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

276 views

Published on

How do you define a "good code"? How to write high-quality software with assistance of linters and static analysis.

Using golint, govet and gofmt can improve our code.

Published in: Technology
  • Be the first to comment

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

  1. 1. Analysing code quality Linters and Static Analysis
  2. 2. Hello! Weverton Timoteo @wevtimoteo
  3. 3. “ Any fool can write code that a computer can understand. Good programmers write code that humans can understand. – Martin Fowler
  4. 4. How Do You Define “Good Code” ?
  5. 5. maintainability?
  6. 6. Efficiency Modularity Elegance What's the first thing that comes to your mind? Proper design decision
  7. 7. 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
  8. 8. Simplicity is it about the number of lines?
  9. 9. Readability comments, conventions, naming
  10. 10. Modularity its all about reusability
  11. 11. Layering attention to the boundaries of each layer
  12. 12. Design plan before build
  13. 13. Efficiency too many connections?
  14. 14. Elegance solve problems with proper solutions
  15. 15. Clarity do you really know what you have to do?
  16. 16. Which one matters most?
  17. 17. Balance
  18. 18. How to achieve this?
  19. 19. is a tool that analyzes source code to flag programming errors, bugs, stylistic errors, and suspicious constructs - Wikipedia What is a linter? “
  20. 20. 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
  21. 21. Packages - Std lib scanner token astparser
  22. 22. Abstract Syntax Tree (AST)
  23. 23. Abstract Syntax Tree (AST)
  24. 24. Abstract Syntax Tree (AST)
  25. 25. Abstract Syntax Tree (AST)
  26. 26. Abstract Syntax Tree (AST)
  27. 27. 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
  28. 28. ● gofmt - Must have for every project ● goimports - Additionally to gofmt, checks unused imports ● unindent - Report code that is unnecessarily indented Code Formatting
  29. 29. gofmt -s (Simplify)
  30. 30. goimports
  31. 31. ● 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
  32. 32. ABC Metrics - Calculate Score ● Assignment count when: =, *=, /=, %=, +=, <<=, >>=, &=, ^= ++, -- ● Branch count when: Function call ● Condition count when: <, >, <=, >=, ==, != else, case
  33. 33. ABC Metrics (abcgo)
  34. 34. ● 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
  35. 35. golint
  36. 36. 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
  37. 37. Revive - Configuration
  38. 38. Revive - Custom Rule
  39. 39. Revive - Custom Rule
  40. 40. Revive - Custom Rule
  41. 41. ● 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
  42. 42. govet
  43. 43. ● Performance ● Unused code ● Reports ● Outdated packages ● Code without tests (no coverage) ● Magic number detector What else?
  44. 44. How should I start?
  45. 45. Thanks! Any questions? Which linters are you using? Tell me! @wevtimoteo
  46. 46. 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
  47. 47. References ● Code Complete: A Practical Handbook of Software Construction ● Lexical Scanning in Go - Rob Pike

×