question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Is it better to return 'Result' or 'Maybe' from methods? -- .FirstOrDefault() -- What is the recommended return type?

See original GitHub issue

Hello!

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:closed
  • Created 10 months ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
vkhorikovcommented, Dec 1, 2022

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):

private Result<Maybe<EmailAddress>> ValidateEmailAddress(string emailAddress)
{
// ...
}
0reactions
vkhorikovcommented, Dec 13, 2022

Fail as in some issue arises where the task completes with an error

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/

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found