UnitsNet icon indicating copy to clipboard operation
UnitsNet copied to clipboard

Replacing the ToString overloads with extensions

Open lipchev opened this issue 8 months ago • 4 comments

  • introducing the QuantityExtensions (in the Extensions folder) with the first two extensions for the ToString overloads
  • removed the ToString(IFormatProvider?) overload from the IQuantity interface
  • removed the ToString overloads from the generated quantities (and HowMuch)

lipchev avatar Apr 19 '25 22:04 lipchev

Ok, here is the size difference:

before: 2.19 MB (2 301 440 bytes) after: 2.17 MB (2 286 080 bytes)

Since we've only got one generic parameter, the bug with Resharper does not apply.

The only breaking parts concern the cases where a given class doesn't have the UnitsNet namespace import (e.g. when given a type that contains a quantity, which is then accessed without assigning to a variable).

I'll make separate PR, with the other version which simply removes the overloads and makes the parameters optional.

lipchev avatar Apr 19 '25 22:04 lipchev

Yeah, I don't like how default parameters for ToString() make it awkward to only pass a culture. It's just so common to have relevant ToString() overloads for this. https://github.com/angularsen/UnitsNet/pull/1553#discussion_r2051710626

Extension methods could work though, but it does have some potential developer expirence friction as you point out regarding missing namespace imports. I expect this to be fairly uncommon though and it only affects 2 of 4 ToString() overloads, and IDEs may do a decent job of importing namespaces for you.

I guess it comes down to, is the 25 kB binary size reduction worth a potentially worse developer experience? ToString() is used a lot.

I'm kinda leaning towards maybe not enough impact to justify this change, what do you think? Or we could try it and see how much outcry there is. Maybe very few are impacted.

angularsen avatar Apr 20 '25 12:04 angularsen

With the perspective that we want to try and use extension methods more in general to avoid duplicating code for N quantities, maybe we'll just have to try this and see how it feels.

angularsen avatar Apr 20 '25 12:04 angularsen

For me personally it's just the interface method that's bothering me.. It might look like I'm optimizing for size, but that's really not a much of priority (in my use case).

lipchev avatar Apr 20 '25 12:04 lipchev

This PR is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Jun 20 '25 02:06 github-actions[bot]

This PR was automatically closed due to inactivity.

github-actions[bot] avatar Jun 27 '25 02:06 github-actions[bot]

Merged as part of #1587

angularsen avatar Aug 09 '25 10:08 angularsen