SlideShare une entreprise Scribd logo
1  sur  55
Let’s Refactor Some Ruby Code
Ana María Martínez Gómez
http://anamaria.martinezgomez.name | anamma06@gmail.com | @anamma_06
Ruby and I are turning 25
https://build.opensuse.or
g
https://github.com/openSUSE/open-build-
service
OBS = Open Build Service
Let’s Refactor Some Ruby Code
Scary comment
src/api/lib/activexml/node.rb
src/api/lib/activexml/node.rb
src/api/lib/activexml/node.rb
https://github.com/openSUSE/open-build-
service/pull/3947
src/api/lib/activexml/node.r
b
comments are a bad ideacomments are a bad idea
nodocnodoc
protectedprotected
Reusing code
piece_of_code_to_reuse (param1, param2)
piece_of_code_to_reuse
piece_of_code_to_reuse
"Param1_1",
params1_2
"Param2_1",
params2_2
def
# code to be reused which uses two
parameters
code to be reused which uses two
parametersend
piece_of_code_to_reuse (param1, param2)
piece_of_code_to_reuse
piece_of_code_to_reuse
"Param1_1",
params1_2
"Param2_1",
params2_2
def
# code to be reused which uses two
parametersend
{ "Param1_1" => params1_2, "Params2_1" =>
params2_2 }
param1 =
key.to_s# code to be reused which uses two
parametersend
.each do |key,
param2|
param1 =
key.capitalize.to_s
{ param1_1: params1_2, params2_1: params2_2 }.each do |key,
param2|
{ Param1_1: params1_2, Params2_1: params2_2 }.each do |key,
param2|
https://github.com/openSUSE/open-build-
service/pull/4105
src/api/app/models/kiwi/image.rb
https://github.com/openSUSE/open-build-
service/pull/4119
src/api/app/models/kiwi/image.rb
Logical conditions
src/api/app/models/flag.rb
ONLY ONE OF THEM IS TRUE
https://github.com/openSUSE/open-build-
service/pull/2324
src/api/app/models/flag.rb
concat + uniq
https://github.com/openSUSE/open-build-
service/pull/4104
src/api/lib/backend/api/build_results/binaries.rb
https://github.com/openSUSE/open-build-
service/pull/4104
src/api/lib/backend/api/build_results/binaries.rb
https://github.com/openSUSE/open-build-
service/pull/4104
src/api/lib/backend/api/build_results/binaries.rb
https://github.com/openSUSE/open-build-
service/pull/4104
src/api/lib/backend/api/build_results/binaries.rb
a + ba + b
a.concat ba.concat b
a + b + ca + b + c
a.concat b, ca.concat b, c
a | ba | b
a.union ba.union b
a | b | ca | b | c
a.union b, ca.union b, c
https://bugs.ruby-
lang.org/issues/14097
StandStand
up!up!
array1.concat(array2,
array3).uniq!
array1 = (array1 | array2 |
array3)
array1.union(array2, array
3)
11
22
33
https://bugs.ruby-
lang.org/issues/14097
https://github.com/ruby/ruby/pull/1747
Yak
ShavingAny apparently useless activity which, by allowing you to overcome
intermediate difficulties, allows you to solve a larger problem
Refactor
OBS
horrible
code
Add new
method to Ruby
Improve
Ruby Array
efficiency
https://github.com/ruby/ruby/pull/1756
Ruby is using SVNRuby is using SVN
We work with peopleWe work with people
Every team works differentlyEvery team works differently
I need to be more patientI need to be more patient
It is a
reasonable
proposal
https://bugs.ruby-lang.org/issues/14097https://bugs.ruby-lang.org/issues/14097
Rails helpers
https://github.com/openSUSE/open-build-
service/pull/5567
["Executive", "Developer", "Consultant", "Strategist", "Agent", "Producer", "Executive", "Officer", "Agent", "Director", "Orchestrator",
"Administrator", "Officer", "Strategist", "Analyst", "Officer", "Liaison", "Designer", "Producer", "Orchestrator", "Director", "Representative",
"Facilitator", "Strategist", "Representative", "Agent", "Orchestrator", "Associate", "Agent", "Coordinator", "Developer", "Strategist", "Technician",
"Consultant", "Facilitator", "Planner", "Executive", "Strategist", "Facilitator", "Specialist", "Strategist", "Producer", "Executive", "Agent", "Planner",
"Specialist", "Orchestrator", "Orchestrator", "Producer", "Agent"]
[["Executive", "executive"], ["Developer", "developer"], ["Consultant", "consultant"], ["Strategist", "strategist"], ["Agent", "agent"], ["Producer",
"producer"], ["Executive", "executive"], ["Officer", "officer"], ["Agent", "agent"], ["Director", "director"], ["Orchestrator", "orchestrator"],
["Administrator", "administrator"], ["Officer", "officer"], ["Strategist", "strategist"], ["Analyst", "analyst"], ["Officer", "officer"], ["Liaison", "liaison"],
["Designer", "designer"], ["Producer", "producer"], ["Orchestrator", "orchestrator"], ["Director", "director"], ["Representative", "representative"],
["Facilitator", "facilitator"], ["Strategist", "strategist"], ["Representative", "representative"], ["Agent", "agent"], ["Orchestrator", "orchestrator"],
["Associate", "associate"], ["Agent", "agent"], ["Coordinator", "coordinator"], ["Developer", "developer"], ["Strategist", "strategist"], ["Technician",
"technician"], ["Consultant", "consultant"], ["Facilitator", "facilitator"], ["Planner", "planner"], ["Executive", "executive"], ["Strategist", "strategist"],
["Facilitator", "facilitator"], ["Specialist", "specialist"], ["Strategist", "strategist"], ["Producer", "producer"], ["Executive", "executive"], ["Agent",
"agent"], ["Planner", "planner"], ["Specialist", "specialist"], ["Orchestrator", "orchestrator"], ["Orchestrator", "orchestrator"], ["Producer", "producer"],
["Agent", "agent"]]
Can it be improved?Can it be improved?
https://github.com/openSUSE/open-build-
service/pull/5567
- improved code- improved code
- options_from_collection_for_select- options_from_collection_for_select
- itself- itself
- but that’s not all...- but that’s not all...
https://github.com/rails/rails/issues/3354
6
Rails helpers areRails helpers are
exposing privateexposing private
methods!methods!
https://github.com/rails/rails/pull/33547
GETGET
INVOLVED!INVOLVED!
Thanks!
http://anamaria.martinezgomez.name | anamma06@gmail.com | @anamma_06

