[Question] Is it better to return 'Result' or 'Maybe' from methods? -- .FirstOrDefault() -- What is the recommended return type?
See original GitHub issueHello!
The title basically says it all.
Let’s say I have a method like this which uses EFCore and queries the DB:
// This method takes a 'where expression' as a parameter, then returns the first result in the result set.
// If there are no results, null is naturally returned (thank you microsoft)
// Take note that it is nullable
public Student? GetFirstStudent(IQueryable<Student> queryable)
{
// dbContext removed for the example...
return queryable.FirstOrDefault();
}
In the example above, and based on the examples in the README.md I would think that the return type should be Result<Student>
So if the ‘Student’ is null, then the Result will be failed.
BUT
Isn’t that the same purpose and functionality as Maybe<T>?
Questions
1.) When is it appropiate / recommended to return Result<T>
2.) When is it appropiate / recommended to return Maybet<T>
Thanks all
Issue Analytics
- State:
- Created 10 months ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
c# - When to use .First and when to use .FirstOrDefault with ...
First() will throw an exception if there's no row to be returned, while .FirstOrDefault() will return the default value ( NULL for all...
Read more >Difference Between First() And FirstOrDefault()
FirstOrDefault() returns the default value (null) if there is no result data and you can see the result in the following screen.
Read more >FirstOrDefault() is a Code Smell - Vasil Kosturski
Single () is the most restrictive method of all. It will return a result only if a single element matches the predicate condition....
Read more >Understanding Single, SingleOrDefault, First and ...
It returns a single specific element from a collection of elements if element match found. An exception is thrown, if more than one...
Read more >Enumerable.FirstOrDefault Method (System.Linq)
Returns the first element of the sequence that satisfies a condition, or a specified default value if no such element is found.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Well, in your particular situation
Maybe<Student>would be more appropriate.Result<Maybe<Student>>is for when for e.g you are requesting a student from an external API and that API may or may not have that student, and also it may or may not fail due to let’s say connectivity issues and you know how to process that connectivity issue.Another example is input validation. For example, you are validating a student email address when registering a student, and that address may be empty (which is allowed) or it may be not empty but invalid (which is not allowed):
May fail and also you know how to handle this failure. Otherwise, there’s no point in wrapping it in a
Result. Check out this article: https://enterprisecraftsmanship.com/posts/error-handling-exception-or-result/