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

Happiness, Lies and Rubocop

Happiness, Lies and Rubocop

Ruby is optimized for happiness, but, are we all happy with the shared style guides and linting rules?

Finding the right balance between common conventions and individual coding style can be a delicate subject, specially for bigger teams.

In this talk we'll explore these limits and try to find a recipe for making sure that Rubocop makes us all happier developers

Josep Egea

March 14, 2022
Tweet

More Decks by Josep Egea

Other Decks in Programming

Transcript

  1. HAPPINESS, LIES AND RUBOCOP
    JOSEP EGEA
    [email protected]

    View Slide

  2. KICK OFF!!

    View Slide

  3. THANKS FOR COMING!!!
    Thanks to members of Madrid.rb
    Thanks to people from outside of Madrid!
    Thanks to our Sponsors! We ❤️
    you!

    View Slide

  4. WHO'S JOSEP
    Software developer & Entrepreneur
    [email protected]
    https://www.josepegea.com
    https://github.com/josepegea

    View Slide

  5. BEFORE WE START …

    View Slide

  6. 🇺🇦 STOP THE WAR
    🇺🇦

    View Slide

  7. ABOUT TODAY

    View Slide

  8. THIS IS A RANT
    This is NOT Expert Advice
    This is NOT Objective
    This is NOT a Best Practice
    It is NOT likely that we agree

    View Slide

  9. EXPECTED TAKEWAYS
    Despite all that, I'd love that …
    You don't get bored!
    We all examine our convictions about code style
    Maybe, who knows, we'll agree on one item or two
    Hopefully, we'll have a fun talk (no monologue!!)

    View Slide

  10. WHY I WANT TO GIVE THIS TALK

    View Slide

  11. WHAT ABOUT THE TITLE???

    View Slide

  12. THERE'S A MOVIE FOR EVERYTHING!!
    "Sex, lies and videotape", by Steven Soderbergh
    1989 Cannes award best movie and best actor

    View Slide

  13. 1. HAPPINESS

    View Slide

  14. WHAT IS HAPPINESS?
    Credits: "Sex, Lies and Videotape"

    View Slide

  15. RUBY IS OPTIMIZED FOR DEVELOPER HAPPINESS

    View Slide

  16. DO RUBY DEVELOPERS AGREE?
    Credits: "Stack Overvflow Developer Survey 2017-2021"

    View Slide

  17. OR ARE WE IN IT FOR THE MONEY?
    Credits: "Stack Overvflow Developer Survey 2017-2021"

    View Slide

  18. HOW OPTIMIZES RUBY FOR HAPPINESS?

    View Slide

  19. THE CANONICAL EXAMPLE
    Ruby adapts to the developer
    Python wants the developer to adapt
    jes% irb

    irb(main):001:0> exit

    jes% irb

    irb(main):001:0> quit

    jes% irb

    irb(main):001:0> ^D

    jes%
    jes% python3

    Python 3.9.10 (main, Jan 15 2022, 11:40:36)

    >>> quit

    Use quit() or Ctrl-D (i.e. EOF) to exit

    >>> exit

    Use exit() or Ctrl-D (i.e. EOF) to exit

    >>> exit()

    jes%

    View Slide

  20. SO, HAPPINESS == CHOICE?

    View Slide

  21. WELL, SOMEBODY SEEMS TO DISAGREE…
    Random pieces of flair from Rubocop
    -------Convert `if-elsif` to `case-when`.----------------------

    --Do not prefix writer method names with `set_`.---------------

    ----------Gems should be sorted in an alphabetical order ...---

    ----Prefer `Regexp.last_match(1)` over `$1`.-------------------

    ---Prefer `each` over `for`.-----------------------------------

    ----------------Redundant `return` detected.-------------------

    -----------------------Redundant `self` detected.--------------

    ---Use `current_col.negative?` instead of `current_col < 0`.---

    ---------------Use `i.zero?` instead of `i == 0`.--------------

    View Slide

  22. AND THIS SHOW IN GIT COMMIT MESSAGES …
    Random commit messages from a real project
    -----`Disable rubocop`-----------------------------------------

    ---------------`Fix Rubocop complaint`-------------------------

    --`Make Rubocop happy`-----------------------------------------

    ----------`Fix Rubocop offences`-------------------------------

    -----------------`Rubocop fixes`-------------------------------

    -----`But this is shorter than disabling rubocop
    🤷
    `-----------

    ----------------------------------`Fix RuboCop`----------------

    ---`Rubocop complaining about class length on CI (only)`-------

    View Slide

  23. IS OUR MARRIAGE WITH RUBY A HAPPY ONE?

    View Slide

  24. 2. LIES

    View Slide

  25. IS OUR HAPPINESS REAL?
    Credits: "Sex, Lies and Videotape"

    View Slide

  26. WHY DO WE PUT UP WITH THIS?

    View Slide

  27. LET'S TAKE A STEP BACK…

    View Slide

  28. THE SOFTWARE DEVELOPMENT DRAMA
    Credits: Introducing Agile Software Engineering in development (SAP Community)

    View Slide

  29. OR ANOTHER WAY TO SEE IT

    View Slide

  30. OR EVEN YET ANOTHER ONE!

    View Slide

  31. THIS IS SOMETHING WE'VE KNOWN FOR A LONG TIME

    View Slide

  32. AND IT DOESN'T SEEM TO GO AWAY
    Dilbert (c) Scott Adams

    View Slide

  33. IN THE END, WE HAVE …
    Unpredictable ETA's
    Bugs
    Longer development times
    More bugs …
    Refactors
    Bugs, bugs, bugs …
    Rewrite / Collapse (Sunset)

    View Slide

  34. THERE'S NO SILVER BULLET, BUT …
    We have sure tried
    😅!!!
    Methodologies: Let's plan ahead!
    Type systems: Let the compiler work!
    Modularity: Let's Divide and conquer!
    OOP: Let's message!
    TDD: Let's get a safety net!
    Design patterns: Let's square the circle!
    Style Guides and Linters: Let's normalize!

    View Slide

  35. LET'S FOCUS ON ONE …
    "Style Guides and Linters"

    View Slide

  36. 3. RUBOCOP

    View Slide

  37. LAWYERS AND COPS …
    Credits: "Sex, Lies and Videotape"

    View Slide

  38. WHAT IS LINTING
    lint was the name originally given to a particular program that
    flagged some suspicious and non-portable constructs (likely to be
    bugs) in C language source code.
    The term is now applied generically to tools that flag suspicious
    usage
    in software written in any computer language
    Stack Overflow

    View Slide

  39. SCOPE OF LINTING
    Initially, portability, security, performance …
    But then it expanded…
    Code formatting
    Code Style
    General Code Metrics

    View Slide

  40. LAYERS OF LINTING
    Layer Nice to have Somewhat useful Very useful Critical
    Security * * * *
    Performance * * *
    Compatibility * *
    Code Metrics * *
    Idioms *
    Formatting *
    Source: Me
    😇

    View Slide

  41. LET'S FOCUS ON THE LOWEST ONES
    Idioms
    Formatting

    View Slide

  42. IDIOMS

    View Slide

  43. WHAT IS IDIOMATIC RUBY?
    What the community likes?
    What makes us feel "in"?
    What makes DHH smile?

    View Slide

  44. WE, AS A COMMUNITY, HAVE DEVELOPED A SHARED TASTE
    We hide return
    But is it always more legible?
    We hide self
    Except for assignments
    We prefer 3.times do over (1..3).each do
    But it's "occupying" the Integer namespace
    What about if a < 0 vs if a.negative? ?
    And travel(3.days) vs
    travel_to(3.days.from_now) ?

    View Slide

  45. RUBY ❤️
    IDIOMS
    Ruby wouldn't be the same without idioms
    They also show that Ruby is alive
    Most likely, every idiom was first an aberration
    In a certain way, they are progress

    View Slide

  46. BUT IDIOMS CAN HAVE A DARK SIDE, TOO
    They can be used to tell apart "the real rubyists"

    View Slide

  47. RUBOCOP IS GREAT FOR LEARNING IDIOMS
    Let's use it as a guide
    But don't forget that idioms are subjective
    Let's not become a cult!

    View Slide

  48. CONSISTENCY (AKA FORMATTING)

    View Slide

  49. ARBITRARY FORMATTING RULES
    Tabs vs spaces
    Single vs double quotes
    Max line length
    Formatting of multiline expressions

    View Slide

  50. THERE'S NOT A BETTER CHOICE
    We can argue about them all we want
    We won't find a clear winner

    View Slide

  51. BUT WE MUST CHOOSE ONE, RIGHT!
    Of course!! For the sake of consistency!!

    View Slide

  52. IS THAT ALWAYS TRUE?

    View Slide

  53. SOME TIMES, CONSISTENCY IS OBJECTIVELY BETTER:
    Some inconsistencies would be a hell for the team
    Mixing tabs and spaces
    Mixing LF with CR/LF

    View Slide

  54. BUT IS IT ALWAYS SO?

    View Slide

  55. THE CASE OF SINGLE AND DOUBLE QUOTES (MY PET PEEVE)
    Even the Ruby Style Guide doesn't take sides
    And according to Rubocop's own survey:
    Single-quoted strings 58%
    Double-quoted strings 39%
    Other 3%
    Credits: Rubocop Defaults Survey, May 2020

    View Slide

  56. IS IT THAT IMPORTANT THAT WE'RE CONSISTENT?
    Reading code, there's almost no mental overhead
    Writing code, It's the last thing you care about
    But … when you get the error in Rubocop …
    There goes the flow
    🤯

    View Slide

  57. BUT, YOU CAN JUST CHANGE YOUR HABITS!!
    Retrain your muscles and you'll be done… right?
    Ok, but …
    Who works just in a single project??
    Murphy says the next one will change!
    🙃

    View Slide

  58. OK, THEN APPLY FORMAT AUTOMATICALLY
    Do we want to mass-process our source code?
    What could possibly go wrong?
    Even if it always works …
    It feels imposed
    😬🔫

    View Slide

  59. AND IT TAKES US FROM …

    View Slide

  60. … TO

    View Slide

  61. … OR EVEN
    Credits: Apple's 1984 Super bowl commercial
    We have created … a garden of pure ideology …
    Our Unification of Thoughts is more powerful a weapon than any
    fleet
    or army on earth …
    We are one people, with one will, one resolve, one cause …
    We shall prevail!

    View Slide

  62. COULDN'T WE JUST AGREE TO DISAGREE AGREEABLY?
    Style/StringLiterals:

    Enabled: false

    View Slide

  63. ON THE OTHER HAND …

    View Slide

  64. WE CAN ALWAYS GO THE PYTHON WAY
    Source: https://github.com/django/django/pull/15387

    View Slide

  65. GETTING BACK TO LIES …

    View Slide

  66. THE ORIGINAL PROBLEM

    View Slide

  67. THE CAUSES IN OUR MIND …
    Tech debt
    Coupling
    Inconsistencies

    View Slide

  68. REALITY IN MOST CASES …
    Business changes
    External stack changes
    People leaving
    Developers getting bored
    Can't hire developers

    View Slide

  69. LET'S BE HONEST!
    Maybe we're giving too much credit to consistency
    When the value of Ruby is beyond that:
    Expressiveness
    Flexibility
    Great community
    Powerful Gem ecosystem
    and…
    why not…

    View Slide

  70. HAPPY DEVELOPERS!!

    View Slide

  71. LET'S KEEP THEM HAPPY!!

    View Slide

  72. LET'S KEEP US HAPPY!!

    View Slide

  73. RECAP

    View Slide

  74. TAKEAWAY
    We take happiness in Ruby for granted, but it's our
    job to keep it there
    We run the risk of throwing the baby of happiness
    with the bathwater
    of consistency
    Rubocop is an outstanding tool, and very valuable,
    but you don't
    have to blindly say "yes to all"
    Use Enabled: false with good judgement and be
    happy!

    View Slide

  75. DID I DO MY JOB?
    Did you get bored?
    Did you examine your convictions about code
    style?
    Did you agree with me, at least on something?
    Was this a monologue? Let's see now, during the
    questions

    View Slide

  76. REFERENCES
    Sex, Lies and Videotape, the movie
    Stack Overflow Developer Survey, Most Loved
    The Rails Doctrine (about happinness)
    "The Mythical Man Month" and "No Silver Bullet", by Fred Brooks
    Rubocop Defaults Survey, May 2020
    Django: PR to reformat codebase with Black
    https://www.imdb.com/title/tt0098724/
    https://insights.stackoverflow.com/survey/2021#technology-most-loved-dreaded-and-wanted
    https://insights.stackoverflow.com/survey/2020#most-loved-dreaded-and-wanted
    https://insights.stackoverflow.com/survey/2019#most-loved-dreaded-and-wanted
    https://insights.stackoverflow.com/survey/2018#most-loved-dreaded-and-wanted
    https://insights.stackoverflow.com/survey/2017#most-loved-dreaded-and-wanted
    https://rubyonrails.org/doctrine#optimize-for-programmer-happiness
    https://en.wikipedia.org/wiki/The_Mythical_Man-Month
    https://en.wikipedia.org/wiki/No_Silver_Bullet
    https://metaredux.com/posts/2020/05/26/rubocop-defaults-survey-results.html
    https://github.com/django/django/pull/15387

    View Slide

  77. BIG THANKS!!
    Hope you had some fun!!
    |
    [email protected] www.josepegea.com

    View Slide