3

I've seen some differing views on whether or not you should include the units of measurements in variables and I'm not quite sure how to deal with corner cases.

For example if I had a variable that stored the "acceleration" obtained from an accelerometer would I have to explicitly state in the variable name its units of measurement which is meters per second squared?

Double acceleration; // m/s^2
// or this
Double accelerationMetersPerSecondSquared
// or maybe this
Double accelerationMeterPerSecSqr

As you can see, my concern is that the variable name can become quite long if the units of measurement are complex and if it's something like acceleration which is known to be in the units of m/s^2 is it necessary to have it?

Of course in other cases it is necessary to have units of measurement such as having a variable to store time.

int time; // time in what unit??
// this is more clear
int timeInSeconds;
Belphegor
  • 1,683
  • 4
  • 23
  • 44
  • IMHO, I always prefer readable variables names, long or not. However, I have already seen code where a class is created if the unit of measurement is an important information, encapsulating operations, and unit checking. It's an interesting question and my suggestion is to do what you and your team think it's best. Decide with your team whats the format all understands better. Newcomer or not. – rafaelim Jul 03 '19 at 18:08
  • 3
    None of the above. Express units in the type, not the variable name; then you can't screw it up. `java.time.Duration` can capture a number of seconds, or minutes, or whatever; you can convert it into seconds when you want, but what you're _trying to express_ is some duration in time. Write your own class for `Acceleration`; create appropriate ways of getting it out of distances and velocities and times. – Louis Wasserman Jul 03 '19 at 18:20
  • 1
    Long variable names aren't inherently a bad thing. The goal is to enhance readability and reduce cognitive overhead when reading the code. In terms of acceleration, it's usually assumed that that your units are m/s^2. In fact, if you typically operate with SI units, I'd advise omitting units *unless* they're non-standard (or not trivially inferred). As for the "time" variable, I'd advise against using that name, because it could imply a timestamp, or it could be a duration. And is it seconds or milliseconds? I think that "durationInSeconds" would remove that ambiguity. – Jordan Jul 03 '19 at 18:25
  • It depends a lot. While the benefit of including the unit where there is reason for doubt is clear to see, IMHO *always in all names* is way too strong and rigid. – Ole V.V. Jul 04 '19 at 10:26

1 Answers1

5

At least for the second part, the latest updates for the Java "date time" APIs have a simple answer.

int time; // time in what unit??

turns into:

Duration somePeriodOfTime ...

That probably tell us what the ideal solution for the first examples could be: to define distinct types, too. So don't use a double that represents square somethings.

Create a class (ideally with "value" semantics) like Area that always contains a numerical value (maybe a double) together with its unit.

That unit could then be implemented as enum, and then you nicely add some built-in conversion code that does the square feet to quadrat meter thing almost magically.

GhostCat
  • 137,827
  • 25
  • 176
  • 248