billroper: (Default)
[personal profile] billroper
At some point late this morning, I came to the horrible realization that the absence of the "const" keyword in Java, coupled with the absence of operator overloading, meant that all of the code that I was porting across from C++ had an ugly, ugly hole in it.

I have ID classes that I use to locate several types of objects in the C++ code. In C++, I would write code like:


const CPdID& id = pPd->GetID();
PdID modifiableID = pPd->GetID();


The first case wouldn't make a copy of the const reference that GetID() returns and I wouldn't be allowed to change it, while the second line would assign the contents of the const reference to my new PdID, since I'd overloaded operator=. And all was right with the world.

Except when I converted that to Java, I ended up with:


PdID id = pPd.GetID();
PdID modifiableID = pPd.GetID();


Now in this case, I have taken the reference that pPd.GetID() has returned that I did not want to modify and am happily going to modify it, because I didn't -- and couldn't -- define the function return as const. Ick.

So I have now made PdID immutable by removing all of the mutators that belong to the class. And I've added a MutablePdID which extends PdID and contains all of the mutators that are no longer in PdID. Add toMutable() and toImmutable() methods to each class that will create new objects as required, and my IDs Formerly Known As Const are now safe from the ravages of clueless coding.

The result looks like:


PdID id = pPd.GetID();
MutablePdID modifiableID = new MutablePdID( pPd.GetID());
// The case below is not preferred (by me), because if GetID() returned a MutablePdID,
// I could change the returned object without intending to. Oops.
//MutablePdID modifiableID = pPd.GetID().toMutable();


This was an exciting way to spend a day of work...

Date: 2014-01-31 02:51 am (UTC)
mdlbear: blue fractal bear with text "since 2002" (Default)
From: [personal profile] mdlbear
The usual pattern in Java is to have immutable objects (using "final" on the fields) that implement clone() when copies are required.

Take a look at Lombok.org, which makes this sort of thing totally painless.

Date: 2014-02-02 05:38 am (UTC)
mdlbear: blue fractal bear with text "since 2002" (Default)
From: [personal profile] mdlbear
Oops. try http://projectlombok.org/features/

Their package names are all org.lombok; that's what threw me off.

There's a @Value annotation that creates an immutable object with an all-args constructor and equals, hashCode, and toString methods.
Edited Date: 2014-02-02 05:48 am (UTC)

Date: 2014-02-02 05:47 am (UTC)
mdlbear: blue fractal bear with text "since 2002" (Default)
From: [personal profile] mdlbear
Immutable objects have many advantages; i.e. you can put them in hash tables, share them in lists, and so on. The question is why one would ever want a mutable one. Especially with something like an ID. Normally one just constructs a new object when necessary.

(You can also store them in a hash table and make a factory method that guarantees that instances are unique, in the sense that a.equals(b) iff a == b. This can be used to speed up tests.)

Note that Java doesn't have C++'s weird syntactic distinction between references and pointers. All objects are represented by pointers, but one can have "final" variables that can't be modified.

Date: 2014-02-02 07:29 pm (UTC)
mdlbear: blue fractal bear with text "since 2002" (Default)
From: [personal profile] mdlbear
My inclination would be to define arithmetic operations on IDs that return a new, immutable, ID. That's the way String operations work in Java, for example. That way you never have to worry about whether you've converted something back to immutable before you store it.

Date: 2014-02-02 07:30 pm (UTC)
mdlbear: blue fractal bear with text "since 2002" (Default)
From: [personal profile] mdlbear
All I can tell you about that is that we're using them at Amazon, and they're *extremely* picky.

Profile

billroper: (Default)
billroper

January 2026

S M T W T F S
     1 2 3
4 5 6 7 8910
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 3031

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Feb. 1st, 2026 06:07 pm
Powered by Dreamwidth Studios