SlideShare a Scribd company logo
1 of 22
Download to read offline
CODE REFACTOR STRATEGY
byTracy LOISEL, Lead Developer at monAlbumPhoto.fr
Geneva, september the 28th
THANKS
• Many thanks to Geneva Ruby Brigade, the
geneva ruby on rails developers group
• Many thanks to monAlbumPhoto.fr to let me
share with you our experience
WHY CODE REFACTOR ?
• to code faster next time
and not by monkey
patching
• to debug easier next time
without hurting yourself
• to build sustainable code
with services oriented
architecture
AND WHY
A STRATEGY FORTHAT ?
• to be far beyond esthetic
matters and keep developers
in peace
• to share a same behave while
refactoring with team-mates
• to let all your developers take
part of the project, no matter
if they feel confident enough
to refactor code from other
developers
THE CONTEXT AT MAP
• monAlbumPhoto is one the main photos printing services in France
• the company started at 2004 and has been acquired by M6 Group in 2010
• it is one the main Ruby on Rails project in Europe said Jean-DanielVouille, CTO at
famous Echos newsgroup
• 20 developpers work in Paris for the IT department under the management of our
CTO Benjamin Grelié, while we outsource also in Russia and India
• the service is powered by a google cloud architecture with above 20 production
servers
• we have a factory of 5000 squared meters in Picardie (France)
THREE MORE WAYS
TO DON’T REPEAT
EACH OTHER
MODEL CONCERNS
• concerns let you share code between models
• think about an email validation regexp. In a single
app many model would include an email_address
attributes
• standardize its validation inside a concern
CONTROLLER CONCERNS
• concerns let you share code between controllers
too
• like constants or methods called within
before_filter
POLYMORPHIC MODEL
POLYMORPHIC MODEL
• think about price information, in a single app you
could have many objects with price attributes
because they are billable
• now think about the mess if each developer design
his own price for his current model ? how many
files 2 or 3 years after ?
POLYMORPHIC MODEL
• polymorphic model and concern are a solution to
manage a kind of information that is use
elsewhere with not necessary variations (cost vs
value, amount vs total, etc)
• standardize implementation of an information with
a polymorphic model
SERVICE-ORIENTED
ARCHITECTURE
THE BEGINNING
• For long term, with developers coming then leaving, we
want to make monkey patch an irregular way to code
• We want to slim down our controllers and models.
Concerns failed to do it in long terms, it’s not enough
• We want to know instantly where a function is located.
No more magic calls and methods that make too
much, even the coffee.
OUR SPLITS
• /app/models is for relations, validations, search strategy and other strategy
definition
• /app/controllers is to accept HTTP requests and respond with data
• /app/models/concerns is to share code between models
• /app/models/concerns is to share code between controllers
• /app/services is for Service Object that accept input, perform works and
return result
• /app/supports is to extend ActiveRecord::Base with a service object
NAMING CONVENTIONS
• Each concern name is appended with ableConcern term like in
AmountableConcern
• Each module support name is appended with Support term like in
CloningSupport
• Each service object name is appended with Service term like in
UserAddressesService and is prefixed with Active term when it play
for a single model like in ActiveFeedService
• We avoid verb for naming our service object
RULES FOR SERVICE OBJECT
• use instance methods, not class methods
• there should be very few public methods
• method should return rich objects and not boolean
• dependent service objects should be created inside the
initialize or lazily
• during the initialize, declare each options as variable instance
CODE LIKE PATTERN HUNTER
• basically refactoring consists of finding repetitive code and place it
in a dedicated method
• in POO, you refactor class with code patterns
• don’t look for how to refactor a class, it will remains incomplete.
Look for patterns instead and refactor to make the pattern
consistent in the implementation
• A class should have only one reason to change and only one - Gang
of Four.
LIST OF PATTERNS
• Template pattern
• Adapter pattern
• Factory pattern
• Builder pattern
• State pattern
• Proxy pattern
LINKS
• https://www.viget.com/articles/slimming-down-your-models-and-controllers
• http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/
• https://richonrails.com/articles/rails-4-code-concerns-in-active-record-models
• https://launchschool.com/blog/understanding-polymorphic-associations-in-rails
• https://www.instacart.com/opensource
• https://github.com/instacart/amountable
• http://multithreaded.stitchfix.com/blog/2015/06/02/anatomy-of-service-objects-in-rails/
• SUGAR : https://www.thc.org/root/phun/unmaintain.html

More Related Content

What's hot

Java and effective programming. Is it possible? - IAESTE Case Week 2016
Java and effective programming. Is it possible? - IAESTE Case Week 2016Java and effective programming. Is it possible? - IAESTE Case Week 2016
Java and effective programming. Is it possible? - IAESTE Case Week 2016Łukasz Koniecki
 
Structural Design pattern - Adapter
Structural Design pattern - AdapterStructural Design pattern - Adapter
Structural Design pattern - AdapterManoj Kumar
 
Test Driven Development in CQ5/AEM
Test Driven Development in CQ5/AEMTest Driven Development in CQ5/AEM
Test Driven Development in CQ5/AEMSagar Sane
 
Introduction to Bdd and cucumber
Introduction to Bdd and cucumberIntroduction to Bdd and cucumber
Introduction to Bdd and cucumberNibu Baby
 

What's hot (9)

Java and effective programming. Is it possible? - IAESTE Case Week 2016
Java and effective programming. Is it possible? - IAESTE Case Week 2016Java and effective programming. Is it possible? - IAESTE Case Week 2016
Java and effective programming. Is it possible? - IAESTE Case Week 2016
 
API-First Design and Django
API-First Design and DjangoAPI-First Design and Django
API-First Design and Django
 
Structural Design pattern - Adapter
Structural Design pattern - AdapterStructural Design pattern - Adapter
Structural Design pattern - Adapter
 
Adapter Design Pattern
Adapter Design PatternAdapter Design Pattern
Adapter Design Pattern
 
Test Driven Development in CQ5/AEM
Test Driven Development in CQ5/AEMTest Driven Development in CQ5/AEM
Test Driven Development in CQ5/AEM
 
Developing for Developers
Developing for DevelopersDeveloping for Developers
Developing for Developers
 
Introduction to Bdd and cucumber
Introduction to Bdd and cucumberIntroduction to Bdd and cucumber
Introduction to Bdd and cucumber
 
Adapter pattern
Adapter patternAdapter pattern
Adapter pattern
 
Ns e commerce integration tests
Ns e commerce integration testsNs e commerce integration tests
Ns e commerce integration tests
 

Viewers also liked

Presentation skills - How to present with power in any situation
Presentation skills - How to present with power in any situationPresentation skills - How to present with power in any situation
Presentation skills - How to present with power in any situationMahmoud Alaa
 
Building a ppmo from scratch
Building a ppmo from scratchBuilding a ppmo from scratch
Building a ppmo from scratchJane Cox
 
Mosaic - Event for Dupont
Mosaic - Event for DupontMosaic - Event for Dupont
Mosaic - Event for DupontYogesh S
 
Osce – the organization of security and cooperation
Osce – the organization of security and cooperationOsce – the organization of security and cooperation
Osce – the organization of security and cooperationHoi Yu, Sophie Cheng
 
Leave of Absence Reduction improvement process- L.O.A. Reduction
Leave of Absence Reduction improvement process-  L.O.A. ReductionLeave of Absence Reduction improvement process-  L.O.A. Reduction
Leave of Absence Reduction improvement process- L.O.A. ReductionJason Peart
 
SPC for National Community Policing Conference Kerala
SPC for National Community Policing Conference KeralaSPC for National Community Policing Conference Kerala
SPC for National Community Policing Conference KeralaPeople's Trust, Jaipur
 
SE OJK LITERASI KEUANGAN KEPADA KONSUMEN
SE OJK LITERASI KEUANGAN KEPADA KONSUMENSE OJK LITERASI KEUANGAN KEPADA KONSUMEN
SE OJK LITERASI KEUANGAN KEPADA KONSUMENDedy Dwi Setyawan
 

Viewers also liked (11)

Presentation skills - How to present with power in any situation
Presentation skills - How to present with power in any situationPresentation skills - How to present with power in any situation
Presentation skills - How to present with power in any situation
 
China and the Arctic
China and the ArcticChina and the Arctic
China and the Arctic
 
Mi Visita a Seconlife
Mi Visita a SeconlifeMi Visita a Seconlife
Mi Visita a Seconlife
 
Building a ppmo from scratch
Building a ppmo from scratchBuilding a ppmo from scratch
Building a ppmo from scratch
 
Data Manager
Data ManagerData Manager
Data Manager
 
Mosaic - Event for Dupont
Mosaic - Event for DupontMosaic - Event for Dupont
Mosaic - Event for Dupont
 
Osce – the organization of security and cooperation
Osce – the organization of security and cooperationOsce – the organization of security and cooperation
Osce – the organization of security and cooperation
 
Leave of Absence Reduction improvement process- L.O.A. Reduction
Leave of Absence Reduction improvement process-  L.O.A. ReductionLeave of Absence Reduction improvement process-  L.O.A. Reduction
Leave of Absence Reduction improvement process- L.O.A. Reduction
 
SPC for National Community Policing Conference Kerala
SPC for National Community Policing Conference KeralaSPC for National Community Policing Conference Kerala
SPC for National Community Policing Conference Kerala
 
CSR-EU
CSR-EUCSR-EU
CSR-EU
 
SE OJK LITERASI KEUANGAN KEPADA KONSUMEN
SE OJK LITERASI KEUANGAN KEPADA KONSUMENSE OJK LITERASI KEUANGAN KEPADA KONSUMEN
SE OJK LITERASI KEUANGAN KEPADA KONSUMEN
 

Similar to Code refactor strategy part #1

API Developer Experience: Why it Matters, and How Documenting Your API with S...
API Developer Experience: Why it Matters, and How Documenting Your API with S...API Developer Experience: Why it Matters, and How Documenting Your API with S...
API Developer Experience: Why it Matters, and How Documenting Your API with S...SmartBear
 
DevOps - Orientation Session
DevOps - Orientation SessionDevOps - Orientation Session
DevOps - Orientation SessionM M Nair
 
Introduction To Design Patterns Class 4 Composition vs Inheritance
 Introduction To Design Patterns Class 4 Composition vs Inheritance Introduction To Design Patterns Class 4 Composition vs Inheritance
Introduction To Design Patterns Class 4 Composition vs InheritanceBlue Elephant Consulting
 
Why DevOps Technology is the Most Popular
Why DevOps Technology is the Most PopularWhy DevOps Technology is the Most Popular
Why DevOps Technology is the Most PopularRavendra Singh
 
Kevin Whinnery: Best Practices for Cross-Platform Mobile Development
Kevin Whinnery: Best Practices for Cross-Platform Mobile DevelopmentKevin Whinnery: Best Practices for Cross-Platform Mobile Development
Kevin Whinnery: Best Practices for Cross-Platform Mobile DevelopmentAxway Appcelerator
 
SOA with Zend Framework
SOA with Zend FrameworkSOA with Zend Framework
SOA with Zend FrameworkMike Willbanks
 
Coordinating Micro-Services with Spring Cloud Contract
Coordinating Micro-Services with Spring Cloud ContractCoordinating Micro-Services with Spring Cloud Contract
Coordinating Micro-Services with Spring Cloud ContractOmri Spector
 
Code like a ninja session 3 open-closed principle
Code like a ninja  session 3   open-closed principleCode like a ninja  session 3   open-closed principle
Code like a ninja session 3 open-closed principleDeon Meyer
 
OOP -interface and objects.pptx
OOP -interface and objects.pptxOOP -interface and objects.pptx
OOP -interface and objects.pptxEdFeranil
 
[Annotated] QConSF 2018: Airbnb's Great Migration - From Monolith to Service-...
[Annotated] QConSF 2018: Airbnb's Great Migration - From Monolith to Service-...[Annotated] QConSF 2018: Airbnb's Great Migration - From Monolith to Service-...
[Annotated] QConSF 2018: Airbnb's Great Migration - From Monolith to Service-...Jessica Tai
 
Building a REST API for Longevity
Building a REST API for LongevityBuilding a REST API for Longevity
Building a REST API for LongevityMuleSoft
 
Edpuzzle - Migrating to React progressively but painlessly
Edpuzzle - Migrating to React progressively but painlesslyEdpuzzle - Migrating to React progressively but painlessly
Edpuzzle - Migrating to React progressively but painlesslySantiago Herrero Bajo
 
Mobile Code Optimisation Services
Mobile Code Optimisation ServicesMobile Code Optimisation Services
Mobile Code Optimisation ServicesRaja Nagendra Kumar
 
CarTrawler's Feature Team Architecture and Development Process Showcase by Lu...
CarTrawler's Feature Team Architecture and Development Process Showcase by Lu...CarTrawler's Feature Team Architecture and Development Process Showcase by Lu...
CarTrawler's Feature Team Architecture and Development Process Showcase by Lu...Lucas Sacramento
 
