Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Why Best Practices?

jeg2
August 04, 2013

Why Best Practices?

This is JEG2's segment of the Ruby Rogues panel form LSRC 2013.

jeg2

August 04, 2013
Tweet

More Decks by jeg2

Other Decks in Technology

Transcript

  1. I Only Have Time to
    Make One Point
    The others made me include this slide!

    View full-size slide

  2. Why Best Practices?

    View full-size slide

  3. How Should We
    Use Struct?
    class Specialized < Struct.new(:whatever)
    # ... define custom methods here...
    end
    Specialized = Struct.new(:whatever) do
    # ... define custom methods here...
    end

    View full-size slide

  4. I Prefer the Block Form
    But that’s not the point!

    View full-size slide

  5. Code Similarity
    and Malleability
    Specialized = Struct.new(:whatever) do
    # ... define custom methods here...
    end
    Trivial = Struct.new(:whatever)

    View full-size slide

  6. An Extra Class
    • The anonymous class
    doesn’t tell us much
    • Code reloading may
    cause “TypeError:
    superclass mismatch…”
    [Specialized,
    #,
    Struct,
    Enumerable,
    Object,
    PP::ObjectMixin,
    Kernel,
    BasicObject]
    class Specialized < Struct.new(:whatever)
    # ... define custom methods here...
    end

    View full-size slide

  7. The “super” Problem
    class Specialized < Struct.new(:whatever)
    def whatever
    super || :default
    end
    include SomeMixin
    end
    Specialized = Struct.new(:whatever) do
    def whatever
    self[:whatever] || :default
    end
    prepend SomeMixin
    end

    View full-size slide

  8. The Point
    • It’s not about the one right way to code
    • It’s about what we learn in the discussion
    • This trivial example alone includes:
    • Code malleability
    • The ancestor class chain
    • The value of prepend

    View full-size slide