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 Slide

  2. .

    View Slide

  3. Thanks!

    View Slide

  4. Why Best Practices?

    View Slide

  5. 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 Slide

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

    View Slide

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

    View Slide

  8. 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 Slide

  9. 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 Slide

  10. 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 Slide