Contenu connexe

Similaire à Let's refactor some Ruby code - EuRuKo 2018

Rails Presentation (Anton Dmitriyev)
Rails Presentation (Anton Dmitriyev)Rails Presentation (Anton Dmitriyev)
Rails Presentation (Anton Dmitriyev)
True-Vision
 
The liferay case: lessons learned evolving from RPC to Hypermedia REST APIs
The liferay case: lessons learned evolving from RPC to Hypermedia REST APIsThe liferay case: lessons learned evolving from RPC to Hypermedia REST APIs
The liferay case: lessons learned evolving from RPC to Hypermedia REST APIs
Jorge Ferrer
 
Instruments ruby on rails
Instruments ruby on railsInstruments ruby on rails
Instruments ruby on rails
pmashchak
 

Similaire à Let's refactor some Ruby code - EuRuKo 2018 (20)

Merb For The Enterprise
Merb For The EnterpriseMerb For The Enterprise
Merb For The Enterprise
 
Ruby on Rails Presentation
Ruby on Rails PresentationRuby on Rails Presentation
Ruby on Rails Presentation
 
Rails Presentation (Anton Dmitriyev)
Rails Presentation (Anton Dmitriyev)Rails Presentation (Anton Dmitriyev)
Rails Presentation (Anton Dmitriyev)
 
