Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Success should halt the step chain when using Continue addon #14

Closed
mrbongiolo opened this issue Oct 26, 2023 · 0 comments
Closed

Success should halt the step chain when using Continue addon #14

mrbongiolo opened this issue Oct 26, 2023 · 0 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@mrbongiolo
Copy link
Contributor

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:

module Divide
  extend self, BCDD::Result.mixin(with: :Continue)

  def call(arg1, arg2)
    validate_numbers(arg1, arg2)
      .and_then(:validate_non_zero)
      .and_then(:divide)
  end

  private

  def validate_numbers(arg1, arg2)
    arg1.is_a?(::Numeric) or return Failure(:invalid_arg, 'arg1 must be numeric')
    arg2.is_a?(::Numeric) or return Failure(:invalid_arg, 'arg2 must be numeric')

    Success(:ok, [arg1, arg2]) # It's not clear that the other steps will still be processed
  end

  def validate_non_zero(numbers)
    return Continue(numbers) unless numbers.last.zero? # same here

    Failure(:division_by_zero, 'arg2 must not be zero')
  end

  def divide((number1, number2))
    Success(:division_completed, number1 / number2)
  end
end

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:

module Divide
  extend self, BCDD::Result.mixin(with: :Continue)

  def call(arg1, arg2)
    validate_numbers(arg1, arg2)
      .and_then(:validate_dividing_by_one)
      .and_then(:validate_non_zero)
      .and_then(:divide)
  end

  private

  def validate_numbers(arg1, arg2)
    arg1.is_a?(::Numeric) or return Failure(:invalid_arg, 'arg1 must be numeric') # halt the processing and return a failed result
    arg2.is_a?(::Numeric) or return Failure(:invalid_arg, 'arg2 must be numeric') # halt the processing and return a failed result

    Continue([arg1, arg2]) # continue to the next step
  end

  def validate_dividing_by_one((number1, number2))
    return Success(:division_completed, number1) if number2 == 1 # halt the processing and return a successful result

    Continue([number1, number2]) # continue to the next step
  end

  def validate_non_zero(numbers)
    return Continue(numbers) unless numbers.last.zero? # continue to the next step

    Failure(:division_by_zero, 'arg2 must not be zero') # halt the processing and return a failed result
  end

  def divide((number1, number2))
    Success(:division_completed, number1 / number2) # halt the processing and return a failed result
  end
end

When Continue addon is DISABLED:

  1. Failure will halt the step chain;
  2. Success will continue the step chain.

When Continue addon is ENABLED:

  1. Failure will halt the step chain;
  2. Success will halt the step chain;
  3. 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.

@mrbongiolo mrbongiolo changed the title Success should halt the chain when using Continue addon Success should halt the step chain when using Continue addon Oct 26, 2023
@serradura serradura added this to the 0.9.x milestone Jan 1, 2024
@serradura serradura added the enhancement New feature or request label Jan 1, 2024
@serradura serradura self-assigned this Jan 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants