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.

Should returning false rather than throwing an exception be better behaviour?

See original GitHub issue

The code looks very good, but I think it is more useful if ‘IsDefinedFast(string states)’ returns false rather than throwing an exception. This means that if the string is not defined in the enum collection and we get back ‘false’ and can act on that in the if statement.

Here is my example:

// currently throws an exception (would be better if it returned false)
if (UserTypeTestEnumExtensions.IsDefinedFast("Gien"))
{
    System.Console.WriteLine("'Gien' is defined");
}
else
{
    System.Console.WriteLine("Not defined is 'Gien'"); 
}


Here is the pseudo code for what I am suggesting:

public static bool IsDefinedFast(string states)
{
    return states switch
    {
        nameof(UnitTests.UserTypeTest.Men) => true,
        nameof(UnitTests.UserTypeTest.Women) => true,
        nameof(UnitTests.UserTypeTest.None) => true,
        _ => false
    };
}

This is only for this specific scenario that I suggest returning false.

What do you think?

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Ajay100000commented, Dec 5, 2022

I have tried 1.0.13 out. It works perfectly. Thanks again for making this change.

0reactions
EngRajabicommented, Dec 5, 2022

Thanks, that is really good. Will this be released as a package? v1.0.12 seems to be from may 31st.

Thanks again, this will be very useful.

Yes, the version is finalized in 1.0.13. There should be no problem

Read more comments on GitHub >

github_iconTop Results From Across the Web

Function returning true/false vs. void when succeeding and ...
Therefore, deciding between throw and return requires other criteria. Throwing exceptions should often be avoided if it endangers the efficiency ...
Read more >
Should I throw an exception or return false?
As informally agreed, whenever you use TryXXXX pattern, your method must be always successful, but return actual success result as boolean.
Read more >
Should I throw an exception or return a boolean? : r/java
The first form is fine, you're making sure your arguments are valid and throwing a runtime exception if they're not. If you're passing...
Read more >
Safe PHP: throwing exceptions instead of returning false
For historic reasons, the PHP core functions return false on failure instead of throwing exceptions. Let's see how we can fix this!
Read more >
Clean Code and the Art of Exception Handling
Rather than fearing exceptions, we should embrace them and learn how to use them to make our code ever better, cleaner, and more...
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