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

Compose Software Like Nature Would - RubyConf 2024

Ahmed Omran
November 20, 2024
19

Compose Software Like Nature Would - RubyConf 2024

Ahmed Omran

November 20, 2024
Tweet

Transcript

  1. Ahmed Omran COMPOSE SOFTWARE LIKE NATURE WOULD Living organisms have

    the incredible ability to adapt to their environment. Let’s explore together how we can create adaptable, maintainable, and resilient software with some inspiration from nature.
  2. –Alan Kay “I thought of objects being like biological cells

    and/or individual computers on a network, only able to communicate with messages…”
  3. Components: e.g. modules & classes Messages: e.g. methods & events

    Components with boundaries Compose with messages
  4. • easier to reason about ( fi ts in your

    head) • easier to change (can be re-wri tt en) • easier to test (con fi dence when changing)
  5. No Boundaries class Certi fi cate def initialize(...) # TODO:

    we should stop sending ten arguments to a method end # Get data from the database # Access the fi le system for a certi fi cate template # Generate a new fi le # Save the fi le to S3 # TODO: fi gure out why this is so slow end
  6. Boundaries class Certi fi cate # Smaller entry point def

    initialize(enrollment) @enrollment = enrollment end # Limited interface def title end def subtitle end def date end private # You don't need to know what else is here! end
  7. Small components class Template a tt r_reader :certi fi cate

    def initialize(certi fi cate) @certi fi cate = certi fi cate end def render # returns HTML end private # It's a secret end class S3File a tt r_reader :certi fi cate, :html def initialize(certi fi cate, html) @certi fi cate = certi fi cate @html = html end def save # returns the url of the fi le end private # It's a secret end
  8. /project |-- certi fi cate.rb |-- template.rb |-- s3_ fi

    le.rb … 1000 other fi les What does system do? title subtitle date render save
  9. project └── compliance └── certi fi cate ├── generator.rb ├──

    template.rb ├── base.rb └── s3_ fi le.rb domain speci fi c namespace coordinator
  10. Compose Components module Certi fi cate class Generator # …

    # Compose our components def generate certi fi cate = Certi fi cate::Base.new(enrolment) html = Certi fi cate::Template.new(certi fi cate).render Certi fi cate::S3File.new(certi fi cate, html).save end # or inject the components def generate(template_strategy, saving_strategy) certi fi cate = Certi fi cate::Base.new(enrolment) html = template_strategy.new(certi fi cate).render saving_strategy.new(certi fi cate, html).save end end end
  11. • persisting state • writing to disk • printing on

    a screen • making HTTP requests • raising errors • mutable data side-e ff ects / outside world
  12. title subtitle date render save Deterministic* “Nerve Net” QR code

    delivery method save (e.g. s3, device) Data (sql) Files S3 Generator Email HTTP
  13. • edges: controllers, background jobs, mailers, rake tasks, etc. •

    be mindful of side-e ff ects • functional/deterministic within reason
  14. Run and Tumble to Be tt er Design small components

    compose with messages push side-e ff ects to boundaries strong boundaries name & structure