You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The current approach to constructing quantities is very verbose, for example:
Power.from_watt(10)
Energy.from_kilo_watt_hour(10)
Not only is very long (which means it takes also a lot of context when reading), but also it not was nice/natural to see as it would be if literals like 10W could be used directly.
If we provide some more likeral-like constructors, this could help readability quite a bit.
We could also consider having more verbose aliases, like power(10).watt, but I'm not sure if it makes sense, as the point of these constructors is to be succinct.
The drawbacks
So, the clear advantage is improved readability, but this comes with some costs. Mainly I can see:
Runtime overhead: Now we need to create one extra instance just to throw it away immediately.
Maintenance cost: Even when this is mostly a one-time cost, as once a quantity is created is rarely changed, we still need to make sure to keep the 2 classes in sync in the rare cases when changes happen.
More cognitive load for learning: Now people need to understand (or at least know) that there are 2 types for each quantity, and when to use which (for example, for type hints, the proper type still needs to be used, so both types will be used regularly).
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
The issue
The current approach to constructing quantities is very verbose, for example:
Power.from_watt(10)
Energy.from_kilo_watt_hour(10)
Not only is very long (which means it takes also a lot of context when reading), but also it not was nice/natural to see as it would be if literals like
10W
could be used directly.If we provide some more likeral-like constructors, this could help readability quite a bit.
The proposal
Add a class like:
We could also consider having more verbose aliases, like
power(10).watt
, but I'm not sure if it makes sense, as the point of these constructors is to be succinct.The drawbacks
So, the clear advantage is improved readability, but this comes with some costs. Mainly I can see:
Beta Was this translation helpful? Give feedback.
All reactions