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.

Elixir Brasil 2019 - Quality: A Panacéia para seu código Elixir

161 views

Published on

A talk explaining how to define `good code`. Using `Code Complete` as a reference to guide over the quality definition and introducing Linters (Credo) and Dialyzer (Dialyxir)

Published in: Technology
  • Be the first to comment

Elixir Brasil 2019 - Quality: A Panacéia para seu código Elixir

  1. 1. Qualidade A Panacéia para seu código Elixir
  2. 2. Hello! Weverton Timoteo @wevtimoteo
  3. 3. ● Very useful item to up your RPG character level ● I’d recommend to buy at least 5 ● Drops from some medium level mobs Panacea
  4. 4. ● Was a goddess of universal remedy ● Panacea traditionally had a potion with which she healed the sick ● A substance meant to cure all diseases ● "Something used to solve all problems" Panacea (from Greek mythology)
  5. 5. “ Any fool can write code that a computer can understand. Good programmers write code that humans can understand. – Martin Fowler
  6. 6. How Do You Define “Good Code” ?
  7. 7. maintainability?
  8. 8. Efficiency Modularity Elegance What's the first thing that comes to your mind? Proper design decision
  9. 9. You write readable code because it helps other people to read your code“ Code Complete, Steve McConnel Error rate Comprehensibility Reviewability Debugging Modifiability
  10. 10. Simplicity is it about the number of lines?
  11. 11. Readability comments, conventions, naming
  12. 12. Modularity its all about reusability
  13. 13. Layering attention to the boundaries of each layer
  14. 14. Design plan before build
  15. 15. Efficiency too many connections?
  16. 16. Elegance solve problems with proper solutions
  17. 17. Clarity do you really know what you have to do?
  18. 18. Balance
  19. 19. How to achieve this?
  20. 20. ● Erlang Open Telecom Protocol ● it's not that much about telecom anymore ● libraries available with the virtual machine ● OTP compliant: basically following process architecture OTP (Open Telecom Protocol)
  21. 21. BEAM ● Bogdan's Erlang Abstract Machine ○ emulator and loader were first implemented by Bogumil Hausman ● Björn's Erlang Abstract Machine ○ completely rewritten by Björn Gustavsson ● BEAM bytecode files have the .beam file extension
  22. 22. ● Static analysis tool that identifies software discrepancies, such as: ○ definite type errors ○ code that has become dead ○ unreachable because of programming error ○ unnecessary tests ● Erlang modules or entire (sets of) applications (.beam files) DIscrepancy AnaLYZer for ERlang programs DIALYZER
  23. 23. The Persistent Lookup Table (PLT) ● Stores the result of an analysis (acting like a cache) ● Then it can be used as starting point for later analyses ● .plt file extension
  24. 24. Gradual Typing Best of Static and Dynamic Worlds: ● Some variables and functions can have type declaration ● Developer decides which ones makes sense to declare ● Tool or Compiler can still check correctness and consistency of usage
  25. 25. ● It allows us to generate tests from the code examples in a module/function/macro's documentation ● Similar to Python's doctest ● Able to refer to specific numbered scenarios ● Copy-pasting examples from an actual IEx session Doctests
  26. 26. ● Run Dialyzer (test environment) to see how the types in API and the usage in the tests (calls) Using Dialyzer for tests
  27. 27. Dialyzer (Cons) ● Output in Erlang format ● No indentation ● No formatting
  28. 28. Dialixyr Mix tasks to simplify use of Dialyzer in Elixir projects
  29. 29. Dialixyr - Explain
  30. 30. Dialixyr - Explain
  31. 31. Dialixyr - Explain
  32. 32. Dialixyr - Ignore file
  33. 33. is a tool that analyzes source code to flag programming errors, bugs, stylistic errors, and suspicious constructs - Wikipedia What is a linter? “
  34. 34. 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
  35. 35. Abstract Syntax Tree (AST) ● It tells us how expressions are represented ● Internal representation of our code ● Using quote/2 we can convert Elixir code into its representation
  36. 36. AST: quote/2 Using quote/2 we can convert Elixir code into its representation
  37. 37. Is Elixir Homoiconic? No! If so: ● We would be writing in ASTs form ● It would bypass the lexing and parsing phase of Elixir compilation
  38. 38. ● Formats the given files and patterns Code Formatting
  39. 39. ● Code Complexity (Cyclomatic Complexity, etc) ● Style and Pattern Checking ● Performance ● Unused code ● Reports ● Outdated packages ● Code without tests (no coverage) ● Magic number detector What a Linter can do?
  40. 40. Dogma
  41. 41. Credo ● Also static analysis tool ● Checks ○ code consistency ○ readability ○ styleguides ○ conventions ● Useful for learning
  42. 42. mix credo categories
  43. 43. mix credo categories
  44. 44. Credo
  45. 45. And if all checks succeed ?
  46. 46. Thanks!Let’s start analyzing our code! |> team |> hiring()
  47. 47. References ● Code Complete: A Practical Handbook of Software Construction ● Elixir, processes and this thing called OTP ● Introducing HDD: Hughes Driven Development, com José Valim ● Credo ● Dialyzer ● Dialixyr ● Ebert: the code review assistant for your organization ● Metaprogramming in Elixir
  48. 48. More References ● ElixirConf 2016 - Dialyzer: Optimistic Type Checking for Erlang and Elixir by Jason Voegele ● Gradualizer: A Gradual Type System for Erlang ● Learn You Some Erlang: OTP

×