https://kotlinlang.org logo
#naming
Title
l

Landry Norris

06/21/2023, 9:52 PM
How should units in variable names be handled? Often, casing changes the meaning significantly. A variable currentLimitMA technically implies that the current limit is in mega-amps, which is very different from the likely intended milli-amps. I really don't like currentLimitmA, since it makes word boundaries unclear.
I can imagine a variable capacitorPF, which if read 'correctly', refers to PetaFerads, a very different scale than picoFarads, and a value much larger than you'd ever see in the real world.
e

Emil Kantis

06/21/2023, 9:58 PM
how are you getting data into these variables? could be a good idea to simply model the units as a value class and accept a "Current" and "Impedance" rather than just a number.. and then that could always be in base units. A bit similar to how a Duration can be either a millisecond or a year.
e

ephemient

06/21/2023, 9:58 PM
㎃ is a unicode character
\u3383
🙂
but for real, I'd consider a wrapper class that conveys the SI unit
if that's not an option, then I'd spell it out longhand
l

Landry Norris

06/21/2023, 10:01 PM
I'd generally prefer wrapper class too and will likely do that for the jvm portion. I also have to make a TypeScript library, so won't have value classes there.
e

Emil Kantis

06/21/2023, 10:03 PM
unless it's performance critical code, I'd take a non-inlined wrapper in TS over the ambiguity any day 🙂
e

ephemient

06/21/2023, 10:04 PM
or if you make the assumption that nobody would use units of current other than amperes,
currentLimitMillis
, similar to many time-based APIs using
toMillis()
etc. assuming that it's simply understood to be multiples of seconds
http://xahlee.info/comp/unicode_units.html if you want to try my initial non-serious answer 😛
k

Klitos Kyriacou

06/22/2023, 8:18 AM
http://xahlee.info/comp/unicode_units.html if you want to try my initial non-serious answer
Wow, I never knew there were so many symbols, and all this time I've been writing "a.m." and "p.m." instead of "㏂" and "㏘". By the way, I don't think "㏙" is related to time - I know it as "parts per million" as used in chemistry.
r

Rob Elliot

06/22/2023, 8:38 AM
but for real, I'd consider a wrapper class that conveys the SI unit
👍 I love
java.time.Duration
precisely because you stop worrying about the units. No more
timeoutMillis
, you can just call it
timeout
. (Staggering number of libraries just call it
timeout
and use a Long as the type, of course... and have documentation that helpfully explains this represents "the time before the operation will fail")
1
8 Views