-
Notifications
You must be signed in to change notification settings - Fork 11
Improvements to throwIf methods #39
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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