Micro Libraries FTW

Micro Libraries FTW

E864e5088627498df8f9b911a9bc3219?s=128

Piotr Solnica

March 16, 2014
Tweet

Transcript

  1. Micro Libraries FTW Piotr Solnica wroclove.rb 2014

  2. Who am I? Ruby and Rails developer OSS ruby hacker

    github.com/solnic @_solnic_
  3. Ruby Object Mapper github.com/rom-rb

  4. Agenda Big libraries vs micro libraries Micro libraries and frameworks

    Example micro libraries microrb.com
  5. Big Libraries vs Micro Libraries

  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
  7. Rails Influence

  8. Ruby developers <3 Rails

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

  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
  11. Rails is great but let’s move on

  12. Let’s embrace Unix philosophy

  13. “Many UNIX programs do quite trivial things in isolation, but,

    combined with other programs, become general and useful tools” The UNIX Programming Environment
  14. Micro Libraries FTW

  15. Why building a micro library?

  16. Complex functionality that could be reused Constantly repeated design pattern

    A specific problem known up front
  17. Building frameworks based on micro libraries

  18. bottom-up vs top-down

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

  20. Does not happen very often

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

    you’re doing
  22. Happens almost all the time

  23. Unfortunately top-down approach can be misleading

  24. DSL + core domain logic == disaster

  25. Frameworks should provide high-level interfaces that are built on top

    of lower level micro libraries
  26. Challenges

  27. Dealing with many dependencies is hard

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

    of gems being installed
  29. Dependency Hell

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

    HELL
  31. Version conflicts between frameworks are the biggest challenge

  32. Semantic Versioning

  33. Be responsible!

  34. Changelogs

  35. Tools can help!

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

    dependencies of gems
  37. Example micro libraries

  38. github.com/dkubb/equalizer

  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! => #<GeoLocation lat=123 lng=321>
  40. github.com/mbj/anima

  41. equalizer + anima

  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
  43. virtus + equalizer

  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
  45. How about a framework?

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

  47. morpher github.com/mbj/morpher

  48. rom + morpher

  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
  50. virtus 2.0 = anima + morpher

  51. microrb.com

  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
  53. Thank You! <3<3<3