Argument icon indicating copy to clipboard operation
Argument copied to clipboard

Add support for comparisons

Open mediocresurgeon opened this issue 3 years ago • 4 comments

Adds the following methods: Argument.GreaterThan() Argument.GreaterThanOrEqualTo() Argument.LessThan() Argument.LessThanOrEqualTo()

mediocresurgeon avatar Jan 21 '21 21:01 mediocresurgeon

The technical work looks great, but I wonder if use cases (not already covered by Positive and similar checks) are common enough. What are some use cases you have in mind for this one?

ashmind avatar Feb 24 '21 17:02 ashmind

The technical work looks great, but I wonder if use cases (not already covered by Positive and similar checks) are common enough. What are some use cases you have in mind for this one?

One real-world example might be String.Substring(int, int).

With the new functionality, we would be able to express the boundary conditions like so:

public string Substring (int startIndex, int length)
{
    Argument.PositiveOrZero(nameof(startIndex), startIndex); // or Argument.GreaterThanOrEqualTo(nameof(startIndex), startIndex, 0);
    Argument.LessThan(nameof(startIndex), startIndex, this.Length);
    
    Argument.PositiveOrZero(nameof(length), length); // or Argument.GreaterThanOrEqualTo(nameof(length), length, 0);
    Argument.LessThanOrEqualTo(nameof(length), length, this.Length - startIndex);
    
    // Implementation
}

mediocresurgeon avatar Feb 24 '21 19:02 mediocresurgeon

Thanks! Yep I was thinking of something like that or Array.CopyTo and similar. But feels like there are not that many of those?

Also just remember one other thought I had when I considered similar methods for v1: I feel in those specific examples general Less/Greater might not give a clear enough error.

E.g Argument.LessThan(nameof(startIndex), startIndex, this.Length); will tell you "Value cannot be greater than or equal to 10", which does explain why exactly, or what 10 is. What would be good instead is a custom exception message, e.g. "Start index cannot exceed string length (10)".

Similarly, instead of Argument.LessThanOrEqualTo(nameof(length), length, this.Length - startIndex); we might consider something like "Substring of length 10 starting at index 5 ends at index 15, which is located beyond the end of the string (10)" (that might be too verbose, first wording I could think of).

You could argue the same point about e.g. Positive... methods. But I feel 0 is kind of special -- e.g. for length getting "Value must be positive" seems more clear on its own than "Value must be less than 17" (what's 17?).

ashmind avatar Feb 27 '21 07:02 ashmind

You're right--the proposed exception message might be adequate for constants, but it doesn't meaningfully communicate how to untangle a more dynamic situation (eg "startIndex plus length indicates a position not within this instance").

How would you feel about including an overload for these new methods which allows the exception message to be specified?

mediocresurgeon avatar Mar 05 '21 21:03 mediocresurgeon