confident ruby: be a coding hemingway

23
Confident Ruby Be A Coding Hemingway

Upload: matthew-salerno

Post on 26-Jun-2015

771 views

Category:

Technology


4 download

DESCRIPTION

Book Report on Avdi Grimm's excellent 'Confident Ruby'

TRANSCRIPT

Page 1: Confident Ruby: Be A Coding Hemingway

Confident RubyBe A Coding Hemingway

Page 2: Confident Ruby: Be A Coding Hemingway

Confident Ruby

Avdi Grimmhttp://www.rubytapas.com/

Page 3: Confident Ruby: Be A Coding Hemingway

● Application Design

● Object Modeling

● Patterns for writing confident Ruby code at the method level

Doesn’t: Does:

Page 4: Confident Ruby: Be A Coding Hemingway

“CODE AS NARRATIVE”

Page 5: Confident Ruby: Be A Coding Hemingway

Responsibilities

Collecting Input

Performing Work

Delivering Output

Handling Errors

Page 6: Confident Ruby: Be A Coding Hemingway

Conversion Methods

Special Case Object

Receive Policies

Page 7: Confident Ruby: Be A Coding Hemingway

Conversion Methods

Conversion Methods for Coercing Inputs

Page 8: Confident Ruby: Be A Coding Hemingway

Checking for ‘nil’ (type checking)

Nested control structures(‘if/else’ blocks)

Type checking

Page 9: Confident Ruby: Be A Coding Hemingway

● #to_a, #to_i, #to_s

● objects don’t resemble the target type

● Implemented by Ruby types but not called

Explicit Conversion

Page 10: Confident Ruby: Be A Coding Hemingway

● #to_ary, #to_int, #to_str

● objects closely resemble the target type

● Called by Ruby but not implemented

Implicit Conversion

Page 11: Confident Ruby: Be A Coding Hemingway

● Array(), Integer(), String()

● Try implicit and explicit conversions

● Bag o’ tricks

Kernel Level Conversion Methods

Page 12: Confident Ruby: Be A Coding Hemingway

Flexible

Coherent

Confident ; )

Page 13: Confident Ruby: Be A Coding Hemingway

Conversion Methods

Special Case Object

Page 14: Confident Ruby: Be A Coding Hemingway

Conversion Methods

=>

Page 15: Confident Ruby: Be A Coding Hemingway

Special Case Object● isolate the differences to a single object

● Leverage polymorphism by conforming to a protocol

Page 16: Confident Ruby: Be A Coding Hemingway

=>

ConfidentExpressiveFlexible

Page 17: Confident Ruby: Be A Coding Hemingway

“The foundation of an object oriented system is the message”

-Sandi Metz, Practical Object-Oriented Design in Ruby

Page 18: Confident Ruby: Be A Coding Hemingway

Conversion Methods

Receive Policies Instead of Data

Page 19: Confident Ruby: Be A Coding Hemingway

“Put the decision for how to handle test cases in the hands of the code best qualified to determine the appropriate response.”

Delegate Responsibility to Client Code

Page 20: Confident Ruby: Be A Coding Hemingway

Passing data/flags

● Intent of the method is obscured

● Calling code is not self-documenting

● Little flexibility. What if we want more than 2 ways to handle errors?

Page 21: Confident Ruby: Be A Coding Hemingway

● Pass policies as block/proc/lambda objects

● Stop handling edge cases from obfuscating the method’s intent

● Flexibility - let’s the client code handle decision making

Passing Policies

Page 22: Confident Ruby: Be A Coding Hemingway

Example: Ruby’s Enumerable#detect

Page 23: Confident Ruby: Be A Coding Hemingway

Write code that tells a storyConfident Ruby - Avdi Grimm