Slide 1

Slide 1 text

Commit.where.not(liked_by: ‘DHH’).reject! A story about open source, ideas, smells and tastes [github.com/claudiob] I’m a Senior Software Engineer at GOOD and I’m going to to tell you a story about what I learned from contributing to Rails.

Slide 2

Slide 2 text

TL;DR Commit.where(“liked_by <> ‘DHH’”) Commit.where.not(liked_by: ‘DHH’) Rails 3 Rails 4 So since this is a story, I’m going to spoil it and tell you how it ends. In Rails 3, ActiveRecord cannot specify negative conditions in a query, so you have to use SQL operators. In Rails 4, ‘where’ becomes a chainable method that can be followed by a new not operator to write a negation

Slide 3

Slide 3 text

Now that you know the end of the story, let’s rewind to its inception. Most people don’t listen when you say something is annoying, even more if you are a stranger. But programmers can be more heartful and kind than normal people. We actually listen to complaints, even when they come from strangers. We empathize!

Slide 4

Slide 4 text

Commit.where(:liked_by ^ ‘DHH’) Someone else had a similar feeling and created a gem to avoid SQL fragments. What’s cool about gems is that you decide if you want to use them or not.

Slide 5

Slide 5 text

But suggesting this syntax in Rails core… gets rejected: it doesn’t smell or taste right. DHH may sound harsh but he is telling the truth. He is inviting us to try harder, to make a better coding world for everyone. This is the main reason I love open source, because you can follow your instincts and feel human again!

Slide 6

Slide 6 text

Commit.where_not_like(sha: ‘2F%’) So a new option came up: introducing where_not. Well then, why not where_like, where_not_like, where_union etc? But this would pollute ActiveRecord, a lot!

Slide 7

Slide 7 text

Commit.where.like(author: ‘cla%’) Commit.where.not(liked_by: ‘DHH’) Finally, the best idea arrives: keept only one ‘where’ and make it chainable.

Slide 8

Slide 8 text

where(“author <> ‘DHH’”) where.not(author: ‘DHH’) @de75af7 where.not(‘author = “DHH”’) where.not(‘author = ?’, ‘DHH’) Rails 3 where(“author like ‘DH%’”) where.like(author: ‘DH%’) @de75af7 Rails 3 where(“author not like ‘DH%’”) where.not_like(author: ‘DH%’) @de75af7 Rails 3 Initially implemented by @amatsuda. Smells almost right…

Slide 9

Slide 9 text

where(“id <> 42”).order(:id) where.not(id: 42).order(:id) @de75af7 Rails 3 …but here’s a gotcha. Using mixins does not smell right. Mixins delegate the responsibility to another module instead of making sure the code does what we want. And you can write “smelly code” like the where.order(…).not(…) statement.

Slide 10

Slide 10 text

where(“id <> 42”).order(:id) where.not(id: 42).order(:id) @de75af7 where.order(:id).not(id: 42) Rails 3 …but here’s a gotcha. Using mixins does not smell right. Mixins delegate the responsibility to another module instead of making sure the code does what we want. And you can write “smelly code” like the where.order(…).not(…) statement.

Slide 11

Slide 11 text

So I made a pull request to use a builder class rather than a module.

Slide 12

Slide 12 text

where(“id <> 42”) where.not(id: 42) @de75af7 Rails 3 where(“id > 42”) where.gt(id: 42) ??? Rails 3 where(“id <= 42”) where.lte(id: 42) ??? Rails 3 where.greater_than(id: 42) ??? where.less_than_or_equal(id: 42) ??? Moreover, if we start adding inequalities, than why not adding ALL of them?

Slide 13

Slide 13 text

Sounds right, but it’s not powerful enough, because they only apply to numeric types, while not has more uses. Funny how a discussion started with “all SQL fragments are annoying” and took us to really understand their power one by one.

Slide 14

Slide 14 text

where(“author <> ‘DHH’”) where.not(author: ‘DHH’) @de75af7 where.not(‘author = “DHH”’) where.not(‘author = ?’, ‘DHH’) Rails 3 where(“author like ‘DH%’”) where.like(author: ‘DH%’) @de75af7 Rails 3 where(“author not like ‘DH%’”) where.not_like(author: ‘DH%’) @de75af7 Rails 3 Which brings us back to evaluate this commit: in this light, where.like and where.not_like look very similar to inequalities: only apply to strings, do not reduce the code.

Slide 15

Slide 15 text

where(“author <> ‘DHH’”) where.not(author: ‘DHH’) @de75af7 where.not(‘author = “DHH”’) where.not(‘author = ?’, ‘DHH’) Rails 3 where(“author like ‘DH%’”) where.like(author: ‘DH%’) @de75af7 Rails 3 where(“author not like ‘DH%’”) where.not_like(author: ‘DH%’) @de75af7 Rails 3 Which brings us back to evaluate this commit: in this light, where.like and where.not_like look very similar to inequalities: only apply to strings, do not reduce the code.

Slide 16

Slide 16 text

Therefore, not powerful enough to belong to the core!

Slide 17

Slide 17 text

TL;DR Commit.where(“liked_by <> ‘DHH’”) Commit.where.not(liked_by: ‘DHH’) Rails 3 Rails 4 In the end, Rails 4 will have where chainable only with .not. Contributing to Rails is fun, you learn a lot from the process, not only about source code, but about communities and the impact you can make following your instincts and listening to people who express feelings like “being annoyed”. So, join the movement and contribute to open source!