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

Micro Libraries FTW

Micro Libraries FTW

Piotr Solnica

March 16, 2014
Tweet

More Decks by Piotr Solnica

Other Decks in Programming

Transcript

  1. Micro Libraries FTW
    Piotr Solnica
    wroclove.rb 2014

    View Slide

  2. Who am I?
    Ruby and Rails developer
    OSS ruby hacker
    github.com/solnic
    @_solnic_

    View Slide

  3. Ruby Object Mapper
    github.com/rom-rb

    View Slide

  4. Agenda
    Big libraries vs micro libraries
    Micro libraries and frameworks
    Example micro libraries
    microrb.com

    View Slide

  5. Big Libraries
    vs
    Micro Libraries

    View Slide

  6. BIG LIBRARIES MICRO LIBRARIES
    Solving many problems Solving just one problem
    Big LOC Small LOC
    Wide interfaces Narrow interfaces
    High churn even after 1.0 Low churn especially after 1.0

    View Slide

  7. Rails Influence

    View Slide

  8. Ruby developers
    <3
    Rails

    View Slide

  9. Rails has a huge impact on
    how we write Ruby

    View Slide

  10. Rails is in favor of huge libraries
    ActiveSupport and a wide adoption of monkey-
    patching
    ActiveRecord-like style - one object with a
    plethora of responsibilities and mixing seemingly
    related concerns

    View Slide

  11. Rails is great but
    let’s move on

    View Slide

  12. Let’s embrace
    Unix philosophy

    View Slide

  13. “Many UNIX programs do quite trivial things
    in isolation, but, combined with other
    programs, become general and useful tools”
    The UNIX Programming Environment

    View Slide

  14. Micro Libraries FTW

    View Slide

  15. Why building a micro library?

    View Slide

  16. Complex functionality that could be reused
    Constantly repeated design pattern
    A specific problem known up front

    View Slide

  17. Building frameworks based on
    micro libraries

    View Slide

  18. bottom-up
    vs
    top-down

    View Slide

  19. bottom-up
    Really great when you know
    exactly what you’re doing

    View Slide

  20. Does not happen
    very often

    View Slide

  21. top-down
    Really great even when you
    don’t know exactly what
    you’re doing

    View Slide

  22. Happens almost
    all the time

    View Slide

  23. Unfortunately top-down approach
    can be misleading

    View Slide

  24. DSL + core domain logic
    ==
    disaster

    View Slide

  25. Frameworks should provide high-level
    interfaces that are built on top of
    lower level micro libraries

    View Slide

  26. Challenges

    View Slide

  27. Dealing with
    many dependencies
    is hard

    View Slide

  28. rubygems + bundler = slow
    “meta-gems”
    PANIC when seeing lots of gems being installed

    View Slide

  29. Dependency Hell

    View Slide

  30. 2 gems depending on different versions of
    another gem
    ==
    HELL

    View Slide

  31. Version conflicts between
    frameworks are the biggest
    challenge

    View Slide

  32. Semantic Versioning

    View Slide

  33. Be responsible!

    View Slide

  34. Changelogs

    View Slide

  35. Tools can help!

    View Slide

  36. ruby-toolbox.com
    lots of information about gems
    gemnasium.com
    detailed information about dependencies of gems

    View Slide

  37. Example micro libraries

    View Slide

  38. github.com/dkubb/equalizer

    View Slide

  39. require "equalizer"!
    !
    class GeoLocation!
    include Equalizer.new(:lat, :lng)!
    !
    attr_reader :lat, :lng!
    !
    def initialize(attributes)!
    @lat, @lng = attributes.values_at(:lat, :lng)!
    end!
    end!
    !
    loc1 = GeoLocation.new(:lat => 123, :lng => 321)!
    loc2 = GeoLocation.new(:lat => 123, :lng => 321)!
    !
    loc1 == loc2 # true!
    !
    loc1.inspect!
    => #

    View Slide

  40. github.com/mbj/anima

    View Slide

  41. equalizer + anima

    View Slide

  42. require "anima"!
    !
    class GeoLocation!
    include Anima.new(:lat, :lng)!
    end!
    !
    loc1 = GeoLocation.new(:lat => 123, :lng => 321)!
    loc2 = GeoLocation.new(:lat => 123, :lng => 321)!
    !
    loc1 == loc2!
    # => true

    View Slide

  43. virtus + equalizer

    View Slide

  44. require "virtus"!
    !
    class GeoLocation!
    include Virtus.value_object!
    !
    values do!
    attribute :lat, Float!
    attribute :lng, Float!
    end!
    end!
    !
    loc1 = GeoLocation.new(:lat => "123", :lng => "321")!
    loc2 = GeoLocation.new(:lat => "123", :lng => "321")!
    !
    loc1 == loc2!
    # => true

    View Slide

  45. How about a framework?

    View Slide

  46. Ruby Object Mapper
    github.com/rom-rb/rom

    View Slide

  47. morpher
    github.com/mbj/morpher

    View Slide

  48. rom + morpher

    View Slide

  49. module ROM!
    class Mapper!
    !
    # Abstract loader class!
    #!
    # @private!
    class Loader!
    !
    # some stuff is here!
    !
    # @api public!
    def call(tuple)!
    transformer.call(tuple)!
    end!
    !
    end # Loader!
    !
    end # Mapper!
    end # ROM
    This is morpher’s transformer
    and it takes care of everything

    View Slide

  50. virtus 2.0 = anima + morpher

    View Slide

  51. microrb.com

    View Slide

  52. Gather libraries that meet specific criteria
    Make it easy to find the right tool for the job
    Help in connecting maintainers and contributors
    Promote style of development that adheres to the
    unix philosophy
    Promote frameworks that are built on top of micro
    libraries

    View Slide

  53. Thank You!
    <3<3<3

    View Slide