The liferay case: lessons learned evolving from RPC to Hypermedia REST APIs
The liferay case: lessons learned evolving from RPC to Hypermedia REST APIsThe liferay case: lessons learned evolving from RPC to Hypermedia REST APIs
The liferay case: lessons learned evolving from RPC to Hypermedia REST APIs
 
Rspec and Capybara Intro Tutorial at RailsConf 2013
Rspec and Capybara Intro Tutorial at RailsConf 2013Rspec and Capybara Intro Tutorial at RailsConf 2013
Rspec and Capybara Intro Tutorial at RailsConf 2013
 
Ruby On Rails Introduction
Ruby On Rails IntroductionRuby On Rails Introduction
Ruby On Rails Introduction
 
Ams adapters
Ams adaptersAms adapters
Ams adapters
 
All Day DevOps 2023 - Implementing OSSF Scorecards Across an Organisation.pdf
All Day DevOps 2023 - Implementing OSSF Scorecards Across an Organisation.pdfAll Day DevOps 2023 - Implementing OSSF Scorecards Across an Organisation.pdf
All Day DevOps 2023 - Implementing OSSF Scorecards Across an Organisation.pdf
 
Toolbox of a Ruby Team
Toolbox of a Ruby TeamToolbox of a Ruby Team
Toolbox of a Ruby Team
 
Aspect-oriented programming in Perl
Aspect-oriented programming in PerlAspect-oriented programming in Perl
Aspect-oriented programming in Perl
 
The Future of Dependency Management for Ruby
The Future of Dependency Management for RubyThe Future of Dependency Management for Ruby
The Future of Dependency Management for Ruby
 
Introduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman OrtegaIntroduction to Rails - presented by Arman Ortega
Introduction to Rails - presented by Arman Ortega
 
The story of language development
The story of language developmentThe story of language development
The story of language development
 
Crossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end FrameworkCrossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end Framework
 
Instruments ruby on rails
Instruments ruby on railsInstruments ruby on rails
Instruments ruby on rails
 
Efektivni vyvoj webovych aplikaci v Ruby on Rails (Webexpo)
Efektivni vyvoj webovych aplikaci v Ruby on Rails (Webexpo)Efektivni vyvoj webovych aplikaci v Ruby on Rails (Webexpo)
Efektivni vyvoj webovych aplikaci v Ruby on Rails (Webexpo)
 
Php resque
Php resquePhp resque
Php resque
 
Great APIs - Future of Your Progress App
Great APIs - Future of Your Progress AppGreat APIs - Future of Your Progress App
Great APIs - Future of Your Progress App
 
Elasticsearch intro output
Elasticsearch intro outputElasticsearch intro output
Elasticsearch intro output
 
Origins of Serverless
Origins of ServerlessOrigins of Serverless
Origins of Serverless
 

Dernier

AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
VictorSzoltysek
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 

Dernier (20)

%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456LEVEL 5   - SESSION 1 2023 (1).pptx - PDF 123456
LEVEL 5 - SESSION 1 2023 (1).pptx - PDF 123456
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptxBUS PASS MANGEMENT SYSTEM USING PHP.pptx
BUS PASS MANGEMENT SYSTEM USING PHP.pptx
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
Pharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodologyPharm-D Biostatistics and Research methodology
Pharm-D Biostatistics and Research methodology
 
ManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide DeckManageIQ - Sprint 236 Review - Slide Deck
ManageIQ - Sprint 236 Review - Slide Deck
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 

Let's refactor some Ruby code - EuRuKo 2018

Notes de l'éditeur

  1. 1 month before first Rails release (december 2005). 13 years ago
  2. The Open Build Service (OBS) is a system to build and distribute binary packages from sources
  3. wrote in 2011
  4. Nodoc: public methods not designed for user consumption – don’t want users to rely on this method
  5. Less than one year - october
  6. - We don’t want that both are true - We want that at least one is true exclusive or is known as xor in mathematics, hardware and many other programming languages
  7. We add an error except if only one of them is true
  8. Bitwise OR
  9. Same for the difference