UnitsNet icon indicating copy to clipboard operation
UnitsNet copied to clipboard

Supporting non-standard specialty units

Open bretthysuik opened this issue 12 years ago • 17 comments

From issue #45 concerning the topic of supporting non-standard specialty units such as power ratio with units dBmW (decibels relative to 1 milliwatt) and dBW (decibels relative to 1 watt):

As for the domain specific units, I'm not sure whether they should be part of the main library or not. I can easily see a domain specific nuget with dependency on UnitsNet, such as UnitsNet.Audio. What do you think and if you agree, what naming would be natural? Setting up a new nuget would involve creating a new set of projects for Audio, setting the output path to a Audio subfolder of the current output path and modifying the build scripts to build, pack and publish both nugets.

On the other hand, it's still a small library and maybe we can postpone splitting up into multiple nugets until there is a demand for it and possibly many more domain specific units, and bump the major version to indicate breaking changes. At this point I'm happy with either way.

bretthysuik avatar Nov 05 '14 16:11 bretthysuik

My vote would be to postpone splitting the library into multiple NuGet packages, since what I have in mind is a major change to the structure of the library:

UnitsNet.SI.Base

  • Only contains standard SI units for length, mass, time, electric current, temperature, amount of substance, and luminosity.
  • http://en.wikipedia.org/wiki/International_System_of_Units#Base_units

UnitsNet.SI.Derived

  • Contains derived SI units such as angle, frequency, force, energy, power, electric charge, electric potential, radioactivity, etc.
  • http://en.wikipedia.org/wiki/International_System_of_Units#Derived_units

UnitsNet.SI.NonStandard

  • Contains units related to SI but not part of the official SI system such as minute, hour, day, litre, electronvolt, etc. Some of these would be full-blown units themselves, others would be extension methods against existing SI units.
  • http://en.wikipedia.org/wiki/Non-SI_units_mentioned_in_the_SI

UnitsNet.Imperial

  • Contains imperial units and extension methods against SI units.
    • For example _mass.Pounds() (this isn't ideal since it has to use method syntax instead of property syntax) and Mass.FromPounds() would be defined as extension methods against the Mass SI unit and reside in the UnitsNet.Imperial namespace.
  • This keeps the base SI units clean such that imperial units need to be explicitly requested by consumers (e.g. using UnitsNet.Imperial). If someone is working only in SI units they shouldn't have their Intellisense polluted with imperial conversions and properties.

The SI projects could be part of a single NuGet package while the Imperial units could be distributed in a separate package. Or maybe all four projects are released as separate packages (this would offer the most flexibility but is a little more complicated to maintain).


I haven't worked out all the details, but this structure should allow for extension points for other developers to add non-standard units in separate NuGet packages. For example, if I wanted to add non-standard units related to decibels (dBV/dBuV/dBmV) I could fork the repo, define the units in UnitsNet.SI.NonStandard.Ratio and release my own NuGet package. The units would still be part of the UnitsNet namespaces but distributed separately keeping the core solution clean and simple.

One problem with this is that people could do whatever they want in their own forks and packages which could lead to issues.

Alternatively, we keep the same general idea as mentioned in the previous paragraphs, but instead of having people release their own separate packages, we just lump everything into UnitsNet.SI.Derived and UnitsNet.SI.NonStandard


This is a major change and complicates things quite a bit more than what currently exists so I'm not sure if you'd want to go in this direction. I think it offers better separation of concerns and extensibility points, though.

bretthysuik avatar Nov 05 '14 17:11 bretthysuik

Another issue with my suggestion is that the json files will have to become more complicated in order to correctly define which namespace the unit should reside. The scripts would also have to get a little more complicated.

For example, if a user wanted to add imperial units to an existing SI unit they would either have to do so by manually implementing them and writing the unit tests or the json would have to define that the unit is imperial and an extension against an existing SI unit.

bretthysuik avatar Nov 06 '14 16:11 bretthysuik

I'll respond to this and the ToString thread this weekend.

angularsen avatar Nov 07 '14 07:11 angularsen

I think you make several good points, but I'm still not sold on it.

A couple of thoughts:

  • Metric and imperial units are probably THE most common unit conversion scenario. I'd find it odd to use properties for metric units and extension methods for imperial units.
  • Splitting up types into namespaces like SI.Base, SI.Derived etc. to match the categorizations are definitely interesting, but I also question the need for it. Does it help the user code in any way or is it more about how the code and structure feels? Then there are conflicts like the imperial units, as you mention, that I don't like.
  • As for extensibility, I don't see how multiple namespaces help. You can already define extension methods for any unit class and put it in whatever namespace you feel like. My initial idea was to have domain specific units in separate namespaces, and typically these would be hosted on third party repos developed by someone else who had the need for it. This could be UnitsNet.ElectricalEngineering, UnitsNet.Astrophysics, UnitsNet.Geometry and so on adding whatever would be missing or useful for that field or domain. These would typically be separate nugets with dependency on UnitsNet as the core nuget having all the most widely used units. I have tried to keep the door open for third party units in Extending with custom units, but this is not battle hardened yet and I don't know of anyone using it yet.

angularsen avatar Nov 07 '14 20:11 angularsen

And then there is something about keeping things simple. Both from the using code's perspective, and for future pull requests to this project. It's rare to get active contributors like you along :-)

angularsen avatar Nov 07 '14 20:11 angularsen

I have to say that I agree with Brett about the extension method divisions for imperial conversion, it does offer better separation.

However, I am not sold on the idea of separating the namespaces too much. It hurts type discoverability unless it is obvious (to the developer, not scientist) where you would look for a given type.

I have always preferred the library separation paradigm. If you offer a library for a domain, it injects its types into the appropriate namespaces. Microsoft even does this with their own libraries. Then developers will find their types in the UnitsNET namespace like they expected, but they add on the domain specific units support they need.

I am not saying that the namespaces should be flat, just divided in ways that makes the types discoverable by the developers.

jcoder76 avatar Nov 07 '14 20:11 jcoder76

@daflame76 : What namespace structure do you vote for? If I understand correctly you want imperial units in a different namespace than SI base units and SI derived units, but not the rest? If you could map out a quick example that would be helpful to understand your point of view.

angularsen avatar Nov 07 '14 21:11 angularsen

This gets complicated because we end up segregating non-SI units into a separate library. That is what my proposal really is. However, I do not think we should make non-SI units another namespace per se. If someone goes through the trouble to pull in the US/UK imperial measurements, then that is what they will get included alongside the SI units. I think separating using scientific naming convention is great for Universities, schools, and scientists. It is not so great for programmers.

Example: Common Library contains SI units, derived units and base commons Add on library offers US gallons, pounds, etc. Another add on library offers UK gallons, pounds, etc. Where commonalities exist, a common base library can be shared offering units for both. Namespaces can remain as is. This keeps usage simple, but separates the implementation.

The other suggestion related to namespaces may be to separate by subject domain like: Geometry - length, width, point Physics - mass, force, energy

jcoder76 avatar Nov 10 '14 19:11 jcoder76

Thanks for the write up. I agree that splitting into namespaces based on scientific categorization has more drawbacks than benefit for the typical programmer usecase.

I'm still not sure about moving imperial / UK units into separate libraries though. I definitely see the point of pulling it in when you need it, but consider that metric/imperial/uk conversions are very commonly used, in fact that is how this project started in the first place.

My biggest qualms are these:

  • Does splitting up into multiple nugets/projects solve a real problem or need? I risk repeating myself, but KISS and YAGNI spring to mind.
  • You have to pull in two nugets just to get a simple metric/imperial conversion going, and in my opinion it feels a bit overkill.
  • Having to pull in the second or third nuget could hurt discoverability. A new user trying out Units.NET might miss the fact that imperial and UK units are supported if only the core nuget is pulled.
  • Extension methods are used instead of properties for units outside core library, and in my opinion that just feels inconsistent
  • Two more projects and two more nugets to manage
  • Generator scripts might get complicated, but I haven't looked into this
  • A slight inconvenience is that you will have to manually install UK and imperial nugets to get both, unless we add an "All" or "Standard" nuget that pulls the most typical combinations of nugets, such as Imperial and UK, but that also feels overkill for a project this small.

The benefits I see are:

  • Cleaner intellisense when you don't care about imperial/UK units
  • Less size. Today's nuget measures ~128kB. My gut feeling says we might get the core library down to ~80kB with today's units. I could be way off though.

Regarding domain namespaces, my intention was not to divide Mass and Force into Physics, but rather keep today's library as-is and add any extra units into engineering domains that would be too specialized to have in the core library. The recently added audio/electronics units #49 #48 #47 are good candidates to move out into a Unitsnet.Audio project and namespace.

angularsen avatar Nov 10 '14 20:11 angularsen

Nuget is pretty friendly to show up all the packages that have the same base name when you search, and if imperial units conversion is part of their lookup, they are likely to find the sub-packages which would automatically pull in the core library.

I am not bent on saying that non-SI units should be a separate package either. I agree that keeping the intellisense clean should be a desirable goal, as long as it offers better discoverability for the units you want to work with. If only there was a way to apply a filter to a package to make intellisense smarter about the suggestions it offers, but there isn't. Namespace and package separation are all we have for now.

Regarding package names, do be careful. Audio is only one target domain of decibel power ratios and signal power output. Radio frequency broadcast and Microwave are others. Signal output, analog power or some such name is too broad maybe, but that closer.

BTW: Internationally the SI volume unit is spelled Litre, not Liter. The latter is US only: http://en.wikipedia.org/wiki/Litre The same applies to Metre vs Meter. So, as you see, it is already region localized in its current implementation. I am not saying we need to make SI correct naming conventions either, just illustrating that there are already compromises that needed to be made. It just needs to be decided which ones are worth it and which are counter productive.

The best validation of design is what Microsoft has often done more recently. Build the code you WANT to see work, make a mock back-end that makes that possible. Then hand it to another developer who knows nothing about it and see what they try to do with it. Better yet if they are working in another domain of development.

jcoder76 avatar Nov 10 '14 21:11 jcoder76

I agree that NuGet is generally good at exposing dependencies and related packages, so not discovering sub packages is not a big point.

Regarding the Audio package name, I agree. It was just the first name that crossed my mind, I am not familiar with the units and would not know the proper domain name or domain names for them.

The US vs international naming convention is probably its own topic of discussion. I early on decided to go with US naming, but that is purely out of personal preference. I simply like it better and I also have the impression that US English and US naming conventions are more widespread than British English in the programming community, but that's just based on a hunch.

By all means, please feel free to do a mockup and test users on the different code structures :-) It would be awesome to get some more facts on the table rather than feelings and hunches, but it sounds like quite a bit of work.

angularsen avatar Nov 10 '14 23:11 angularsen

US naming is the standard way with .NET naming conventions, so I am not at all complaining. That was merely an example that there are compromises that need to be made to satisfy the widest audience and develop standard conventions everyone expects. I am in agreement with your choices.

We just need to figure out what those best case compromises are for this situation. If I am coding this library, this is how I see it looking:

using UnitsNet.PowerRatios;

public class RelativePowerView
{
    public RelativePowerView(PowerRatio referencePower)
    {
        ReferencePower = referencePower;
    }

    public PowerRatio ReferencePower { get; private set; }

    public PowerRatio GetRelativePower(PowerRatio measuredPower)
    {
        return measuredPower-ReferencePower;
    }

    public void UpdateRelativePower(PowerRatio measuredPower)
    {
        RelativePower.Text = GetRelativePower(measuredPower).ToString(PowerRatioType.dB);
    }
}

Not the greatest code in the world, but this style makes sense to me. So, after writing this, I may be advocating to use some namespaces for specialized domains. It is not common to mix power ratios with geometric, except maybe for purposes of manual frequency edge analysis, calibration, or digital to analog conversion using vectors and angles. Power ratios are often measured relative to energy potential: dBm/dBmW (milliwatt) and dBmV (millivolt), so power units are common. I don't see this working for all domains, because it would become restrictive when applying more common physics equations: E = mv2

This means, there are definite domains like power, geometry, motion, mass, but as energy/mass/velocity theories indicate, they are inseparable and directly linked together. In some cases interchangeable. We only need to satisfy the most common use cases, as you have already said. Make the simple scenarios simple, but the complex possible.

It seems more troublesome to me to separate the unit conversions/output types into a separate namespace, since it would commonly be used by UI code, or when attempting a special calculation such as linear power addition in power ratios when adding two powers together. However, it would be counterproductive if it cluttered the root namespaces.

jcoder76 avatar Nov 12 '14 18:11 jcoder76

Ultimately, I think separating the units into domains is a bad idea because 1) it's hard to exactly define domains in some cases and 2) units often cross multiple domains. This will lead to discoverability problems.

bretthysuik avatar Nov 12 '14 21:11 bretthysuik

This is definitely something that needs a voting system to see what the wider audience thinks. Assuming we can get enough people.

We are only three people. Hardly enough for more than conjecture and definitely not representative of the wider audience.

jcoder76 avatar Nov 13 '14 12:11 jcoder76

At this point I see no good reason to do any major changes to the library structure. I personally like that there is a single nuget that covers the majority of usecases of unit conversion and representation. I'm fine with the fact that there are a couple of less widely used units in there.

I would like to postpone this topic for later when we start getting more requests for specialized units, so that there is a real need to move things out to keep it lean. Hopefully at that point we have more insight and a wider perspective to better consider what units should go where.

If you agree with me, then I vote to close this topic and reopen it at a later time.

angularsen avatar Nov 13 '14 18:11 angularsen

If you agree with me, then I vote to close this topic and reopen it at a later time.

Agreed.

bretthysuik avatar Nov 14 '14 16:11 bretthysuik

Agreed.

jcoder76 avatar Nov 15 '14 05:11 jcoder76