-
Notifications
You must be signed in to change notification settings - Fork 408
Removed the obsolete methods (Min/Max) from UnitMath
#1601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Removed the obsolete methods (Min/Max) from UnitMath
#1601
Conversation
|
@angularsen We're currently left with one extension method (
Here are my comments from the other thread regarding these:
I wouldn't mind keeping the |
I like that |
| [Fact] | ||
| public void MinOfLengthsCalculatesCorrectly() | ||
| { | ||
| var units = new[] {Length.FromMeters(1), Length.FromCentimeters(50)}; | ||
|
|
||
| Length min = units.Min(LengthUnit.Centimeter); | ||
|
|
||
| Assert.Equal(50, min.Value); | ||
| Assert.Equal(LengthUnit.Centimeter, min.Unit); | ||
| } |
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.
I was looking to remove these tests (and the functions) from the 🐲 PR and it appears that there I've actually added the linq-equivalent:
[Fact]
public void MinOfLengthsCalculatesCorrectlyWithLinq()
{
var units = new[] { Length.FromMeters(1), Length.FromCentimeters(50) };
Length min = units.Min();
Assert.Equal(50, min.Value);
Assert.Equal(LengthUnit.Centimeter, min.Unit);
}Do you think we want such tests here?
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.
It can be useful enough to have a few of these I think, to spot mistakes when messing with comparison implementations in the future
|
Ok here's a more subtle one- consider the following two versions of the public static TQuantity Clamp<TQuantity>(TQuantity value, TQuantity min, TQuantity max)
where TQuantity : IQuantityOfType<TQuantity>, IComparable<TQuantity>
{
UnitKey targetUnit = value.UnitKey;
TQuantity minValue = UnitConverter.Default.ConvertToUnit(min, targetUnit);
TQuantity maxValue = UnitConverter.Default.ConvertToUnit(max, targetUnit);
if (minValue.CompareTo(maxValue) > 0)
{
throw new ArgumentException($"min ({min}) cannot be greater than max ({max})", nameof(min));
}
if (value.CompareTo(minValue) < 0)
{
return minValue;
}
if (value.CompareTo(maxValue) > 0)
{
return maxValue;
}
return value;
} public static TQuantity Clamp<TQuantity>(TQuantity value, TQuantity min, TQuantity max)
where TQuantity : IQuantity, IComparable<TQuantity>
{
if (min.CompareTo(max) > 0)
{
throw new ArgumentException($"min ({min}) cannot be greater than max ({max})", nameof(min));
}
if (value.CompareTo(min) < 0)
{
return min;
}
if (value.CompareTo(max) > 0)
{
return max;
}
return value;
}They both have one thing in common: they avoid calling the The difference is obviously in whether the The good news is that since the There are no bad news here, just wondering if you'd rather keep the original behavior regarding the unit or not (personally if I was writing this function from scratch I would have preferred the second version, but I guess someone must have had a reason to do it the other way..). |
|
I'm not entirely sure, these two seem very equivalent to me, but off the bat I like the second version better with fewer constraints. |
|
LGTM merging Remaining points:
|
Removed the obsolete methods from
UnitMath:Min(IEnumerable, TUnit)Min(IEnumerable, Func)Max(IEnumerable, TUnit)Max(IEnumerable, Func)