SlideShare ist ein Scribd-Unternehmen logo
1 von 15
Downloaden Sie, um offline zu lesen
July 14, 2015
Talking to strangers causes train wrecks
Mike Toppa
CTO, Poka Yoke Design
@mtoppa
http://pokayoke.design
My house, yesterday
I just moved to Boston from Philly. I took this picture yesterday. It looks pretty much the same today.
Goal: unpack this quote
“Mockist testers do talk more about avoiding 'train wrecks' - method
chains of style getThis().getThat().getTheOther(). Avoiding method
chains is also known as following the Law of Demeter. While method
chains are a smell, the opposite problem of middle men objects bloated
with forwarding methods is also a smell. (I've always felt I'd be more
comfortable with the Law of Demeter if it were called the Suggestion of
Demeter.)”
Martin Fowler, 2004
Mocks aren’t Stubs
The Law of Demeter, I
✤ Your method can call other methods in its class directly
def full_name
"#{self.first_name} #{self.last_name}"
end
✤ You can play with yourself
From c2.com "Law of Demeter"
Back in the 1980s, a group of programmers working on a project called the Demeter system realized that certain qualities in their object-oriented code led
to the code being easier to maintain and change. [from Avdi Grimm]
Analogy by Peter VanRooijen
The Law of Demeter, II
✤ Your method can call methods on its own fields directly (but not on
the fields' fields)
user.department.name
✤ You can play with your own toys (but you can’t take them apart)
From c2.com "Law of Demeter"
The Law of Demeter, III
✤ When your method takes parameters, your method can call methods
on those parameters directly
def user_info(user)
"Name: #{user.full_name}"
end
✤ You can play with toys that were given to you
From c2.com "Law of Demeter"
The Law of Demeter, IV
✤ When your method creates local objects, that method can call
methods on the local objects
# Sorry, I can’t give an example without
# going on a tangent about dependency injection
✤ And you can play with toys you’ve made yourself
From c2.com "Law of Demeter"
Violation example in a Rails project
In Ruby, and Rails, this kind of chaining is so easy to do, you might not realize the implications of what you’re doing. There are 3 concerns to emphasize:
1. This code is brittle. If any component of the method chain changes, this line of code, and any others like it, will break. Coupling is high, and information
is not localized.
2. In the context of ActiveRecord, this approach has the potential to yield horrific, poorly performing underlying SQL queries.
3. If the code was in a method, it would be challenging to write a unit test for it
Testing with train wrecks
def user_info(user)
"Name: #{user.name}. "
"Boss: #{user.department.try(:head).try(:name)}"
end
From Demeter: It’s not just a good idea. It’s the law.
This example is from Avdi Grimm
describe '#user_info' do
subject { user_info(user) }
let(:user) {
stub(
'user',
name: "Bob",
department:
stub(
name: "Accounting",
head: stub(
name: "Jack",
position: stub(title: "Vice President")
)
),
division: stub(
name: "Microwave Oven Programming"
)
),
position: stub(title: "Senior Bean Counter")
)
}
# examples...
end
From Demeter: It’s not just a good idea. It’s the law.
Alternately, you could try to do this without mocks, and use factories or fixtures instead. But that just moves the brittleness into your test data setup.
Reducing Rails train wrecks:
Delegation
From Demeter: It’s not just a good idea. It’s the law.
class User
delegate :name, to: :department, prefix: true, allow_nil: true
end
…
def user_info(user)
"Name: #{user.name}. Dept: #{user.department_name}"
end
While Demeter allows us to play with our friends, delegation can still help reduce brittleness. If there is a change in the relationship between users and
department, we can create a department_name method that expresses the new logic, without having to change calling code. For example, if users can now
be part of more than one department.
Reducing Rails train wrecks:
has many :through
class User < ActiveRecord::Base
has_many :departments
has_many :divisions, through: :departments
end
Now we can treat divisions as an immediate collaborator. You can say this is cheating, but the point is, information about divisions is now localized. If
something changes in the relationship to divisions, we can replace the “has many through” relationship with a “divisions” method.
Reducing Rails train wrecks:
nested has many :through
class Candidate < ActiveRecord::Base
has_many :campaigns
has_many :races, through: :campaigns
has_many :sought_offices, through: :races
end
Fowler’s conundrum:
Demeter as suggestion, or law?
✤ With nested has many :through, now we can say:
✤ candidate.sought_offices
✤ With Rails, we don’t need “middle men objects bloated with
forwarding methods” to access distant collaborators
✤ The question I’ll leave you to ponder is:
✤ Is this a good solution to Fowler’s conundrum?
ポカヨケ
http://pokayoke.design
@mtoppa
My wife has a new job in Boston, so we’re moving, and I’ll be starting a new job too. I’ll be co-founder and CTO of Poka Yoke Design, and I’m looking forward to working
more with WordPress again. My partner is based in Memphis, so I’m very pleased to be strengthening my ties to the WordPress community here in Tennessee.

Weitere ähnliche Inhalte

Ähnlich wie Talking to strangers causes train wrecks

CHAPTER 6FunctionsChapter Topics6.1 Introductio.docx
CHAPTER 6FunctionsChapter Topics6.1  Introductio.docxCHAPTER 6FunctionsChapter Topics6.1  Introductio.docx
CHAPTER 6FunctionsChapter Topics6.1 Introductio.docx
robertad6
 
creating portable social networks with microformats
creating portable social networks with microformatscreating portable social networks with microformats
creating portable social networks with microformats
elliando dias
 
Uses & Abuses of Mocks & Stubs
Uses & Abuses of Mocks & StubsUses & Abuses of Mocks & Stubs
Uses & Abuses of Mocks & Stubs
PatchSpace Ltd
 

Ähnlich wie Talking to strangers causes train wrecks (20)

On Coding Guidelines
On Coding GuidelinesOn Coding Guidelines
On Coding Guidelines
 
CHAPTER 6FunctionsChapter Topics6.1 Introductio.docx
CHAPTER 6FunctionsChapter Topics6.1  Introductio.docxCHAPTER 6FunctionsChapter Topics6.1  Introductio.docx
CHAPTER 6FunctionsChapter Topics6.1 Introductio.docx
 
The Modlet Pattern
The Modlet PatternThe Modlet Pattern
The Modlet Pattern
 
How Can Software Engineering Support AI
How Can Software Engineering Support AIHow Can Software Engineering Support AI
How Can Software Engineering Support AI
 
Patterns of Semantic Integration
Patterns of Semantic IntegrationPatterns of Semantic Integration
Patterns of Semantic Integration
 
Multi-threaded web crawler in Ruby
Multi-threaded web crawler in RubyMulti-threaded web crawler in Ruby
Multi-threaded web crawler in Ruby
 
C# interview
C# interviewC# interview
C# interview
 
Ruby on rails delegate
Ruby on rails delegateRuby on rails delegate
Ruby on rails delegate
 
Cocoon gem example
Cocoon gem exampleCocoon gem example
Cocoon gem example
 
Bad Smells in Code
Bad Smells in CodeBad Smells in Code
Bad Smells in Code
 
Semantic Integration Patterns
Semantic Integration PatternsSemantic Integration Patterns
Semantic Integration Patterns
 
No more Three Tier - A path to a better code for Cloud and Azure
No more Three Tier - A path to a better code for Cloud and AzureNo more Three Tier - A path to a better code for Cloud and Azure
No more Three Tier - A path to a better code for Cloud and Azure
 
Graphql
GraphqlGraphql
Graphql
 
10 things you're doing wrong in Talend
10 things you're doing wrong in Talend10 things you're doing wrong in Talend
10 things you're doing wrong in Talend
 
10 things you're doing wrong in Talend
10 things you're doing wrong in Talend10 things you're doing wrong in Talend
10 things you're doing wrong in Talend
 
Rails traps
Rails trapsRails traps
Rails traps
 
creating portable social networks with microformats
creating portable social networks with microformatscreating portable social networks with microformats
creating portable social networks with microformats
 
How do I Even Web App
How do I Even Web AppHow do I Even Web App
How do I Even Web App
 
Uses & Abuses of Mocks & Stubs
Uses & Abuses of Mocks & StubsUses & Abuses of Mocks & Stubs
Uses & Abuses of Mocks & Stubs
 
Threading in C#
Threading in C#Threading in C#
Threading in C#
 

Mehr von mtoppa

WordCamp Nashville 2015: Agile Contracts for WordPress Consultants
WordCamp Nashville 2015: Agile Contracts for WordPress ConsultantsWordCamp Nashville 2015: Agile Contracts for WordPress Consultants
WordCamp Nashville 2015: Agile Contracts for WordPress Consultants
mtoppa
 
Dependency Injection for Wordpress
Dependency Injection for WordpressDependency Injection for Wordpress
Dependency Injection for Wordpress
mtoppa
 

Mehr von mtoppa (20)

RubyConf 2022 - From beginner to expert, and back again
RubyConf 2022 - From beginner to expert, and back againRubyConf 2022 - From beginner to expert, and back again
RubyConf 2022 - From beginner to expert, and back again
 
RailsConf 2022 - Upgrading Rails: The Dual Boot Way
RailsConf 2022 - Upgrading Rails: The Dual Boot WayRailsConf 2022 - Upgrading Rails: The Dual Boot Way
RailsConf 2022 - Upgrading Rails: The Dual Boot Way
 
Applying Omotenashi (Japanese customer service) to your work
Applying Omotenashi (Japanese customer service) to your workApplying Omotenashi (Japanese customer service) to your work
Applying Omotenashi (Japanese customer service) to your work
 
A11Y? I18N? L10N? UTF8? WTF? Understanding the connections between: accessib...
A11Y? I18N? L10N? UTF8? WTF? Understanding the connections between:  accessib...A11Y? I18N? L10N? UTF8? WTF? Understanding the connections between:  accessib...
A11Y? I18N? L10N? UTF8? WTF? Understanding the connections between: accessib...
 
The promise and peril of Agile and Lean practices
The promise and peril of Agile and Lean practicesThe promise and peril of Agile and Lean practices
The promise and peril of Agile and Lean practices
 
Why do planes crash? Lessons for junior and senior developers
Why do planes crash? Lessons for junior and senior developersWhy do planes crash? Lessons for junior and senior developers
Why do planes crash? Lessons for junior and senior developers
 
Boston Ruby Meetup: The promise and peril of Agile and Lean practices
Boston Ruby Meetup: The promise and peril of Agile and Lean practicesBoston Ruby Meetup: The promise and peril of Agile and Lean practices
Boston Ruby Meetup: The promise and peril of Agile and Lean practices
 
A real-life overview of Agile and Scrum
A real-life overview of Agile and ScrumA real-life overview of Agile and Scrum
A real-life overview of Agile and Scrum
 
WordCamp Nashville 2016: The promise and peril of Agile and Lean practices
WordCamp Nashville 2016: The promise and peril of Agile and Lean practicesWordCamp Nashville 2016: The promise and peril of Agile and Lean practices
WordCamp Nashville 2016: The promise and peril of Agile and Lean practices
 
WordCamp US: Clean Code
WordCamp US: Clean CodeWordCamp US: Clean Code
WordCamp US: Clean Code
 
Dependency Injection for PHP
Dependency Injection for PHPDependency Injection for PHP
Dependency Injection for PHP
 
WordCamp Boston 2015: Agile Contracts for WordPress Consultants
WordCamp Boston 2015: Agile Contracts for WordPress ConsultantsWordCamp Boston 2015: Agile Contracts for WordPress Consultants
WordCamp Boston 2015: Agile Contracts for WordPress Consultants
 
WordCamp Nashville 2015: Agile Contracts for WordPress Consultants
WordCamp Nashville 2015: Agile Contracts for WordPress ConsultantsWordCamp Nashville 2015: Agile Contracts for WordPress Consultants
WordCamp Nashville 2015: Agile Contracts for WordPress Consultants
 
Rails testing: factories or fixtures?
Rails testing: factories or fixtures?Rails testing: factories or fixtures?
Rails testing: factories or fixtures?
 
WordCamp Lancaster 2014: A11Y? I18N? L10N? UTF8? WTF?
WordCamp Lancaster 2014: A11Y? I18N? L10N? UTF8? WTF?WordCamp Lancaster 2014: A11Y? I18N? L10N? UTF8? WTF?
WordCamp Lancaster 2014: A11Y? I18N? L10N? UTF8? WTF?
 
WordCamp Nashville: Clean Code for WordPress
WordCamp Nashville: Clean Code for WordPressWordCamp Nashville: Clean Code for WordPress
WordCamp Nashville: Clean Code for WordPress
 
A real-life overview of Agile workflow practices
A real-life overview of Agile workflow practicesA real-life overview of Agile workflow practices
A real-life overview of Agile workflow practices
 
Why Agile? Why Now?
Why Agile? Why Now?Why Agile? Why Now?
Why Agile? Why Now?
 
Object Oriented Programming for WordPress Plugin Development
Object Oriented Programming for WordPress Plugin DevelopmentObject Oriented Programming for WordPress Plugin Development
Object Oriented Programming for WordPress Plugin Development
 
Dependency Injection for Wordpress
Dependency Injection for WordpressDependency Injection for Wordpress
Dependency Injection for Wordpress
 

Kürzlich hochgeladen

JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)
Max Lee
 

Kürzlich hochgeladen (20)

AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
 
How to install and activate eGrabber JobGrabber
How to install and activate eGrabber JobGrabberHow to install and activate eGrabber JobGrabber
How to install and activate eGrabber JobGrabber
 
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
 
How to pick right visual testing tool.pdf
How to pick right visual testing tool.pdfHow to pick right visual testing tool.pdf
How to pick right visual testing tool.pdf
 
10 Essential Software Testing Tools You Need to Know About.pdf
10 Essential Software Testing Tools You Need to Know About.pdf10 Essential Software Testing Tools You Need to Know About.pdf
10 Essential Software Testing Tools You Need to Know About.pdf
 
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
Entropy, Software Quality, and Innovation (presented at Princeton Plasma Phys...
 
IT Software Development Resume, Vaibhav jha 2024
IT Software Development Resume, Vaibhav jha 2024IT Software Development Resume, Vaibhav jha 2024
IT Software Development Resume, Vaibhav jha 2024
 
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdf
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdfImplementing KPIs and Right Metrics for Agile Delivery Teams.pdf
Implementing KPIs and Right Metrics for Agile Delivery Teams.pdf
 
AI Hackathon.pptx
AI                        Hackathon.pptxAI                        Hackathon.pptx
AI Hackathon.pptx
 
INGKA DIGITAL: Linked Metadata by Design
INGKA DIGITAL: Linked Metadata by DesignINGKA DIGITAL: Linked Metadata by Design
INGKA DIGITAL: Linked Metadata by Design
 
JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)JustNaik Solution Deck (stage bus sector)
JustNaik Solution Deck (stage bus sector)
 
Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024Top Mobile App Development Companies 2024
Top Mobile App Development Companies 2024
 
OpenChain @ LF Japan Executive Briefing - May 2024
OpenChain @ LF Japan Executive Briefing - May 2024OpenChain @ LF Japan Executive Briefing - May 2024
OpenChain @ LF Japan Executive Briefing - May 2024
 
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
COMPUTER AND ITS COMPONENTS PPT.by naitik sharma Class 9th A mittal internati...
 
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdfA Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
A Comprehensive Appium Guide for Hybrid App Automation Testing.pdf
 
What need to be mastered as AI-Powered Java Developers
What need to be mastered as AI-Powered Java DevelopersWhat need to be mastered as AI-Powered Java Developers
What need to be mastered as AI-Powered Java Developers
 
SQL Injection Introduction and Prevention
SQL Injection Introduction and PreventionSQL Injection Introduction and Prevention
SQL Injection Introduction and Prevention
 
KLARNA - Language Models and Knowledge Graphs: A Systems Approach
KLARNA -  Language Models and Knowledge Graphs: A Systems ApproachKLARNA -  Language Models and Knowledge Graphs: A Systems Approach
KLARNA - Language Models and Knowledge Graphs: A Systems Approach
 
APVP,apvp apvp High quality supplier safe spot transport, 98% purity
APVP,apvp apvp High quality supplier safe spot transport, 98% purityAPVP,apvp apvp High quality supplier safe spot transport, 98% purity
APVP,apvp apvp High quality supplier safe spot transport, 98% purity
 
Workforce Efficiency with Employee Time Tracking Software.pdf
Workforce Efficiency with Employee Time Tracking Software.pdfWorkforce Efficiency with Employee Time Tracking Software.pdf
Workforce Efficiency with Employee Time Tracking Software.pdf
 

