SlideShare a Scribd company logo
1 of 44
Download to read offline
How to f*ck up
the refactoring
The ultimate guide
Saturday, April 6, 13
Situation
Saturday, April 6, 13
•2 developers
Saturday, April 6, 13
•2 developers
•2 projects
Saturday, April 6, 13
•2 developers
•2 projects
•Near the same stage
Saturday, April 6, 13
Project1
• Written by novice
Saturday, April 6, 13
Project1
• Written by novice
• Full of code duplication
Saturday, April 6, 13
Project1
• Written by novice
• Full of code duplication
• Denormalized data structure
Saturday, April 6, 13
Project1
Still maintainable
Saturday, April 6, 13
Project2
• Written by more experienced developer
Saturday, April 6, 13
Project2
• Written by more experienced developer
• No code duplication
Saturday, April 6, 13
Project2
• Written by more experienced developer
• No code duplication
• No data duplication
Saturday, April 6, 13
Project2
Maintainable through rewrite
Saturday, April 6, 13
Saturday, April 6, 13
@ptico
}Github
Twitter
LinkedIn
Andrey Savchenko
Aejis
Saturday, April 6, 13
Refactoring
anti-patterns
Saturday, April 6, 13
“IDD”
Intuition Driven Development
Saturday, April 6, 13
lib/extensions.rb________
class Hash
def to_ostruct
# Convert text to ostruct
end
end
module Wicked::Controller::Concerns::RenderRedirect
def process_resource!(resource)
# processing staff
end
end
Saturday, April 6, 13
app/controllers/application_controller.rb
module AjaxActions
def users_list
render :json => @users
end
end
Saturday, April 6, 13
lib/model_helper.rb
module ModelHelpers
def as_json(*)
super(:include => :logs)
end
end
Saturday, April 6, 13
Howtoprevent
• Do not do refactor unless you know
exactly what you do
• Do not assign refactor tasks to the juniors
• Learn design patterns
Saturday, April 6, 13
“DRY Madness”
Saturday, April 6, 13
DRY does not mean “don’t type the
same characters twice”‟David Chelimsky
Saturday, April 6, 13
Bad
%(foo bar baz).each do |name|
define_method(:"set_#{name}") do |val|
val = val.to_s if name == "bar"
name = :"#{name}_name"
self.prev[name] = self.attributes[name]
self.attributes[name] = val
end
end
Saturday, April 6, 13
Good
def set_attribute(name, val)
prev[name] = attributes[name]
attributes[name] = val
end
def set_foo(val)
set_attribute(:foo_name, val)
end
def set_bar(val)
set_attribute(:bar_name, val.to_s)
end
def set_baz(val)
set_attribute(:baz_name, val)
end
Saturday, April 6, 13
tasks.do_the_task(:special, "foo")
tasks.do_the_task(:ordinal, "bar")
def do_the_task(type, val)
if type == :special
val = convert_val_for_special(val)
end
if type == :ordinal && val.is_a?(String)
self.is_string = true
end
self.type = type
self.state = :done
self.val = val
save
end
Bad
Saturday, April 6, 13
Good
def do_special_task(val)
val = convert_val_for_special(val)
persist_task(:special, val)
end
def do_ordinal_task(val)
self.is_string = true if val.is_a?(String)
persist_task(:ordinal, val)
end
def do_another_task
persist_task(:another, val)
end
def persist_task(type, val)
self.type = type
self.state = :done
self.val = val
save
end
Saturday, April 6, 13
Howtoprevent
• Don’t afraid to type twice - afraid to
implement twice
• Avoid metaprogramming
• Avoid metaprogramming
Saturday, April 6, 13
“Frankenstein”
Saturday, April 6, 13
class PigLivery
def filter(blood)
# do important things
end
end
class Pig
def initialize
@livery = PigLivery.new
# ...
end
end
Saturday, April 6, 13
- class PigLivery
+ class PigHumanLivery
class Human
def initialize
@livery = PigHumanLivery.new
# ...
end
end
def initialize
if for_human?
resize_a_bit
end
end
Saturday, April 6, 13
class FrankensteinMonster
def initialize
@livery = PigHumanLivery.new
@eyes = CatDogEyes.new
@jaw = MonkeyJaw.new
# ...
end
end
Saturday, April 6, 13
Howtoprevent
• Use OOP superpower (subclassing instead
conditions, etc.)
• Follow the interface segregation principle
Saturday, April 6, 13
“General content”
Saturday, April 6, 13
class GeneralContent
# Attrs: title, body, coords, tags
end
class Page < GeneralContent
# Uses: title, body, tags
end
class City < GeneralContent
# Uses: title, body, coords
end
class Hotel < GeneralContent
# Uses: title, coords, stars, tags
end
Saturday, April 6, 13
Howtoprevent
• Do not rebuild data structure while
business requirements is not finalized
• If you consciously do something like this —
try to change your profession
Saturday, April 6, 13
Common tips
Saturday, April 6, 13
Don’trefactor onearly
stages
Saturday, April 6, 13
You have enought time

Saturday, April 6, 13
Learndesign patterns
andprinciples
Saturday, April 6, 13
SOLID
GRASP
YAGNI
Saturday, April 6, 13
Usecommonsense
Saturday, April 6, 13
Saturday, April 6, 13
Questions?
Attributions:
Linecons by Sergey Shmidt
http://www.flickr.com/photos/amyn-design/8548659957/
http://www.flickr.com/photos/greenflames09/100781977/
Andrey Savchenko
Aejis
@ptico
Saturday, April 6, 13

More Related Content

More from Ruby Meditation

More from Ruby Meditation (20)

Use cases for Serverless Technologies - Ruslan Tolstov (RUS) | Ruby Meditatio...
Use cases for Serverless Technologies - Ruslan Tolstov (RUS) | Ruby Meditatio...Use cases for Serverless Technologies - Ruslan Tolstov (RUS) | Ruby Meditatio...
Use cases for Serverless Technologies - Ruslan Tolstov (RUS) | Ruby Meditatio...
 
The Trailblazer Ride from the If Jungle into a Civilised Railway Station - Or...
The Trailblazer Ride from the If Jungle into a Civilised Railway Station - Or...The Trailblazer Ride from the If Jungle into a Civilised Railway Station - Or...
The Trailblazer Ride from the If Jungle into a Civilised Railway Station - Or...
 
What/How to do with GraphQL? - Valentyn Ostakh (ENG) | Ruby Meditation 27
What/How to do with GraphQL? - Valentyn Ostakh (ENG) | Ruby Meditation 27What/How to do with GraphQL? - Valentyn Ostakh (ENG) | Ruby Meditation 27
What/How to do with GraphQL? - Valentyn Ostakh (ENG) | Ruby Meditation 27
 
New features in Rails 6 - Nihad Abbasov (RUS) | Ruby Meditation 26
New features in Rails 6 -  Nihad Abbasov (RUS) | Ruby Meditation 26New features in Rails 6 -  Nihad Abbasov (RUS) | Ruby Meditation 26
New features in Rails 6 - Nihad Abbasov (RUS) | Ruby Meditation 26
 
Security Scanning Overview - Tetiana Chupryna (RUS) | Ruby Meditation 26
Security Scanning Overview - Tetiana Chupryna (RUS) | Ruby Meditation 26Security Scanning Overview - Tetiana Chupryna (RUS) | Ruby Meditation 26
Security Scanning Overview - Tetiana Chupryna (RUS) | Ruby Meditation 26
 
Teach your application eloquence. Logs, metrics, traces - Dmytro Shapovalov (...
Teach your application eloquence. Logs, metrics, traces - Dmytro Shapovalov (...Teach your application eloquence. Logs, metrics, traces - Dmytro Shapovalov (...
Teach your application eloquence. Logs, metrics, traces - Dmytro Shapovalov (...
 
Best practices. Exploring - Ike Kurghinyan (RUS) | Ruby Meditation 26
Best practices. Exploring - Ike Kurghinyan (RUS) | Ruby Meditation 26Best practices. Exploring - Ike Kurghinyan (RUS) | Ruby Meditation 26
Best practices. Exploring - Ike Kurghinyan (RUS) | Ruby Meditation 26
 
Road to A/B testing - Alexey Vasiliev (ENG) | Ruby Meditation 25
Road to A/B testing - Alexey Vasiliev (ENG) | Ruby Meditation 25Road to A/B testing - Alexey Vasiliev (ENG) | Ruby Meditation 25
Road to A/B testing - Alexey Vasiliev (ENG) | Ruby Meditation 25
 
Concurrency in production. Real life example - Dmytro Herasymuk | Ruby Medita...
Concurrency in production. Real life example - Dmytro Herasymuk | Ruby Medita...Concurrency in production. Real life example - Dmytro Herasymuk | Ruby Medita...
Concurrency in production. Real life example - Dmytro Herasymuk | Ruby Medita...
 
Data encryption for Ruby web applications - Dmytro Shapovalov (RUS) | Ruby Me...
Data encryption for Ruby web applications - Dmytro Shapovalov (RUS) | Ruby Me...Data encryption for Ruby web applications - Dmytro Shapovalov (RUS) | Ruby Me...
Data encryption for Ruby web applications - Dmytro Shapovalov (RUS) | Ruby Me...
 
Rails App performance at the limit - Bogdan Gusiev
Rails App performance at the limit - Bogdan GusievRails App performance at the limit - Bogdan Gusiev
Rails App performance at the limit - Bogdan Gusiev
 
GDPR. Next Y2K in 2018? - Anton Tkachov | Ruby Meditation #23
GDPR. Next Y2K in 2018? - Anton Tkachov | Ruby Meditation #23GDPR. Next Y2K in 2018? - Anton Tkachov | Ruby Meditation #23
GDPR. Next Y2K in 2018? - Anton Tkachov | Ruby Meditation #23
 
Postgres vs Elasticsearch while enriching data - Vlad Somov | Ruby Meditaiton...
Postgres vs Elasticsearch while enriching data - Vlad Somov | Ruby Meditaiton...Postgres vs Elasticsearch while enriching data - Vlad Somov | Ruby Meditaiton...
Postgres vs Elasticsearch while enriching data - Vlad Somov | Ruby Meditaiton...
 
Database Sharding in Rails Applications – Vitalik Danchenko | Ruby Meditatio...
 Database Sharding in Rails Applications – Vitalik Danchenko | Ruby Meditatio... Database Sharding in Rails Applications – Vitalik Danchenko | Ruby Meditatio...
Database Sharding in Rails Applications – Vitalik Danchenko | Ruby Meditatio...
 
Building MVP from business owner’s perspective – Piotr Latoszek | Ruby Medita...
Building MVP from business owner’s perspective – Piotr Latoszek | Ruby Medita...Building MVP from business owner’s perspective – Piotr Latoszek | Ruby Medita...
Building MVP from business owner’s perspective – Piotr Latoszek | Ruby Medita...
 
Growing Rails Apps - Dmitry Zhlobo | Ruby Meditation #23
Growing Rails Apps - Dmitry Zhlobo | Ruby Meditation #23Growing Rails Apps - Dmitry Zhlobo | Ruby Meditation #23
Growing Rails Apps - Dmitry Zhlobo | Ruby Meditation #23
 
Residence permit EU - Gennadii Miroshnychenko
Residence permit EU - Gennadii MiroshnychenkoResidence permit EU - Gennadii Miroshnychenko
Residence permit EU - Gennadii Miroshnychenko
 
Practical SOLID with Rails - Andrii Savchenko
Practical SOLID with Rails - Andrii SavchenkoPractical SOLID with Rails - Andrii Savchenko
Practical SOLID with Rails - Andrii Savchenko
 
Open education - Artem Suchov
Open education - Artem SuchovOpen education - Artem Suchov
Open education - Artem Suchov
 
Inside Out Ruby: Using MRI as a C library - Artur Pyrogovskyi
Inside Out Ruby: Using MRI as a C library - Artur PyrogovskyiInside Out Ruby: Using MRI as a C library - Artur Pyrogovskyi
Inside Out Ruby: Using MRI as a C library - Artur Pyrogovskyi
 

Recently uploaded

Recently uploaded (20)

Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
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
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
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...
 
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
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
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)
 
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
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 

How to f*ck_up_the_refactoring - Andrii Savchenko