SlideShare uma empresa Scribd logo
1 de 17
Baixar para ler offline
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.
TL;DR
Rails 3    Commit.where(“liked_by <> ‘DHH’”)



Rails 4     Commit.where.not(liked_by: ‘DHH’)

          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
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!
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.
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!
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!
Commit.where.not(liked_by: ‘DHH’)

                                                                                Commit.where.like(author: ‘cla%’)

Finally, the best idea arrives: keept only one ‘where’ and make it chainable.
Rails 3                                                    where(“author <> ‘DHH’”)

@de75af7 where.not(author: ‘DHH’)

                                                            where.not(‘author = “DHH”’)

                                                            where.not(‘author = ?’, ‘DHH’)

 Rails 3                                                    where(“author like ‘DH%’”)

@de75af7 where.like(author: ‘DH%’)

 Rails 3                                                    where(“author not like ‘DH%’”)

@de75af7 where.not_like(author: ‘DH%’)
 Initially implemented by @amatsuda. Smells almost right…
Rails 3                                                                                               where(“id <> 42”).order(:id)

@de75af7 where.not(id: 42).order(:id)




 …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.
Rails 3                                                                                               where(“id <> 42”).order(:id)

@de75af7 where.not(id: 42).order(:id)

                                                                                                           where.order(:id).not(id: 42)




 …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.
So I made a pull request to use a builder class rather than a module.
Rails 3                                                                        where(“id <> 42”)

@de75af7 where.not(id: 42)

 Rails 3                                                                        where(“id > 42”)

                         ???                                                    where.gt(id: 42)

                         ???                                                    where.greater_than(id: 42)

 Rails 3                                                                        where(“id <= 42”)

                         ???                                                    where.lte(id: 42)

                         ???                                                    where.less_than_or_equal(id: 42)
  Moreover, if we start adding inequalities, than why not adding ALL of them?
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.
Rails 3                                                                                                                                                                         where(“author <> ‘DHH’”)

@de75af7 where.not(author: ‘DHH’)

                                                                                                                                                                                 where.not(‘author = “DHH”’)

                                                                                                                                                                                 where.not(‘author = ?’, ‘DHH’)

 Rails 3                                                                                                                                                                         where(“author like ‘DH%’”)

@de75af7 where.like(author: ‘DH%’)

 Rails 3                                                                                                                                                                         where(“author not like ‘DH%’”)

@de75af7 where.not_like(author: ‘DH%’)
  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.
Rails 3                                                                                                                                                                         where(“author <> ‘DHH’”)

@de75af7 where.not(author: ‘DHH’)

                                                                                                                                                                                 where.not(‘author = “DHH”’)

                                                                                                                                                                                 where.not(‘author = ?’, ‘DHH’)

 Rails 3                                                                                                                                                                         where(“author like ‘DH%’”)

@de75af7 where.like(author: ‘DH%’)

 Rails 3                                                                                                                                                                         where(“author not like ‘DH%’”)

@de75af7 where.not_like(author: ‘DH%’)
  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.
Therefore, not powerful enough to belong to the core!
TL;DR
Rails 3                                                                                                                                                                                                                                     Commit.where(“liked_by <> ‘DHH’”)



Rails 4                                                                                                                                                                                                                                     Commit.where.not(liked_by: ‘DHH’)


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!

Mais conteúdo relacionado

Semelhante a How Rails improved negative query conditions with where.not

