You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, Continue is an alias for Success(:continued, value), providing a kind of better readability when chaining steps together. One "problem" is that Success can still be used instead of Continue to advance the operation from one step to another. Thus making the addon contract "weak".
See the example below:
moduleDivideextendself,BCDD::Result.mixin(with: :Continue)defcall(arg1,arg2)validate_numbers(arg1,arg2).and_then(:validate_non_zero).and_then(:divide)endprivatedefvalidate_numbers(arg1,arg2)arg1.is_a?(::Numeric)orreturnFailure(:invalid_arg,'arg1 must be numeric')arg2.is_a?(::Numeric)orreturnFailure(:invalid_arg,'arg2 must be numeric')Success(:ok,[arg1,arg2])# It's not clear that the other steps will still be processedenddefvalidate_non_zero(numbers)returnContinue(numbers)unlessnumbers.last.zero?# same hereFailure(:division_by_zero,'arg2 must not be zero')enddefdivide((number1,number2))Success(:division_completed,number1 / number2)endend
This makes it harder to really differentiate Continue from Success.
So, a better approach should enforce that Success also halts the chain when used on any given step (just like Failure does), thus making Continue required on steps that still need further processing. See the example below:
moduleDivideextendself,BCDD::Result.mixin(with: :Continue)defcall(arg1,arg2)validate_numbers(arg1,arg2).and_then(:validate_dividing_by_one).and_then(:validate_non_zero).and_then(:divide)endprivatedefvalidate_numbers(arg1,arg2)arg1.is_a?(::Numeric)orreturnFailure(:invalid_arg,'arg1 must be numeric')# halt the processing and return a failed resultarg2.is_a?(::Numeric)orreturnFailure(:invalid_arg,'arg2 must be numeric')# halt the processing and return a failed resultContinue([arg1,arg2])# continue to the next stependdefvalidate_dividing_by_one((number1,number2))returnSuccess(:division_completed,number1)ifnumber2 == 1# halt the processing and return a successful resultContinue([number1,number2])# continue to the next stependdefvalidate_non_zero(numbers)returnContinue(numbers)unlessnumbers.last.zero?# continue to the next stepFailure(:division_by_zero,'arg2 must not be zero')# halt the processing and return a failed resultenddefdivide((number1,number2))Success(:division_completed,number1 / number2)# halt the processing and return a failed resultendend
When Continue addon is DISABLED:
Failure will halt the step chain;
Success will continue the step chain.
When Continue addon is ENABLED:
Failure will halt the step chain;
Success will halt the step chain;
Continue CANNOT be used to produce a "final" result (this should raise an error).
Enforcing those rules when using the ' Continue' addon would improve readability and "contract strictness" since it would be clear that any Failure and Success on a step would generate a final result, and any other steps are ignored.
The text was updated successfully, but these errors were encountered:
mrbongiolo
changed the title
Success should halt the chain when using Continue addonSuccess should halt the step chain when using Continue addon
Oct 26, 2023
Currently,
Continue
is an alias forSuccess(:continued, value)
, providing a kind of better readability when chaining steps together. One "problem" is thatSuccess
can still be used instead ofContinue
to advance the operation from one step to another. Thus making the addon contract "weak".See the example below:
This makes it harder to really differentiate
Continue
fromSuccess
.So, a better approach should enforce that
Success
also halts the chain when used on any given step (just likeFailure
does), thus makingContinue
required on steps that still need further processing. See the example below:When
Continue
addon is DISABLED:Failure
will halt the step chain;Success
will continue the step chain.When
Continue
addon is ENABLED:Failure
will halt the step chain;Success
will halt the step chain;Continue
CANNOT be used to produce a "final" result (this should raise an error).Enforcing those rules when using the ' Continue' addon would improve readability and "contract strictness" since it would be clear that any
Failure
andSuccess
on a step would generate a final result, and any other steps are ignored.The text was updated successfully, but these errors were encountered: