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. THANKS FOR COMING!!! Thanks to members of Madrid.rb Thanks to

    people from outside of Madrid! Thanks to our Sponsors! We ❤️ you!
  2. 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
  3. 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!!)
  4. THERE'S A MOVIE FOR EVERYTHING!! "Sex, lies and videotape", by

    Steven Soderbergh 1989 Cannes award best movie and best actor
  5. OR ARE WE IN IT FOR THE MONEY? Credits: "Stack

    Overvflow Developer Survey 2017-2021"
  6. 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%
  7. 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`.--------------
  8. 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)`-------
  9. IN THE END, WE HAVE … Unpredictable ETA's Bugs Longer

    development times More bugs … Refactors Bugs, bugs, bugs … Rewrite / Collapse (Sunset)
  10. 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!
  11. 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
  12. SCOPE OF LINTING Initially, portability, security, performance … But then

    it expanded… Code formatting Code Style General Code Metrics
  13. LAYERS OF LINTING Layer Nice to have Somewhat useful Very

    useful Critical Security * * * * Performance * * * Compatibility * * Code Metrics * * Idioms * Formatting * Source: Me 😇
  14. 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) ?
  15. 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
  16. BUT IDIOMS CAN HAVE A DARK SIDE, TOO They can

    be used to tell apart "the real rubyists"
  17. 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!
  18. ARBITRARY FORMATTING RULES Tabs vs spaces Single vs double quotes

    Max line length Formatting of multiline expressions
  19. THERE'S NOT A BETTER CHOICE We can argue about them

    all we want We won't find a clear winner
  20. SOME TIMES, CONSISTENCY IS OBJECTIVELY BETTER: Some inconsistencies would be

    a hell for the team Mixing tabs and spaces Mixing LF with CR/LF
  21. 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
  22. 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 🤯
  23. 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! 🙃
  24. 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 😬🔫
  25. … 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!
  26. REALITY IN MOST CASES … Business changes External stack changes

    People leaving Developers getting bored Can't hire developers
  27. 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…
  28. 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!
  29. 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
  30. 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