Redis Indices (#RedisTLV)
Redis Indices (#RedisTLV)Redis Indices (#RedisTLV)
Redis Indices (#RedisTLV)Itamar Haber
 
Elegant CSS Design In Drupal: LESS vs Sass
Elegant CSS Design In Drupal: LESS vs SassElegant CSS Design In Drupal: LESS vs Sass
Elegant CSS Design In Drupal: LESS vs SassMediacurrent
 
Transformational Tricks for RDF.pptx
Transformational Tricks for RDF.pptxTransformational Tricks for RDF.pptx
Transformational Tricks for RDF.pptxKurt Cagle
 
Prototype Utility Methods(1)
Prototype Utility Methods(1)Prototype Utility Methods(1)
Prototype Utility Methods(1)mussawir20
 
Ruby On Rails Introduction
Ruby On Rails IntroductionRuby On Rails Introduction
Ruby On Rails IntroductionThomas Fuchs
 
Ruby from zero to hero
Ruby from zero to heroRuby from zero to hero
Ruby from zero to heroDiego Lemos
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perlworr1244
 
php&mysql with Ethical Hacking
php&mysql with Ethical Hackingphp&mysql with Ethical Hacking
php&mysql with Ethical HackingBCET
 
Hijacking Ruby Syntax in Ruby (RubyConf 2018)
Hijacking Ruby Syntax in Ruby (RubyConf 2018)Hijacking Ruby Syntax in Ruby (RubyConf 2018)
Hijacking Ruby Syntax in Ruby (RubyConf 2018)SATOSHI TAGOMORI
 

Semelhante a How Rails improved negative query conditions with where.not (12)

Redis Indices (#RedisTLV)
Redis Indices (#RedisTLV)Redis Indices (#RedisTLV)
Redis Indices (#RedisTLV)
 
Rest in Rails
Rest in RailsRest in Rails
Rest in Rails
 
Elegant CSS Design In Drupal: LESS vs Sass
Elegant CSS Design In Drupal: LESS vs SassElegant CSS Design In Drupal: LESS vs Sass
Elegant CSS Design In Drupal: LESS vs Sass
 
Low-Maintenance Perl
Low-Maintenance PerlLow-Maintenance Perl
Low-Maintenance Perl
 
Transformational Tricks for RDF.pptx
Transformational Tricks for RDF.pptxTransformational Tricks for RDF.pptx
Transformational Tricks for RDF.pptx
 
Prototype Utility Methods(1)
Prototype Utility Methods(1)Prototype Utility Methods(1)
Prototype Utility Methods(1)
 
Ruby On Rails Introduction
Ruby On Rails IntroductionRuby On Rails Introduction
Ruby On Rails Introduction
 
Ruby from zero to hero
Ruby from zero to heroRuby from zero to hero
Ruby from zero to hero
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
SEO for Developers
SEO for DevelopersSEO for Developers
SEO for Developers
 
php&mysql with Ethical Hacking
php&mysql with Ethical Hackingphp&mysql with Ethical Hacking
php&mysql with Ethical Hacking
 
Hijacking Ruby Syntax in Ruby (RubyConf 2018)
Hijacking Ruby Syntax in Ruby (RubyConf 2018)Hijacking Ruby Syntax in Ruby (RubyConf 2018)
Hijacking Ruby Syntax in Ruby (RubyConf 2018)
 

Último

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 

Último (20)

Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 

How Rails improved negative query conditions with where.not

  • 1. 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.
  • 2. TL;DR Rails 3 Commit.where(“liked_by <> ‘DHH’”) Rails 4 Commit.where.not(liked_by: ‘DHH’) 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
  • 3. 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!
  • 4. 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.
  • 5. 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!
  • 6. 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!
  • 7. Commit.where.not(liked_by: ‘DHH’) Commit.where.like(author: ‘cla%’) Finally, the best idea arrives: keept only one ‘where’ and make it chainable.
  • 8. Rails 3 where(“author <> ‘DHH’”) @de75af7 where.not(author: ‘DHH’) where.not(‘author = “DHH”’) where.not(‘author = ?’, ‘DHH’) Rails 3 where(“author like ‘DH%’”) @de75af7 where.like(author: ‘DH%’) Rails 3 where(“author not like ‘DH%’”) @de75af7 where.not_like(author: ‘DH%’) Initially implemented by @amatsuda. Smells almost right…
  • 9. Rails 3 where(“id <> 42”).order(:id) @de75af7 where.not(id: 42).order(:id) …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.
  • 10. Rails 3 where(“id <> 42”).order(:id) @de75af7 where.not(id: 42).order(:id) where.order(:id).not(id: 42) …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.
  • 11. So I made a pull request to use a builder class rather than a module.
  • 12. Rails 3 where(“id <> 42”) @de75af7 where.not(id: 42) Rails 3 where(“id > 42”) ??? where.gt(id: 42) ??? where.greater_than(id: 42) Rails 3 where(“id <= 42”) ??? where.lte(id: 42) ??? where.less_than_or_equal(id: 42) Moreover, if we start adding inequalities, than why not adding ALL of them?
  • 13. 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.
  • 14. Rails 3 where(“author <> ‘DHH’”) @de75af7 where.not(author: ‘DHH’) where.not(‘author = “DHH”’) where.not(‘author = ?’, ‘DHH’) Rails 3 where(“author like ‘DH%’”) @de75af7 where.like(author: ‘DH%’) Rails 3 where(“author not like ‘DH%’”) @de75af7 where.not_like(author: ‘DH%’) 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.
  • 15. Rails 3 where(“author <> ‘DHH’”) @de75af7 where.not(author: ‘DHH’) where.not(‘author = “DHH”’) where.not(‘author = ?’, ‘DHH’) Rails 3 where(“author like ‘DH%’”) @de75af7 where.like(author: ‘DH%’) Rails 3 where(“author not like ‘DH%’”) @de75af7 where.not_like(author: ‘DH%’) 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.
  • 16. Therefore, not powerful enough to belong to the core!
  • 17. TL;DR Rails 3 Commit.where(“liked_by <> ‘DHH’”) Rails 4 Commit.where.not(liked_by: ‘DHH’) 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!