Skip to content

Add support for more elegant handling of data results #4

@WithLithum

Description

@WithLithum

The current handling of data results are incredibly complex, and the code that handles the result objects are responsible for the majority of the cognitive complexity of a method (not to mention the amount of time needed to type them in), as shown below:

var resultA = MethodA().RequireNonEmpty();
if (resultA.Error != null)
{
    return DataResult<ObjectA>.CausedBy(resultA);
}

return MethodB(resultA.Result!);

This issue proposes new data result handling APIs that have two level distinctions:

Level 1

  • Implicitly converts OpResult to a failure or empty DataResult
  • Add an ! operator that returns true if the result is not successful
  • Add a Fold method to convert DataResult to OpResult
  • Introduce Results class which support implicit type arguments for data results better

This turns the above code down to:

var resultA = MethodA().RequireNonEmpty();
if (!resultA)
{
    return resultA.Fold();
}

return MethodB(resultA.Result!);

While this do not do much for cognitive complexity, it is way easier to type.

Level 2

Making APIs accept DataResult directly will mean that a failure result can be passed down and the same result will be returned. So this would become:

return MethodB(MethodA());

Significantly better because method chaining is now possible.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions