Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Are you happy with your code quality?

Are you happy with your code quality?

Presentation done at Geneva.rb and SoftShake with @rsempe

Vincent Pochet

October 25, 2013
Tweet

More Decks by Vincent Pochet

Other Decks in Programming

Transcript

  1. ARE YOU HAPPY WITH
    YOUR CODE QUALITY?
    Track Ruby/Rails, October 25th 2013.
    Presentation by and
    Vincent Pochet Romain Sempé

    View Slide

  2. VINCENT POCHET
    Website:
    Twitter:
    Github:
    vincent-pochet.eu
    @vin100pochet
    vincent-pochet

    View Slide

  3. ROMAIN SEMPÉ
    Website:
    Twitter:
    Github:
    sempe.me
    @rsempe
    rsempe

    View Slide

  4. CO-FOUNDERS OF
    We provide our expertise in web technologies with Rails.
    Located in Archamps, near Geneva.

    View Slide

  5. WHAT IS GOOD CODE?

    View Slide

  6. IT DEPENDS ON THE CONTEXT

    View Slide

  7. A good code is:
    EFFICIENT
    (The customer is happy)

    View Slide

  8. A good code is:
    TESTED

    View Slide

  9. A good code is:
    SIMPLE AND READABLE

    View Slide

  10. A good code is:
    PERFORMANT AND SCALABLE

    View Slide

  11. A good code is:
    SECURE

    View Slide

  12. A good code is:
    FLEXIBLE AND REUSABLE

    View Slide

  13. A good code is:
    NOT PERFECT

    View Slide

  14. OTHER MEASUREMENTS OF CODE QUALITY?

    View Slide

  15. View Slide

  16. HOW CAN I WRITE GOOD CODE?

    View Slide

  17. CREATE A POSITIVE WORK ENVIRONMENT

    View Slide

  18. TRUST IN WHAT YOU'RE DOING

    View Slide

  19. GET CLEAR SPECIFICATIONS

    View Slide

  20. COMMUNICATE WITH YOUR COWORKERS

    View Slide

  21. TEST DRIVEN DEVELOPMENT

    View Slide

  22. FOLLOW THE STYLE GUIDES
    github.com/styleguide/ruby
    guidelines.plataformatec.com.br/ruby.html

    View Slide

  23. BEST PRACTICES

    View Slide

  24. READ SOURCE CODE

    View Slide

  25. FOLLOW YOUR INTUITION

    View Slide

  26. WRITE ELOQUENT RUBY CODE
    "Remember, good code is like a good joke: It
    needs no explanation."

    View Slide

  27. FACTORIAL EXAMPLE

    View Slide

  28. FACTORIAL EXAMPLE (2)

    View Slide

  29. FACTORIAL EXAMPLE (3)

    View Slide

  30. FACTORIAL EXAMPLE (4)

    View Slide

  31. PROGRAMMING TRANSGRESSIONS
    FIXME is a "fuck you" to your coworkers

    View Slide

  32. REFACTORING

    View Slide

  33. PAIR PROGRAMMING

    View Slide

  34. CODE REVIEWS

    View Slide

  35. HOW TO MEASURE MY CODE QUALITY?

    View Slide

  36. USE TOOLS!

    View Slide

  37. Measure:
    LINE COUNT
    $ r
    a
    k
    e s
    t
    a
    t
    s
    +
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    -
    -
    +
    | N
    a
    m
    e | L
    i
    n
    e
    s | L
    O
    C | C
    l
    a
    s
    s
    e
    s | M
    e
    t
    h
    o
    d
    s | M
    /
    C | L
    O
    C
    /
    M |
    +
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    -
    -
    +
    | C
    o
    n
    t
    r
    o
    l
    l
    e
    r
    s | 1
    1
    8
    8 | 7
    6
    4 | 1
    9 | 1
    5
    0 | 7 | 3 |
    | H
    e
    l
    p
    e
    r
    s | 1
    5
    1 | 1
    0
    1 | 0 | 1
    8 | 0 | 3 |
    | M
    o
    d
    e
    l
    s | 1
    3
    4
    6 | 7
    7
    5 | 1
    5 | 1
    3
    4 | 8 | 3 |
    | L
    i
    b
    r
    a
    r
    i
    e
    s | 6
    4 | 2
    9 | 2 | 5 | 2 | 3 |
    | I
    n
    t
    e
    g
    r
    a
    t
    i
    o
    n t
    e
    s
    t
    s | 0 | 0 | 0 | 0 | 0 | 0 |
    | F
    u
    n
    c
    t
    i
    o
    n
    a
    l t
    e
    s
    t
    s | 1
    4
    0
    0 | 1
    1
    5
    3 | 2
    1 | 1
    6
    5 | 7 | 4 |
    | U
    n
    i
    t t
    e
    s
    t
    s | 1
    5
    8
    1 | 1
    2
    4
    1 | 4
    3 | 1
    7
    6 | 4 | 5 |
    +
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    -
    -
    +
    | T
    o
    t
    a
    l | 5
    7
    3
    0 | 4
    0
    6
    3 | 1
    0
    0 | 6
    4
    8 | 6 | 4 |
    +
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    -
    -
    +
    C
    o
    d
    e L
    O
    C
    : 1
    6
    6
    9 T
    e
    s
    t L
    O
    C
    : 2
    3
    9
    4 C
    o
    d
    e t
    o T
    e
    s
    t R
    a
    t
    i
    o
    : 1
    :
    1
    .
    4

    View Slide

  38. Measure:
    COMPLEXITY
    Flog
    Cyclomatic complexity
    ABC metric

    View Slide

  39. Measure:
    COMPLEXITY
    FLOG
    $ f
    l
    o
    g a
    p
    p
    /
    m
    o
    d
    e
    l
    s
    1
    1
    8
    2
    .
    2
    : f
    l
    o
    g t
    o
    t
    a
    l
    7
    .
    8
    : f
    l
    o
    g
    /
    m
    e
    t
    h
    o
    d a
    v
    e
    r
    a
    g
    e
    4
    1
    .
    6
    : S
    t
    o
    r
    y
    :
    :
    S
    t
    e
    p
    #
    c
    h
    a
    n
    g
    e
    _
    p
    o
    s
    i
    t
    i
    o
    n a
    p
    p
    /
    m
    o
    d
    e
    l
    s
    /
    s
    t
    o
    r
    y
    /
    s
    t
    e
    p
    .
    r
    b
    :
    1
    3
    3
    2
    7
    .
    7
    : S
    t
    o
    r
    y
    :
    :
    S
    t
    e
    p
    #
    u
    p
    d
    a
    t
    e
    _
    t
    r
    i
    g
    g
    e
    r
    _
    i
    t
    e
    m
    s
    ! a
    p
    p
    /
    m
    o
    d
    e
    l
    s
    /
    s
    t
    o
    r
    y
    /
    s
    t
    e
    p
    .
    r
    b
    :
    8
    3
    2
    5
    .
    8
    : S
    e
    r
    v
    i
    c
    e
    A
    c
    c
    o
    u
    n
    t
    :
    :
    G
    o
    o
    g
    l
    e
    O
    a
    u
    t
    h
    2
    :
    :
    f
    r
    o
    m
    _
    o
    m
    n
    i
    a
    u
    t
    h a
    p
    p
    /
    m
    o
    d
    e
    l
    s
    /
    s
    e
    r
    v
    i
    c
    e
    _
    a
    c
    c
    o
    u
    n
    t
    /
    g
    o
    o
    g
    l
    e
    _
    o
    a
    u
    2
    5
    .
    8
    : S
    e
    r
    v
    i
    c
    e
    A
    c
    c
    o
    u
    n
    t
    :
    :
    F
    a
    c
    e
    b
    o
    o
    k
    :
    :
    f
    r
    o
    m
    _
    o
    m
    n
    i
    a
    u
    t
    h a
    p
    p
    /
    m
    o
    d
    e
    l
    s
    /
    s
    e
    r
    v
    i
    c
    e
    _
    a
    c
    c
    o
    u
    n
    t
    /
    f
    a
    c
    e
    b
    o
    o
    k
    .
    r
    b
    :
    2
    0
    2
    2
    .
    3
    : P
    r
    o
    g
    r
    a
    m
    #
    s
    c
    h
    e
    d
    u
    l
    e
    _
    r
    u
    n a
    p
    p
    /
    m
    o
    d
    e
    l
    s
    /
    p
    r
    o
    g
    r
    a
    m
    .
    r
    b
    :
    1
    0
    9
    2
    1
    .
    4
    : S
    t
    o
    r
    y
    :
    :
    S
    t
    e
    p
    #
    v
    a
    l
    i
    d
    a
    t
    e
    _
    a
    c
    t
    i
    o
    n a
    p
    p
    /
    m
    o
    d
    e
    l
    s
    /
    s
    t
    o
    r
    y
    /
    s
    t
    e
    p
    .
    r
    b
    :
    2
    3
    2
    2
    1
    .
    4
    : R
    o
    l
    e
    S
    t
    e
    p
    #
    v
    a
    l
    i
    d
    a
    t
    e
    _
    a
    c
    t
    i
    o
    n a
    p
    p
    /
    m
    o
    d
    e
    l
    s
    /
    r
    o
    l
    e
    _
    s
    t
    e
    p
    .
    r
    b
    :
    6
    4
    1
    7
    .
    6
    : S
    t
    o
    r
    y
    :
    :
    S
    t
    e
    p
    #
    i
    n
    c
    r
    e
    m
    e
    n
    t
    _
    a
    l
    l
    _
    p
    o
    s
    i
    t
    i
    o
    n
    s a
    p
    p
    /
    m
    o
    d
    e
    l
    s
    /
    s
    t
    o
    r
    y
    /
    s
    t
    e
    p
    .
    r
    b
    :
    1
    7
    8

    View Slide

  40. Measure:
    DUPLICATION
    KEEP IN MIND :
    DRY principle is:
    Every piece of knowledge must have a single,
    unambiguous, authoritative representation
    within a system.
    And not:
    Don’t type the same characters into the
    keyboard multiple times.

    View Slide

  41. Measure:
    DUPLICATION
    FLAY
    $ f
    l
    a
    y a
    p
    p
    /
    m
    o
    d
    e
    l
    s
    T
    o
    t
    a
    l s
    c
    o
    r
    e (
    l
    o
    w
    e
    r i
    s b
    e
    t
    t
    e
    r
    ) = 4
    0
    6
    1
    ) I
    D
    E
    N
    T
    I
    C
    A
    L c
    o
    d
    e f
    o
    u
    n
    d i
    n :
    d
    e
    f
    s (
    m
    a
    s
    s
    *
    2 = 2
    2
    0
    )
    a
    p
    p
    /
    m
    o
    d
    e
    l
    s
    /
    s
    e
    r
    v
    i
    c
    e
    _
    a
    c
    c
    o
    u
    n
    t
    /
    f
    a
    c
    e
    b
    o
    o
    k
    .
    r
    b
    :
    2
    0
    a
    p
    p
    /
    m
    o
    d
    e
    l
    s
    /
    s
    e
    r
    v
    i
    c
    e
    _
    a
    c
    c
    o
    u
    n
    t
    /
    g
    o
    o
    g
    l
    e
    _
    o
    a
    u
    t
    h
    2
    .
    r
    b
    :
    8
    2
    ) S
    i
    m
    i
    l
    a
    r c
    o
    d
    e f
    o
    u
    n
    d i
    n :
    d
    e
    f
    n (
    m
    a
    s
    s = 7
    0
    )
    a
    p
    p
    /
    m
    o
    d
    e
    l
    s
    /
    r
    o
    l
    e
    _
    s
    t
    e
    p
    .
    r
    b
    :
    6
    4
    a
    p
    p
    /
    m
    o
    d
    e
    l
    s
    /
    s
    t
    o
    r
    y
    /
    s
    t
    e
    p
    .
    r
    b
    :
    2
    3
    2
    3
    ) S
    i
    m
    i
    l
    a
    r c
    o
    d
    e f
    o
    u
    n
    d i
    n :
    d
    e
    f
    n (
    m
    a
    s
    s = 4
    0
    )
    a
    p
    p
    /
    m
    o
    d
    e
    l
    s
    /
    s
    t
    o
    r
    y
    /
    s
    t
    e
    p
    .
    r
    b
    :
    1
    1
    2
    a
    p
    p
    /
    m
    o
    d
    e
    l
    s
    /
    s
    t
    o
    r
    y
    /
    s
    t
    e
    p
    .
    r
    b
    :
    1
    1
    6

    View Slide

  42. Measure:
    CHURN
    The change of your source files over time
    $ c
    h
    u
    r
    n
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    * P
    r
    o
    j
    e
    c
    t C
    h
    u
    r
    n
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    *
    F
    i
    l
    e
    s
    :
    +
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +
    | f
    i
    l
    e
    _
    p
    a
    t
    h | t
    i
    m
    e
    s
    _
    c
    h
    a
    n
    g
    e
    d |
    +
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    -
    +
    | c
    o
    n
    f
    i
    g
    /
    l
    o
    c
    a
    l
    e
    s
    /
    e
    n
    .
    y
    m
    l | 2
    8
    1 |
    | d
    b
    /
    s
    t
    r
    u
    c
    t
    u
    r
    e
    .
    s
    q
    l | 1
    0
    8 |
    | a
    p
    p
    /
    v
    i
    e
    w
    s
    /
    p
    l
    a
    y
    e
    r
    /
    s
    h
    o
    w
    .
    h
    t
    m
    l
    .
    e
    r
    b | 9
    2 |
    | a
    p
    p
    /
    v
    i
    e
    w
    s
    /
    s
    t
    o
    r
    i
    e
    s
    /
    s
    h
    o
    w
    .
    h
    t
    m
    l
    .
    e
    r
    b | 8
    8 |
    | G
    e
    m
    f
    i
    l
    e
    .
    l
    o
    c
    k | 8
    7 |

    View Slide

  43. Measure:
    CHURN
    Churn VS Complexity
    Turbulence

    View Slide

  44. All metrics
    METRIC FU
    https://github.com/metricfu/metric_fu

    View Slide

  45. (For Ruby < 1.9)
    :
    Measure:
    TEST COVERAGE
    RCov
    SimpleCov
    $ C
    O
    V
    E
    R
    A
    G
    E
    =
    t
    r
    u
    e r
    a
    k
    e t
    e
    s
    t
    # R
    u
    n
    n
    i
    n
    g t
    e
    s
    t
    s
    :
    F
    i
    n
    i
    s
    h
    e
    d t
    e
    s
    t
    s i
    n 1
    4
    .
    5
    5
    6
    9
    4
    3
    s
    , 1
    1
    .
    8
    8
    4
    4 t
    e
    s
    t
    s
    /
    s
    , 2
    4
    .
    7
    9
    9
    2 a
    s
    s
    e
    r
    t
    i
    o
    n
    s
    /
    s
    .
    1
    7
    3 t
    e
    s
    t
    s
    , 3
    6
    1 a
    s
    s
    e
    r
    t
    i
    o
    n
    s
    , 0 f
    a
    i
    l
    u
    r
    e
    s
    , 0 e
    r
    r
    o
    r
    s
    , 0 s
    k
    i
    p
    s
    r
    u
    b
    y -
    v
    : r
    u
    b
    y 2
    .
    0
    .
    0
    p
    2
    4
    7 (
    2
    0
    1
    3
    -
    0
    6
    -
    2
    7 r
    e
    v
    i
    s
    i
    o
    n 4
    1
    6
    7
    4
    )
    1
    0
    7
    1 / 1
    1
    8
    7 L
    O
    C (
    9
    0
    .
    2
    3
    %
    ) c
    o
    v
    e
    r
    e
    d
    .
    R
    u
    n o
    p
    t
    i
    o
    n
    s
    :
    # R
    u
    n
    n
    i
    n
    g t
    e
    s
    t
    s
    :
    F
    i
    n
    i
    s
    h
    e
    d t
    e
    s
    t
    s i
    n 1
    8
    .
    5
    8
    0
    0
    9
    9
    s
    , 8
    .
    7
    7
    2
    8 t
    e
    s
    t
    s
    /
    s
    , 2
    4
    .
    5
    9
    6
    2 a
    s
    s
    e
    r
    t
    i
    o
    n
    s
    /
    s
    .
    1
    6
    3 t
    e
    s
    t
    s
    , 4
    5
    7 a
    s
    s
    e
    r
    t
    i
    o
    n
    s
    , 0 f
    a
    i
    l
    u
    r
    e
    s
    , 0 e
    r
    r
    o
    r
    s
    , 0 s
    k
    i
    p
    s
    r
    u
    b
    y -
    v
    : r
    u
    b
    y 2
    .
    0
    .
    0
    p
    2
    4
    7 (
    2
    0
    1
    3
    -
    0
    6
    -
    2
    7 r
    e
    v
    i
    s
    i
    o
    n 4
    1
    6
    7
    4
    )
    1
    1
    5
    9 / 1
    1
    6
    0 L
    O
    C (
    9
    9
    .
    9
    1
    %
    ) c
    o
    v
    e
    r
    e
    d
    .

    View Slide

  46. Measure:
    TEST COVERAGE
    SIMPLECOV REPORT

    View Slide

  47. Measure:
    TEST COVERAGE
    SIMPLECOV REPORT DETAILS

    View Slide

  48. Security:
    BRAKEMAN
    http://brakemanscanner.org/

    View Slide

  49. View Slide

  50. CI:
    TRAVIS

    View Slide

  51. CI:
    SEMAPHORE

    View Slide

  52. Coverage:
    COVERALLS

    View Slide

  53. Project dependencies:
    GEMNASIUM

    View Slide

  54. Perfomance:
    NEW RELIC

    View Slide

  55. Code quality!
    CODECLIMATE

    View Slide

  56. SOFTWARE != CODE

    View Slide

  57. "Good software meets the present objective at
    an acceptable cost without incurring an
    unacceptable amount of future risk"
    Brandon Keepers - Github

    View Slide

  58. QUALITY VS VELOCITY

    View Slide

  59. MERCI ET BISOUS!

    View Slide