67 points · by ismaelct · 2y ago · 66 comments
The middleware pattern had a lot of the same concepts present in this post: we called context "state" and you could use special exceptions to halt or pause a middleware chain in the middle.
This was a really great way for over a decade (to this day!) to represent a long-running series of steps that individually may branch, fail, accumulate values, etc. I don't recall the exact count, but an old `vagrant up` used to execute something like 40 "actions" (as we called each step).
I'm not trying to disregard this blog post in any way, I'm only pointing out this pattern is indeed very useful and has been proven in production environments for a very long time!
Indeed!https://github.com/hashicorp/vagrant/blob/main/lib/vagrant/a...
Yes, it's not a new pattern by any means, and there's many ways to "halt" the pipeline as you say. For example ActiveRecord stops callback chains if any callback throws ":halt".
Other examples are Redis.rb's pipelining API https://github.com/redis/redis-rb?tab=readme-ov-file#pipelin...
Or more generally any builder-style pattern that composes a set of operations for later execution, including again ActiveRecord's query chaining.
In my article I tried to show a specific implementation using the Railway pattern (where the result must only respond to "#continue?() => Boolean")
The closest ruby way is:
result = DoStuff
return something if result.nil? # error, early exit
result = OtherStuff(result)
return something if result.nil? # error, early exit
No additional complexity. Otherwise usually it ends up just rewriting "programming".With pattern matching there are also some alternatives now.
I love curried functions by default, but if it's not the default, it never works out even in functional languages like Elixir. You need to have every developer (and new hired developer) pretty much on board with that and exclude every library that doesn't do this, or wrap it (huge burden).
Given those, I'd rather stick to what I wrote. Not my favorite, I loved my brief experience with Elm
def step(callable = nil, &block)
you're missing the nil default in the blog post so it ArgumentError's when passed only a blockI’m sure this is great and solves real problems for OP and friends. I just ask that you think twice about it before forcing it on coworkers. Please, this poor soul can’t take it anymore.
Not that code is this serious at all. Idk.
If they drive slower then yeah they're morons and I need to pass them as soon as humanly possible.
In both cases they become non-factors and I can drive in peace. The fast disappear into the horizon and the slow get left behind. This plays out until I am alone in the road and no longer need to consider the actions of other humans behind the wheel. That is when peace is achieved. Sadly it does not last long.
I mean they're probably tailgating and aggressively changing lanes and don't have any awareness of the dangers of differential velocity between lanes of traffic. I don't think you really need to learn those 'tricks'.
You can still freely let them go and they can give any bored cops ahead of you something to do though.
There is a place and time for everything. You can drive as fast as you can in a Formula 1 race, but when you're around pedestrians you gotta slow down.
Similarly, complex abstractions can often be fast to write but difficult to maintain. Similar to a lack of them.
But sure, it all has a time and place. Except the attitude of dismissing or never discussing this topic. That's the only thing that is "wrong" here.
I haven’t gotten a chance to mess around with this specifically, how much of its internals are mixed into execution?
I want traces to go to actual code as much as possible.
I also want my IDE to work.
One of the most annoying things in my life right now is aasm. It’s a good idea, but I hate having to know this new dsl, I hate the dynamic methods it defines, I hate having to constantly explain to the confused folks that this random bang method is actually triggering this `event :event_name` over here. I would just so much rather have none of the DSL and meta-programming stuff, even if it’s twice as much “boilerplate”, if that means people who understand normal code can actually trace what the program is doing using the tools they already know how to use.
Certainly don’t mind abstraction like a standardized Result class, it’s more the chaining and custom halt with error patterns that I can tell are going to complicate debugging.
Personally I've dealt with enough Trailblazer code now that for business logic I think I'd much rather work with a simple ruby class with a procedural interface.
I like the use of a Result object but not at the expense of exception handling or bloated stacktraces full of library code.
Being lightweight, easy to debug and upgrade is underrated IMHO.
Having said all that, I still think this is a really interesting article and great food for thought depending the usecase.
1. https://fsharpforfunandprofit.com/posts/against-railway-orie...
A pipeline is essentially this
steps = [ ->(r) { r }, ->(r) { r }, ->(r) { r }, ]
Wrap initial data in a common Value object
initial = Result.new(some_data)
Run the Result through the steps, in order
result = steps.reduce(initial) { |r, step| step.call(r) }
That's the pattern, really. A reduce operation.
Re. stack trace, it can add noise because you're iterating over steps instead of calling them procedurally one by one, and you may want to decorate steps (put steps inside steps) for encapsulation, caching, etc. but again no different than Rack.
Developers don't like to think about abstractions and naming is a known hard problem. People pick this up and now everything is easy. Every name becomes: DoThis, DoThat, DoThisOtherThing and only answers to `call`. No need to think!
Now you have deeply nested procedures that oversimplify their interfaces to match the library and suddenly you no longer have meaningful error types, you have strings. You no longer have abstractions, you only have procedural code with some objects in the middle. The list goes on.
Like most things in development, used in the right places and with careful consideration, this can be very valuable. But this is a very insidious change that turns your code into a functional style with several drawbacks that are rarely considered.
I would further add that we should extend that thoughtfulness to the opinionated frameworks many of us rely on. They usually come with hundreds of complicated patterns baked in, and we "oversimplify our interfaces" to match the framework as a matter of course. All I'm saying is that committing to any mental model provided by a design pattern or framework has similar drawbacks. In this case, the mental model is "a big operation is a list of smaller operations in a chain". Use with caution.
> No need to think!
Not sure I agree with fully. If anything, I've found that I need to think harder about what constitutes a step in a workflow, what are the names of each stage, what concept they encapsulate. I can't just chuck everything into a god object or a deeply nested hierarchy tree somewhere.
But again I agree. Using this pattern I've definitely over-complicated or gone down the wrong path at times. I would say though that I found it easier to roll back and change direction when compared deeply nested object graphs, for example.
This 'framework' doesn't require that though:
def double_number(r) = r.continue(r.value * 2)
def add_one(r) = r.continue(r.value + 1)
def square_number(r) = r.continue(r.value ** 2)
pipeline = Pipeline.new do |pl|
pl.step method(:double_number)
pl.step method(:add_one)
pl.step method(:square_number)
end
Just methods, with no need to implement call() because that is what a method/proc/lambda in ruby implements. In other languages like C# you could have an API that can take a Func<Result, Result> so that you can just pass delgates/lambdas/methods to it. And adding a bit more DSL could make the construction of the pipeline even less verbose.You can use this without going full-Java and creating a dozen (or a hundred) 5-line classes in different files.
I actually consider the urge to introduce overengineered patterns like this a sure sign of a "mid-level developer". Senior knows better.
Sometimes, the cost of keeping really talented senior engineers who have solved a very difficult part of your tech stack is to give them the freedom to over engineer solutions that are probably a bit overcomplicated (maybe like OP). The alternative is they get bored and leave and take a wealth of technical knowledge with them
Programming is about managing complexity. Making stack trace twice as long, hiding control flow in data, most cases of metaprogramming do not reduce complexity, they increase it. Unless your app is all about usage of some specific pattern (a lot of procedural scenarios with sideffects, shortcircuiting on error and no return value) then "railway framework" is not worth it in ruby. Use exceptions (really!). Use throw, maybe.
A lot of what people used to call senior has become "staff" in larger companies.
definitely works better than "fat models" that do everything and ruby on rails callback hell
That's not what this boils down to.
These are procedures. OOP is generally about objects sending messages to each other. So this solution is all about executing procedural calls, which has nothing to do with inheritance/composition.
You can very easily have OO code with composition without having to rely on everything having one single interface "call/run/execute".
Pipeline steps are command objects (a pretty standard OO pattern), so they have a single entry point / public method. But they can still fully leverage any other OO pattern in their implementation. The more complex ones I use may instantiate other objects, pass messages between them, etc.
But the single-method #call API is what makes composition easy. See Rack, or any number of middleware-style designs, for other common uses of this in Ruby.
I feel like that's the kind of over-simplification that then makes people pick a style like this without fully considering the implications.
Every single place that I've seen use these "command pattern" systems devolved into a complete utter mess of procedures. People forget the basics of OO and write everything into "steps" because that's now the hammer and everything is a nail.
If they stuck to the outer layers as the orchestrator for everything, that'd be great. But that never happens in my experience.
I think you're right about how that line comes across, I'll try and improve it.