Presentation on Messaging with code samples using East Orientation (& Injection)
- Eastward is sending a message (telling)
- Westward is receiving a message (usually after asking)
-
Dr. Alan Kay - Rules to design good messaging; AKA good OOD. Many people cite this source as messaging inspiration:
- The Meaning of "Object-Oriented Programming" http://userpage.fu-berlin.de/~ram/pub/pub_jf47ht81Ht/doc_kay_oop_en
-
James Ladd - Created Eastward Oriented Rules (original blog is unavailable)
- Blog: "Why we fail at OO": http://jamesladdcode.com/?p=12
-
Jim Gay EASTWARD APPLIED to RUBY: (RubyConf 2014) Eastward Ho!
Enforced Tell - Don't Ask
- Always Return 'self' - especially public methods - (factories & initializers exempt)
- Objects MAY ONLY QUERY itself - avoid public getters!
- Objects MAY ONLY CHANGE their own state - avoid public setters!
- Break the Rules sparingly - mostly for output -- then pass a simple object (ideally immutable) - think Structs & View-Models
Rules 2 & 3 are basically unnecessary if rule 1 is followed closely!
EASY & HELPFUL: with a pipeline architecture:
input -> processing -> output
Examples:
- CLI scripts
- Background Jobs
- Data conversions outputs
CONTROVERSIAL to use with a "controller" architecture - that expects to "control" - the opposite of East Oriented code.
- Jim Weirich - Decoupling from Rails & his sample code (summarized by DHH)
- DHH - Calls this Test Driven Damage
- Uncle Bob Support for Decoupling & Architecture the Lost Years
When Strict East isn't practical
Helpful for daily coding - when East Oriented not practical
- Tell, don't ask - Could I return self?, What else canI make private?
- Exceptions common - pass a data for output & formatting - using view-models, value-objects, struct or minimally a hash
I like to explore the practice of getting as close as possible to East Oriented (along with Sandi Metz's Code Complexity Rules & Behavior Injection)
I don't have access to the Data Exchange I wrote for my previous employer - but Jim Gay's address code can use a similar pipeline flow
- 01_east_west_address.rb - is east-west, very simple and clear -- great when very simple and flexibility isn't needed
- 02_east_address.rb - is east only is great when additional for flexible outputs and keeping processing clear (only objects can change and query themselves)
Given Time and Interest
- 03_east_injection_address.rb - injection and decorators allow lots of flexibility and complexity while preserving the benefits of East Orientated Message passing with one place data is mutated and single purpose for each class.
- 00_jim_gay_refactor_process.md - Jim Gay's refactoring process from East/West to West