2 ms·
TL,DR: Complex logic depends on data objects declared in your module. Instead of Logic => Dependency, refactor as Logic => Data + Data => Dependency. The code i
by pacala 12y ago
TL,DR: Complex logic depends on data objects declared in your module. Instead of Logic => Dependency, refactor as Logic => Data + Data => Dependency. The code is organized as:
// What kind of Data Logic processes.
trait Data { ... }
// How Data reads from a concrete Dependency
class DataFromDependency extends Data { ... }
// Logic
class Logic(data: Data) { ... }
Whys:
* Trivial testing of the Logic, by instantiating Data objects with fake data. If you ever had to instantiate a DB and populate it with 132 right objects just to test that date conversions work correctly, you know what this means.
* The Data adaptors reduce the semantic surface of the Dependency to what Logic needs. This makes reading and reasoning about Logic easier, especially if Dependency is a "fully featured" library with tens of methods Logic couldn't care less about.
- nitrogen 12y agoIn Ruby this sort of thing comes naturally due to its much more flexible metaprogramming features than Java-like languages. Testing frameworks like RSpec and the various Gems that go with it for building mock objects make this very easy and common, without having to restructure the original code or have a bunch of nonobvious names for a bunch of patterns. I have nothing against Java and still take work in Java, but I think every Java developer would learn a lot from Ruby-like languages.