From: Roedy Green on
Consider a program that uses a mixture of 10-character and 13-digit
ISBN numbers (International Standard Book Number). From Java's point
of view, they are all Strings.

It would be nice if there were some mechanism to label Strings with a
light-weight type, probably a purely compile-time concept, similar to
generics. You would then label some string vars as ISBN13 and some as
ISBN10, and similarly parameters to methods. Then the compiler could
detect an improper passing of in ISBN13 string to an ISBN10 parameter.

An extension of this idea is units of measure, so that you could not
pass a cm variable to an inch parameter (though possibly you could
with an automatic conversion applied).

Think how many times you have wondered if a variable is measured in
seconds, ms, or ns. Here would be a formal way to specify and check
your assumption was correct.

Going a step further, your type might have a associated assertion to
make sure the value were valid. Perhaps there would be a way to
insert that assertion at every assignment.
--
Roedy Green Canadian Mind Products
http://mindprod.com

When you are programming and you feel paralysis from overwhelm:
(o) Write down your worries in an electronic todo list, so you can temporarily stop thinking about them.
(o) Write smaller modules and classes so you don�t need to juggle many facts to complete the code.
(o) Do some necessary, but simple, clerical task.
(o) Solve some simple problem with at least some probability of being useful in the big solution. It will break the logjam.
~ Roedy
From: Michal Kleczek on
Roedy Green wrote:

> Consider a program that uses a mixture of 10-character and 13-digit
> ISBN numbers (International Standard Book Number). From Java's point
> of view, they are all Strings.
>
> It would be nice if there were some mechanism to label Strings with a
> light-weight type, probably a purely compile-time concept, similar to
> generics. You would then label some string vars as ISBN13 and some as
> ISBN10, and similarly parameters to methods. Then the compiler could
> detect an improper passing of in ISBN13 string to an ISBN10 parameter.
>
> An extension of this idea is units of measure, so that you could not
> pass a cm variable to an inch parameter (though possibly you could
> with an automatic conversion applied).
>
> Think how many times you have wondered if a variable is measured in
> seconds, ms, or ns. Here would be a formal way to specify and check
> your assumption was correct.

That is not so easy to make it right. Read for example:
http://research.sun.com/projects/plrg/Publications/p109-allen.pdf

--
Michal
From: markspace on
Roedy Green wrote:
>
> It would be nice if there were some mechanism to label Strings with a
> light-weight type, probably a purely compile-time concept,...
>
> An extension of this idea is units of measure, so that you could not
> pass a cm variable to an inch parameter (though possibly you could
> with an automatic conversion applied).


While I understand what you're saying, I think just making wrapper
classes is correct, and pretty darn light-weight too. Once you make a
ISBN10 label, you have to verify that you only attach this label to
valid objects, and then you have to define any special operations that
work with this label, and then... you've got a class anyway.
From: Kenneth P. Turvey on
On Mon, 28 Sep 2009 10:50:57 -0700, markspace wrote:

> While I understand what you're saying, I think just making wrapper
> classes is correct, and pretty darn light-weight too. Once you make a
> ISBN10 label, you have to verify that you only attach this label to
> valid objects, and then you have to define any special operations that
> work with this label, and then... you've got a class anyway.

I agree. Most of the time when you are using primitive types to hold
information that really has a meaningful type of its own you are making a
mistake.

A common example is an email address. People are always using strings to
hold email addresses, but if you are using them for anything other than
just a key, they really need a class of their own.


--
Kenneth P. Turvey <evoturvey(a)gmail.com>
From: alexandre_paterson on
On Sep 28, 6:18 pm, Roedy Green <see_webs...(a)mindprod.com.invalid>
wrote:
> Consider a program that uses a mixture of 10-character and 13-digit
> ISBN numbers (International Standard Book Number). From Java's point
> of view, they are all Strings.

I kinda see what you mean but I'm puzzled by your "they're all
Strings".

Why would they be all Strings?

Why not use real OO abstraction for the ISBNs?

What about:

interface ISBN {

... getEAN();

... getCountryOrArea();

... isCheckDigitCorrect();

etc.

}

?

Say, here's a nice Douglas Cockford base 32 encoded product
key we're using for our software (fake number ;)

4BTZA0 - T85Q - 2HNSQ8 - MBX2 - 70PHVV - BSZD - QCN2D1

In our codebase we don't treat that as a String...
It's a ProductKey and our ProductKey abstraction
provides many things related to, well, product keys.

 |  Next  |  Last
Pages: 1 2 3 4 5 6 7 8 9 10 11
Prev: Putting passwords in a properties file?
Next: Interview