$30 off During Our Annual Pro Sale. View Details »

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 jes@josepegea.com

  2. KICK OFF!!

  3. THANKS FOR COMING!!! Thanks to members of Madrid.rb Thanks to

    people from outside of Madrid! Thanks to our Sponsors! We ❤️ you!
  4. WHO'S JOSEP Software developer & Entrepreneur jes@josepegea.com https://www.josepegea.com https://github.com/josepegea

  5. BEFORE WE START …

  6. 🇺🇦 STOP THE WAR 🇺🇦

  7. ABOUT TODAY

  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
  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!!)
  10. WHY I WANT TO GIVE THIS TALK

  11. WHAT ABOUT THE TITLE???

  12. THERE'S A MOVIE FOR EVERYTHING!! "Sex, lies and videotape", by

    Steven Soderbergh 1989 Cannes award best movie and best actor
  13. 1. HAPPINESS

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

  15. RUBY IS OPTIMIZED FOR DEVELOPER HAPPINESS

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

  17. OR ARE WE IN IT FOR THE MONEY? Credits: "Stack

    Overvflow Developer Survey 2017-2021"
  18. HOW OPTIMIZES RUBY FOR HAPPINESS?

  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%
  20. SO, HAPPINESS == CHOICE?

  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`.--------------
  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)`-------
  23. IS OUR MARRIAGE WITH RUBY A HAPPY ONE?

  24. 2. LIES

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

  26. WHY DO WE PUT UP WITH THIS?

  27. LET'S TAKE A STEP BACK…

  28. THE SOFTWARE DEVELOPMENT DRAMA Credits: Introducing Agile Software Engineering in

    development (SAP Community)
  29. OR ANOTHER WAY TO SEE IT

  30. OR EVEN YET ANOTHER ONE!

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

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

    Adams
  33. IN THE END, WE HAVE … Unpredictable ETA's Bugs Longer

    development times More bugs … Refactors Bugs, bugs, bugs … Rewrite / Collapse (Sunset)
  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!
  35. LET'S FOCUS ON ONE … "Style Guides and Linters"

  36. 3. RUBOCOP

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

  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
  39. SCOPE OF LINTING Initially, portability, security, performance … But then

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

    useful Critical Security * * * * Performance * * * Compatibility * * Code Metrics * * Idioms * Formatting * Source: Me 😇
  41. LET'S FOCUS ON THE LOWEST ONES Idioms Formatting

  42. IDIOMS

  43. WHAT IS IDIOMATIC RUBY? What the community likes? What makes

    us feel "in"? What makes DHH smile?
  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) ?
  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
  46. BUT IDIOMS CAN HAVE A DARK SIDE, TOO They can

    be used to tell apart "the real rubyists"
  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!
  48. CONSISTENCY (AKA FORMATTING)

  49. ARBITRARY FORMATTING RULES Tabs vs spaces Single vs double quotes

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

    all we want We won't find a clear winner
  51. BUT WE MUST CHOOSE ONE, RIGHT! Of course!! For the

    sake of consistency!!
  52. IS THAT ALWAYS TRUE?

  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
  54. BUT IS IT ALWAYS SO?

  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
  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 🤯
  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! 🙃
  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 😬🔫
  59. AND IT TAKES US FROM …

  60. … TO

  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!
  62. COULDN'T WE JUST AGREE TO DISAGREE AGREEABLY? Style/StringLiterals: Enabled: false

  63. ON THE OTHER HAND …

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

  65. GETTING BACK TO LIES …

  66. THE ORIGINAL PROBLEM

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

  68. REALITY IN MOST CASES … Business changes External stack changes

    People leaving Developers getting bored Can't hire developers
  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…
  70. HAPPY DEVELOPERS!!

  71. LET'S KEEP THEM HAPPY!!

  72. LET'S KEEP US HAPPY!!

  73. RECAP

  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!
  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
  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
  77. BIG THANKS!! Hope you had some fun!! | jes@josepegea.com www.josepegea.com