Next Generation Architecture Showcase July 2019
Next Generation Architecture Showcase July 2019Next Generation Architecture Showcase July 2019
Next Generation Architecture Showcase July 2019Alan Pearson Mathews
 
Segue to design patterns
Segue to design patternsSegue to design patterns
Segue to design patternsRahul Singh
 
An Introduction To Software Development - Implementation
An Introduction To Software Development - ImplementationAn Introduction To Software Development - Implementation
An Introduction To Software Development - ImplementationBlue Elephant Consulting
 

Similar to Code refactor strategy part #1 (20)

API Developer Experience: Why it Matters, and How Documenting Your API with S...
API Developer Experience: Why it Matters, and How Documenting Your API with S...API Developer Experience: Why it Matters, and How Documenting Your API with S...
API Developer Experience: Why it Matters, and How Documenting Your API with S...
 
DevOps - Orientation Session
DevOps - Orientation SessionDevOps - Orientation Session
DevOps - Orientation Session
 
Introduction To Design Patterns Class 4 Composition vs Inheritance
 Introduction To Design Patterns Class 4 Composition vs Inheritance Introduction To Design Patterns Class 4 Composition vs Inheritance
Introduction To Design Patterns Class 4 Composition vs Inheritance
 
Why DevOps Technology is the Most Popular
Why DevOps Technology is the Most PopularWhy DevOps Technology is the Most Popular
Why DevOps Technology is the Most Popular
 
Kevin Whinnery: Best Practices for Cross-Platform Mobile Development
Kevin Whinnery: Best Practices for Cross-Platform Mobile DevelopmentKevin Whinnery: Best Practices for Cross-Platform Mobile Development
Kevin Whinnery: Best Practices for Cross-Platform Mobile Development
 
SOA with Zend Framework
SOA with Zend FrameworkSOA with Zend Framework
SOA with Zend Framework
 
Coordinating Micro-Services with Spring Cloud Contract
Coordinating Micro-Services with Spring Cloud ContractCoordinating Micro-Services with Spring Cloud Contract
Coordinating Micro-Services with Spring Cloud Contract
 
Code like a ninja session 3 open-closed principle
Code like a ninja  session 3   open-closed principleCode like a ninja  session 3   open-closed principle
Code like a ninja session 3 open-closed principle
 
OOP -interface and objects.pptx
OOP -interface and objects.pptxOOP -interface and objects.pptx
OOP -interface and objects.pptx
 
[Annotated] QConSF 2018: Airbnb's Great Migration - From Monolith to Service-...
[Annotated] QConSF 2018: Airbnb's Great Migration - From Monolith to Service-...[Annotated] QConSF 2018: Airbnb's Great Migration - From Monolith to Service-...
[Annotated] QConSF 2018: Airbnb's Great Migration - From Monolith to Service-...
 
Building a REST API for Longevity
Building a REST API for LongevityBuilding a REST API for Longevity
Building a REST API for Longevity
 
Edpuzzle - Migrating to React progressively but painlessly
Edpuzzle - Migrating to React progressively but painlesslyEdpuzzle - Migrating to React progressively but painlessly
Edpuzzle - Migrating to React progressively but painlessly
 
android principle.pptx
android principle.pptxandroid principle.pptx
android principle.pptx
 
Mobile Code Optimisation Services
Mobile Code Optimisation ServicesMobile Code Optimisation Services
Mobile Code Optimisation Services
 
CarTrawler's Feature Team Architecture and Development Process Showcase by Lu...
CarTrawler's Feature Team Architecture and Development Process Showcase by Lu...CarTrawler's Feature Team Architecture and Development Process Showcase by Lu...
CarTrawler's Feature Team Architecture and Development Process Showcase by Lu...
 
Next Generation Architecture Showcase July 2019
Next Generation Architecture Showcase July 2019Next Generation Architecture Showcase July 2019
Next Generation Architecture Showcase July 2019
 
Aspect Oriented Programming
Aspect Oriented ProgrammingAspect Oriented Programming
Aspect Oriented Programming
 
An introduction to DevOps
An introduction to DevOpsAn introduction to DevOps
An introduction to DevOps
 
Segue to design patterns
Segue to design patternsSegue to design patterns
Segue to design patterns
 
An Introduction To Software Development - Implementation
An Introduction To Software Development - ImplementationAn Introduction To Software Development - Implementation
An Introduction To Software Development - Implementation
 

Recently uploaded

Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 

Recently uploaded (20)

Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 

Code refactor strategy part #1

  • 1. CODE REFACTOR STRATEGY byTracy LOISEL, Lead Developer at monAlbumPhoto.fr Geneva, september the 28th
  • 2. THANKS • Many thanks to Geneva Ruby Brigade, the geneva ruby on rails developers group • Many thanks to monAlbumPhoto.fr to let me share with you our experience
  • 3. WHY CODE REFACTOR ? • to code faster next time and not by monkey patching • to debug easier next time without hurting yourself • to build sustainable code with services oriented architecture
  • 4. AND WHY A STRATEGY FORTHAT ? • to be far beyond esthetic matters and keep developers in peace • to share a same behave while refactoring with team-mates • to let all your developers take part of the project, no matter if they feel confident enough to refactor code from other developers
  • 5. THE CONTEXT AT MAP • monAlbumPhoto is one the main photos printing services in France • the company started at 2004 and has been acquired by M6 Group in 2010 • it is one the main Ruby on Rails project in Europe said Jean-DanielVouille, CTO at famous Echos newsgroup • 20 developpers work in Paris for the IT department under the management of our CTO Benjamin Grelié, while we outsource also in Russia and India • the service is powered by a google cloud architecture with above 20 production servers • we have a factory of 5000 squared meters in Picardie (France)
  • 6. THREE MORE WAYS TO DON’T REPEAT EACH OTHER
  • 7. MODEL CONCERNS • concerns let you share code between models • think about an email validation regexp. In a single app many model would include an email_address attributes • standardize its validation inside a concern
  • 8.
  • 9. CONTROLLER CONCERNS • concerns let you share code between controllers too • like constants or methods called within before_filter
  • 10.
  • 12. POLYMORPHIC MODEL • think about price information, in a single app you could have many objects with price attributes because they are billable • now think about the mess if each developer design his own price for his current model ? how many files 2 or 3 years after ?
  • 13. POLYMORPHIC MODEL • polymorphic model and concern are a solution to manage a kind of information that is use elsewhere with not necessary variations (cost vs value, amount vs total, etc) • standardize implementation of an information with a polymorphic model
  • 14.
  • 16. THE BEGINNING • For long term, with developers coming then leaving, we want to make monkey patch an irregular way to code • We want to slim down our controllers and models. Concerns failed to do it in long terms, it’s not enough • We want to know instantly where a function is located. No more magic calls and methods that make too much, even the coffee.
  • 17. OUR SPLITS • /app/models is for relations, validations, search strategy and other strategy definition • /app/controllers is to accept HTTP requests and respond with data • /app/models/concerns is to share code between models • /app/models/concerns is to share code between controllers • /app/services is for Service Object that accept input, perform works and return result • /app/supports is to extend ActiveRecord::Base with a service object
  • 18. NAMING CONVENTIONS • Each concern name is appended with ableConcern term like in AmountableConcern • Each module support name is appended with Support term like in CloningSupport • Each service object name is appended with Service term like in UserAddressesService and is prefixed with Active term when it play for a single model like in ActiveFeedService • We avoid verb for naming our service object
  • 19. RULES FOR SERVICE OBJECT • use instance methods, not class methods • there should be very few public methods • method should return rich objects and not boolean • dependent service objects should be created inside the initialize or lazily • during the initialize, declare each options as variable instance
  • 20. CODE LIKE PATTERN HUNTER • basically refactoring consists of finding repetitive code and place it in a dedicated method • in POO, you refactor class with code patterns • don’t look for how to refactor a class, it will remains incomplete. Look for patterns instead and refactor to make the pattern consistent in the implementation • A class should have only one reason to change and only one - Gang of Four.
  • 21. LIST OF PATTERNS • Template pattern • Adapter pattern • Factory pattern • Builder pattern • State pattern • Proxy pattern
  • 22. LINKS • https://www.viget.com/articles/slimming-down-your-models-and-controllers • http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/ • https://richonrails.com/articles/rails-4-code-concerns-in-active-record-models • https://launchschool.com/blog/understanding-polymorphic-associations-in-rails • https://www.instacart.com/opensource • https://github.com/instacart/amountable • http://multithreaded.stitchfix.com/blog/2015/06/02/anatomy-of-service-objects-in-rails/ • SUGAR : https://www.thc.org/root/phun/unmaintain.html