Hacker News new | ask | show | jobs
by janjan 5410 days ago
I use some kind of Hungarian notation in which I do not describe the type of a variable in its name but instead its unit. Since I do a lot of medical image processing I have to deal with a lot of different coordinate system. For example, one is the "real world" which is measured in mm and one is the image as an array which unit is given in pixel. By appending _pix and _mm to variable you can see that some things are just wrong. For example

  pos_x = curr_pos_x + diff_pos_x
is not clear, but from

  pos_x_mm = curr_pos_x_mm + diff_pos_x_pix
it is clear that something is very wrong in this line.

edit: I just saw that this is basically what [1] is about.

[1] http://www.joelonsoftware.com/articles/Wrong.html

2 comments

The later statically typed languages like Haskell make it relatively easy to create a type which is internally stored as some base type, like an Integer, but at the type system level is treated as a distinct type. Thus one can not accidentally multiply a Pixel by a Millimeter when trying to compute an area on the screen, while under the hood pixels and millimeters are still just Ints (or whatever). "Real" Hungarian is a good hack for languages lacking that.
If you're willing to go even further with Haskell you can do some really crazy stuff: http://code.google.com/p/dimensional/wiki/GMExample
If you are using C++, you can make the units (dimensionality) compile-time checked using templates. See: http://learningcppisfun.blogspot.com/2007/01/units-and-dimen... (that isn't where I came across it, but the code looks basically the same.) It's easy to add more dimensions for any units you care about (pixels for example).
Thanks, that looks very interesting! I wish I would have used this years ago, but (un)fortunately I switched over to Python (and some C for extensions).