Conversation
7f4f317 to
5b11342
Compare
5b11342 to
be78340
Compare
be78340 to
8697d91
Compare
8697d91 to
dcce196
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
BCDD::Result#and_then!In the Ruby ecosystem, several gems facilitate operation composition using classes and modules. Two notable examples are the
interactorgem and theu-casegem.interactorgem exampleu-casegem exampleTo facilitate migration for users accustomed to the above approaches,
bcdd-resultincludes theBCDD::Result#and_then!/BCDD::Result::Context#and_then!methods, which will invoke the methodcallof the given operation and expect it to return aBCDD::Result/BCDD::Result::Contextobject.Dependency Injection
Like
#and_then,#and_then!also supports an additional argument for dependency injection.In BCDD::Result
In BCDD::Result::Context
Configuration
Explanation:
enable!(:and_then!): Activates theand_then!feature.default_method_name_to_call: Sets a default method other than:callforand_then!.Analysis: Why is
and_then!an Anti-pattern?The
and_then!approach, despite its brevity, introduces several issues:Lack of Clarity: The input/output relationship between the steps is not apparent.
Steps Coupling: Each operation becomes interdependent (high coupling), complicating implementation and compromising the reusability of these operations.
We recommend cautious use of
#and_then!. Due to these issues, it is turned off by default and considered an antipattern.It should be a temporary solution, primarily for assisting in migration from another to gem to
bcdd-result.#and_thenversus#and_then!The main difference between the
#and_thenand#and_then!is that the latter does not check the result source. However, as a drawback, the result source will change.Attention: to ensure the correct behavior, do not mix
#and_thenand#and_then!in the same result chain.Analysis: Why is
#and_thenthe antidote/standard?The
BCDD::Result#and_then/BCDD::Result::Context#and_thenmethods diverge from the above approach by requiring explicit invocation and mapping of the outcomes at each process step. This approach has the following advantages:Clarity: The input/output relationship between the steps is apparent and highly understandable.
Steps uncoupling: Each operation becomes independent (low coupling). You can even map a failure result to a success (and vice versa).
See this example to understand what your code should look like: