Slide 1

Slide 1 text

HAPPINESS, LIES AND RUBOCOP JOSEP EGEA jes@josepegea.com

Slide 2

Slide 2 text

KICK OFF!!

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

WHO'S JOSEP Software developer & Entrepreneur jes@josepegea.com https://www.josepegea.com https://github.com/josepegea

Slide 5

Slide 5 text

BEFORE WE START …

Slide 6

Slide 6 text

🇺🇦 STOP THE WAR 🇺🇦

Slide 7

Slide 7 text

ABOUT TODAY

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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!!)

Slide 10

Slide 10 text

WHY I WANT TO GIVE THIS TALK

Slide 11

Slide 11 text

WHAT ABOUT THE TITLE???

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

1. HAPPINESS

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

RUBY IS OPTIMIZED FOR DEVELOPER HAPPINESS

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

HOW OPTIMIZES RUBY FOR HAPPINESS?

Slide 19

Slide 19 text

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%

Slide 20

Slide 20 text

SO, HAPPINESS == CHOICE?

Slide 21

Slide 21 text

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`.--------------

Slide 22

Slide 22 text

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)`-------

Slide 23

Slide 23 text

IS OUR MARRIAGE WITH RUBY A HAPPY ONE?

Slide 24

Slide 24 text

2. LIES

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

WHY DO WE PUT UP WITH THIS?

Slide 27

Slide 27 text

LET'S TAKE A STEP BACK…

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

OR ANOTHER WAY TO SEE IT

Slide 30

Slide 30 text

OR EVEN YET ANOTHER ONE!

Slide 31

Slide 31 text

THIS IS SOMETHING WE'VE KNOWN FOR A LONG TIME

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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!

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

3. RUBOCOP

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

LET'S FOCUS ON THE LOWEST ONES Idioms Formatting

Slide 42

Slide 42 text

IDIOMS

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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) ?

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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!

Slide 48

Slide 48 text

CONSISTENCY (AKA FORMATTING)

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

IS THAT ALWAYS TRUE?

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

BUT IS IT ALWAYS SO?

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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 🤯

Slide 57

Slide 57 text

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! 🙃

Slide 58

Slide 58 text

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 😬🔫

Slide 59

Slide 59 text

AND IT TAKES US FROM …

Slide 60

Slide 60 text

… TO

Slide 61

Slide 61 text

… 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!

Slide 62

Slide 62 text

COULDN'T WE JUST AGREE TO DISAGREE AGREEABLY? Style/StringLiterals: Enabled: false

Slide 63

Slide 63 text

ON THE OTHER HAND …

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

GETTING BACK TO LIES …

Slide 66

Slide 66 text

THE ORIGINAL PROBLEM

Slide 67

Slide 67 text

THE CAUSES IN OUR MIND … Tech debt Coupling Inconsistencies

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

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…

Slide 70

Slide 70 text

HAPPY DEVELOPERS!!

Slide 71

Slide 71 text

LET'S KEEP THEM HAPPY!!

Slide 72

Slide 72 text

LET'S KEEP US HAPPY!!

Slide 73

Slide 73 text

RECAP

Slide 74

Slide 74 text

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!

Slide 75

Slide 75 text

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

Slide 76

Slide 76 text

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

Slide 77

Slide 77 text

BIG THANKS!! Hope you had some fun!! | jes@josepegea.com www.josepegea.com