Qualidade
A Panacéia para seu código Elixir
Hello!
Weverton Timoteo
@wevtimoteo
● Very useful item to up your RPG
character level
● I’d recommend to buy at least 5
● Drops from some medium level
mobs
Panacea
● 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)
“
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
You write readable code because it helps other
people to read your code“
Code Complete, Steve McConnel
Error rate
Comprehensibility
Reviewability
Debugging
Modifiability
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?
Balance
How to achieve
this?
● 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)
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
● 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
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
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
● 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
● Run Dialyzer (test environment) to see how the
types in API and the usage in the tests (calls)
Using Dialyzer for tests
Dialyzer (Cons)
● Output in Erlang format
● No indentation
● No formatting
Dialixyr
Mix tasks to simplify use of
Dialyzer in Elixir projects
Dialixyr - Explain
Dialixyr - Explain
Dialixyr - Explain
Dialixyr - Ignore file
is a tool that analyzes source code to flag
programming errors, bugs, stylistic errors,
and suspicious constructs
- Wikipedia
What is a linter?
“
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
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
AST: quote/2
Using quote/2 we can convert Elixir code into its representation
Is Elixir Homoiconic?
No!
If so:
● We would be writing in ASTs form
● It would bypass the lexing and parsing phase of Elixir compilation
● Formats the given files and patterns
Code Formatting
● 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?
Dogma
Credo
● Also static analysis tool
● Checks
○ code consistency
○ readability
○ styleguides
○ conventions
● Useful for learning
mix credo categories
mix credo categories
Credo
And if all checks
succeed ?
Thanks!Let’s start analyzing
our code!
|> team
|> hiring()
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
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

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

  • 1.
    Qualidade A Panacéia paraseu código Elixir
  • 2.
  • 3.
    ● Very usefulitem to up your RPG character level ● I’d recommend to buy at least 5 ● Drops from some medium level mobs Panacea
  • 4.
    ● Was agoddess 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.
    “ Any fool canwrite code that a computer can understand. Good programmers write code that humans can understand. – Martin Fowler
  • 6.
    How Do YouDefine “Good Code” ?
  • 7.
  • 8.
    Efficiency Modularity Elegance What'sthe first thing that comes to your mind? Proper design decision
  • 9.
    You write readablecode because it helps other people to read your code“ Code Complete, Steve McConnel Error rate Comprehensibility Reviewability Debugging Modifiability
  • 10.
    Simplicity is it aboutthe number of lines?
  • 11.
  • 12.
  • 13.
    Layering attention to theboundaries of each layer
  • 14.
  • 15.
  • 16.
  • 17.
    Clarity do you reallyknow what you have to do?
  • 18.
  • 19.
  • 20.
    ● Erlang OpenTelecom 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.
    BEAM ● Bogdan's ErlangAbstract 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.
    ● Static analysistool 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.
    The Persistent LookupTable (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.
    Gradual Typing Best ofStatic 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.
    ● It allowsus 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.
    ● Run Dialyzer(test environment) to see how the types in API and the usage in the tests (calls) Using Dialyzer for tests
  • 27.
    Dialyzer (Cons) ● Outputin Erlang format ● No indentation ● No formatting
  • 28.
    Dialixyr Mix tasks tosimplify use of Dialyzer in Elixir projects
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
    is a toolthat analyzes source code to flag programming errors, bugs, stylistic errors, and suspicious constructs - Wikipedia What is a linter? “
  • 34.
    Homoiconicity ● The codeused 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.
    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.
    AST: quote/2 Using quote/2we can convert Elixir code into its representation
  • 37.
    Is Elixir Homoiconic? No! Ifso: ● We would be writing in ASTs form ● It would bypass the lexing and parsing phase of Elixir compilation
  • 38.
    ● Formats thegiven files and patterns Code Formatting
  • 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.
  • 41.
    Credo ● Also staticanalysis tool ● Checks ○ code consistency ○ readability ○ styleguides ○ conventions ● Useful for learning
  • 42.
  • 43.
  • 44.
  • 45.
    And if allchecks succeed ?
  • 46.
    Thanks!Let’s start analyzing ourcode! |> team |> hiring()
  • 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.
    More References ● ElixirConf2016 - Dialyzer: Optimistic Type Checking for Erlang and Elixir by Jason Voegele ● Gradualizer: A Gradual Type System for Erlang ● Learn You Some Erlang: OTP