Uh oh!
There was an error while loading. Please reload this page.
Improvements to throwIf methods - #39
Conversation
Daniel Jurek (danieljurek)
left a comment
There was a problem hiding this comment.
👍
Makes sense that ThrowIf.Null is useful outside of constructors.
Some small commentary for consideration.
| => o ?? throw (calledFromMember == ".ctor" | ||
| ? (Exception)new ArgumentNullException( | ||
| paramName, | ||
| $"Null argument {paramName} passed to {calledFromMember}in file {calledFromFile} at line {sourceLineNumber}" |
There was a problem hiding this comment.
The call stack will already have the calling method name, file, and line number info. Not sure if it's valuable to keep in here.
Also, if you do keep it, put a space between {calledFromMember}in...
| $"{paramName} found to be null or whitespace in method {calledFromMember} in file {calledFromFile} at line {sourceLineNumber}", | ||
| paramName | ||
| ) | ||
| : new NullReferenceException( |
There was a problem hiding this comment.
Nitpicking: If you're checking for whitespace/empty string you might want to throw an ArgumentException when the value is an empty or whitespace string.
| [CallerLineNumber]int sourceLineNumber = -1 | ||
| ) where TParam : class | ||
| => o ?? throw (calledFromMember == ".ctor" | ||
| ? (Exception)new ArgumentNullException( |
There was a problem hiding this comment.
One weird corner case would be if the constructor was throwing because of null when checking something that's not an argument:
private object resultFromSomeMethod;
public MyClass(string param) { this.resultFromSomeMethod = SomeMethod(param);
ThrowIf.Null(this.resultFromSomeMethod); }
Not sure if/how we should handle that scenario outside of using code analysis at build time.
to improve tracing and increase flexibility of usage