Talking to strangers causes train wrecks

  • 1. July 14, 2015 Talking to strangers causes train wrecks Mike Toppa CTO, Poka Yoke Design @mtoppa http://pokayoke.design
  • 2. My house, yesterday I just moved to Boston from Philly. I took this picture yesterday. It looks pretty much the same today.
  • 3. Goal: unpack this quote “Mockist testers do talk more about avoiding 'train wrecks' - method chains of style getThis().getThat().getTheOther(). Avoiding method chains is also known as following the Law of Demeter. While method chains are a smell, the opposite problem of middle men objects bloated with forwarding methods is also a smell. (I've always felt I'd be more comfortable with the Law of Demeter if it were called the Suggestion of Demeter.)” Martin Fowler, 2004 Mocks aren’t Stubs
  • 4. The Law of Demeter, I ✤ Your method can call other methods in its class directly def full_name "#{self.first_name} #{self.last_name}" end ✤ You can play with yourself From c2.com "Law of Demeter" Back in the 1980s, a group of programmers working on a project called the Demeter system realized that certain qualities in their object-oriented code led to the code being easier to maintain and change. [from Avdi Grimm] Analogy by Peter VanRooijen
  • 5. The Law of Demeter, II ✤ Your method can call methods on its own fields directly (but not on the fields' fields) user.department.name ✤ You can play with your own toys (but you can’t take them apart) From c2.com "Law of Demeter"
  • 6. The Law of Demeter, III ✤ When your method takes parameters, your method can call methods on those parameters directly def user_info(user) "Name: #{user.full_name}" end ✤ You can play with toys that were given to you From c2.com "Law of Demeter"
  • 7. The Law of Demeter, IV ✤ When your method creates local objects, that method can call methods on the local objects # Sorry, I can’t give an example without # going on a tangent about dependency injection ✤ And you can play with toys you’ve made yourself From c2.com "Law of Demeter"
  • 8. Violation example in a Rails project In Ruby, and Rails, this kind of chaining is so easy to do, you might not realize the implications of what you’re doing. There are 3 concerns to emphasize: 1. This code is brittle. If any component of the method chain changes, this line of code, and any others like it, will break. Coupling is high, and information is not localized. 2. In the context of ActiveRecord, this approach has the potential to yield horrific, poorly performing underlying SQL queries. 3. If the code was in a method, it would be challenging to write a unit test for it
  • 9. Testing with train wrecks def user_info(user) "Name: #{user.name}. " "Boss: #{user.department.try(:head).try(:name)}" end From Demeter: It’s not just a good idea. It’s the law. This example is from Avdi Grimm
  • 10. describe '#user_info' do subject { user_info(user) } let(:user) { stub( 'user', name: "Bob", department: stub( name: "Accounting", head: stub( name: "Jack", position: stub(title: "Vice President") ) ), division: stub( name: "Microwave Oven Programming" ) ), position: stub(title: "Senior Bean Counter") ) } # examples... end From Demeter: It’s not just a good idea. It’s the law. Alternately, you could try to do this without mocks, and use factories or fixtures instead. But that just moves the brittleness into your test data setup.
  • 11. Reducing Rails train wrecks: Delegation From Demeter: It’s not just a good idea. It’s the law. class User delegate :name, to: :department, prefix: true, allow_nil: true end … def user_info(user) "Name: #{user.name}. Dept: #{user.department_name}" end While Demeter allows us to play with our friends, delegation can still help reduce brittleness. If there is a change in the relationship between users and department, we can create a department_name method that expresses the new logic, without having to change calling code. For example, if users can now be part of more than one department.
  • 12. Reducing Rails train wrecks: has many :through class User < ActiveRecord::Base has_many :departments has_many :divisions, through: :departments end Now we can treat divisions as an immediate collaborator. You can say this is cheating, but the point is, information about divisions is now localized. If something changes in the relationship to divisions, we can replace the “has many through” relationship with a “divisions” method.
  • 13. Reducing Rails train wrecks: nested has many :through class Candidate < ActiveRecord::Base has_many :campaigns has_many :races, through: :campaigns has_many :sought_offices, through: :races end
  • 14. Fowler’s conundrum: Demeter as suggestion, or law? ✤ With nested has many :through, now we can say: ✤ candidate.sought_offices ✤ With Rails, we don’t need “middle men objects bloated with forwarding methods” to access distant collaborators ✤ The question I’ll leave you to ponder is: ✤ Is this a good solution to Fowler’s conundrum?
  • 15. ポカヨケ http://pokayoke.design @mtoppa My wife has a new job in Boston, so we’re moving, and I’ll be starting a new job too. I’ll be co-founder and CTO of Poka Yoke Design, and I’m looking forward to working more with WordPress again. My partner is based in Memphis, so I’m very pleased to be strengthening my ties to the WordPress community here in Tennessee.