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

refactor: Apply Result pattern to avoid returning null in private methods #169

Merged
merged 3 commits into from
Jun 15, 2024

Conversation

MrDave1999
Copy link
Owner

@MrDave1999 MrDave1999 commented Jun 15, 2024

This pattern has been applied for two reasons:

  • It makes the producer code more expressive because it uses factory methods such as Success and Failure instead of returning a null value.
  • It makes the consumer code more expressive because it uses properties such as IsSuccess and IsFailed instead of performing a null check.

Expressive code means that it indicates its own intention so that it is clear at first glance.

Performance

Performance will not be affected because the "ref struct" type is used.
See https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/ref-struct

You can use the ref modifier in the declaration of a structure type. Instances of a ref struct type are allocated on the stack and can't escape to the managed heap.

This means that there will be no load on the garbage collector, reducing the memory reallocations on the heap.

@MrDave1999 MrDave1999 added the refactor Refactoring existing code, to make it more readable, cleaner or more efficient label Jun 15, 2024
@MrDave1999 MrDave1999 merged commit d8c7a65 into master Jun 15, 2024
1 check passed
@MrDave1999 MrDave1999 deleted the refactor-12 branch June 15, 2024 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Refactoring existing code, to make it more readable, cleaner or more efficient
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant