473,320 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Putting units in identifiers?

I just saw an interesting identifier in a C# book I'm reading...

double dollarsPerHead

This jumped out at me, as it is not common to put units in an identifer. We
don't see "millisecondsSinceStart" ("time"), or "milesPerHour" ("speed").
But, perhaps it's a good idea. If memory serves me right, one of the Mars
landers crashed because two development teams used different units (metric
and imperial), across an interface. Also, when I'm coding, I'm frequenctly
checking units against specs, when it would be easier just to read the
identifier.

I think that a project should be consistent -either always include the
units, or never include them.

My opinion - if the unit is included, then programmers will trust the
identifier, rather than specs, and if the unit ever changes, but the
identifier doesn't, then the identifier is misleading. Therefore, units
should never be included in identifiers. My general approach to "conventions"
is to avoid conventions which can't be enforced automatically.

So I would replace "dollarsPerHead" with "ticketPrice"

Opinions?

Javaman.

oh, the book is "Design Patterns in C#", by Steve Metsker. I highly
recommend it. :)
Nov 17 '05 #1
5 2188
I agree, if you're going to pass quantities around as ints, decimals,
or doubles, for example.

I solved the problem a different way: I created the following:

a UnitOfMeasure class that encapsulates the "unit of measure" concept:
minutes, pounds, feet, sqaure meters, etc.

a Measure structure that encapsulates a value and a unit of measure.

a PricePerUnit structure that encapsulates a value, a currency, and a
unit of measure.

Originally I envisoned a very rich, complex system that would support
arbitrary combinations of units of measure and allow multiplication and
division operations on them, but it turned out we didn't need anything
that sophisticated here. It was enough to be able to automatically
convert between feet, inches, millimeters, and other such units, so I
came up with a conversion class to handle standard conversions, and I
was done.

I encourage everyone in our group to return Measures, not decimals or
ints. That way the UoM follows the value around wherever it goes.

However, if we were to just pass basic types, I would insist on
including units in names. Few things suck worse that finding a value in
some far-flung corner of the system and having no idea in which units
its expressed. (Recent example: a column in our database mistakenly
labeled "SPECIFIC_GRAVITY" that really contained pounds per (foot times
square millimeters). Don't ask.)

Nov 17 '05 #2
Javaman59 wrote:
I just saw an interesting identifier in a C# book I'm reading...

double dollarsPerHead

This jumped out at me, as it is not common to put units in an identifer. We
don't see "millisecondsSinceStart" ("time"), or "milesPerHour" ("speed").
But, perhaps it's a good idea. If memory serves me right, one of the Mars
landers crashed because two development teams used different units (metric
and imperial), across an interface. Also, when I'm coding, I'm frequenctly
checking units against specs, when it would be easier just to read the
identifier.

I think that a project should be consistent -either always include the
units, or never include them.

My opinion - if the unit is included, then programmers will trust the
identifier, rather than specs, and if the unit ever changes, but the
identifier doesn't, then the identifier is misleading. Therefore, units
should never be included in identifiers. My general approach to "conventions"
is to avoid conventions which can't be enforced automatically.

So I would replace "dollarsPerHead" with "ticketPrice"


The problem is that if you're implicitly saying that if the unit isn't
included, then programmers will consult the spec. That's not my
experience.

I've only recently started including units, particularly for timing -
things like:
void DoSomething (int timeoutSeconds)

and also, while not strictly a unit:
void DoSomething (DateTime timeUtc)

It's made things a lot easier so far.

Note that if you're changing the spec, then the identifier is only one
change - and a very easy one at that. I would say there's far more risk
from identifying all the places it's used, and changing those
accordingly. Where a property name includes the units/timezone, this is
actually made a lot easier: you can change the name to indicate the new
unit/timezone, and that will be a breaking change - you can find all
the uses just by trying to build again :)

Jon

Nov 17 '05 #3
To clarify, when I said "I agree" I meant that I was agreeing with the
idea of including units in names if you're not going to create a
Measure structure. Consider the following lines of code:

decimal speed = 17.5;
decimal length = 20.5;

How fast is that? How long is that? You have no idea. Whether those are
variables, parameters to methods, or properties, the same problem comes
up: the numbers are meaningless. Compare that with this:

decimal speedKph = 17.5;
decimal lengthFeet = 20.5;

Now you know exactly what the numbers mean. In our system, we take it a
step farther and do something like this:

Measure length = new Measure(20.5, UnitOfMeasure.Feet);

Now we can pass "length" around as a Measure, and any part of the
system can unpack it and see exactly how long it is. In particular, we
can do this:

if (length < new Measure(10, UnitOfMeasure.Inches) || length > new
Measure(20, UnitOfMeasure.Feet))

which in our industry is valuable indeed.

Putting units in variable names makes values meaningful to the
programmer. Creating a structure that contains units and a quantity
makes values meaningful to the code.

Nov 17 '05 #4
Thankyou both for your excellent replies.

I like Bruce's proposal for a Measure class and PricePerUnit class. There is
some overhead, but it could save time on a medium to large projects, and it
is re-usable between projects.

I agree with both of you that in the absence of a Measure class, then we
should put units in our identifiers.

These proposals are especially helpful for software teams, but scale down
well also. Even in my currrent project (1 man year), I find that I keep
forgetting the unit of measure on variables (nanoseconds? milliseconds?..),
and either waste time looking it up, or take a guess, and hope for the best.
:)

Regards,

Javaman

"Javaman59" wrote:
I just saw an interesting identifier in a C# book I'm reading...

double dollarsPerHead

This jumped out at me, as it is not common to put units in an identifer. We
don't see "millisecondsSinceStart" ("time"), or "milesPerHour" ("speed").
But, perhaps it's a good idea. If memory serves me right, one of the Mars
landers crashed because two development teams used different units (metric
and imperial), across an interface. Also, when I'm coding, I'm frequenctly
checking units against specs, when it would be easier just to read the
identifier.

I think that a project should be consistent -either always include the
units, or never include them.

My opinion - if the unit is included, then programmers will trust the
identifier, rather than specs, and if the unit ever changes, but the
identifier doesn't, then the identifier is misleading. Therefore, units
should never be included in identifiers. My general approach to "conventions"
is to avoid conventions which can't be enforced automatically.

So I would replace "dollarsPerHead" with "ticketPrice"

Opinions?

Javaman.

oh, the book is "Design Patterns in C#", by Steve Metsker. I highly
recommend it. :)

Nov 17 '05 #5
> I like Bruce's proposal for a Measure class and PricePerUnit class.

You'll find that it will work better if you make them structs, and make
UnitOfMeasure an immutable class.

Just thought I'd save you some grief. :-)

Nov 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Doug Fort | last post by:
This is an excerpt from a much longer post on the python-dev mailing list. I'm responding here, to avoid cluttering up python-dev. <snip> >Some English readers might not really imagine, but it...
2
by: TheSeeker | last post by:
Hi, As part of a larger project, I am trying to use the GNU Units program to provide unit conversions between quantities. My first iteration, which worked OK, was to simply use units as a...
5
by: dhruba.bandopadhyay | last post by:
I am trying to port an old Pascal DOS game to DOS C/C++. I am wondering if anyone is familar with the dos & crt Pascal units and whether there are C/C++ equivalent libraries. Maybe dos.c & crt.c? ...
6
by: DaTurk | last post by:
Hi, I'm a bit rusty with the wonderful language of c++. I worked with it, maybe 6 or 7 years ago and now I need to knock the dust off. But I was perusing some code when I noticed that this one...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.