billroper: (Default)
[personal profile] billroper
So I'm still working on porting my code from C++ to Java. I'm over the 40,000 line mark now, which is good, but there is still a lot of stuff left to port. I've been working through the Var class for several days and am nearing the 6000 line mark there. (It's a large and important class with many methods.) And as I work down through it, I run into many other classes that aren't completed where I either go "I'll fix that later" or "I should go fix that now".

So I needed to copy an object that held an array of other complex objects that held still more complex objects along with a metric ton of ordinary data. And I was about to swear and write yet another copy constructor (because I am a recalcitrant C++ programmer apparently) when I suddenly realized that I was about to do way too much work.

If I implement the Cloneable interface and the clone() method on these objects, then I get a free bitwise copy of everything that they contain. So for that object with a metric ton of ordinary data, I just call super.clone() and I have now copied the metric ton of ordinary data and only need to fix up and clone the complex objects that I want to have new copies of, since my intent is to do a deep copy when calling clone().

So I ran around and implemented all of the necessary clone functions for half a dozen classes.

And the code base got 47 lines longer. Instead of a whole lot more lines of mechanical copying which would have pumped up the line count, but not actually done any more work.

Oh.

OK. I guess I'm not yet too old to learn some new tricks. :)

Date: 2014-01-09 07:20 pm (UTC)
ext_63737: Posing at Zeusaphone concert, 2008 (That's It boater)
From: [identity profile] beamjockey.livejournal.com
If I implement the Cloneable interface and the clone() method on these objects

Just to be clear: This is one of your programming blog entries, and not one of your parenting blog entries, am I correct?

Date: 2014-01-09 08:42 pm (UTC)
From: [identity profile] rono-60103.livejournal.com
This can be a strength of Object Oriented languages with good libraries. On the other hand, there can be some danger since you often don't really know what lurks in the library.

At least with Java there is some expectation that the libraries will be the same. I hate to think how risky that could be with C++ code targeted at multiple target platforms with multiple (dynamic) libraries that claim to be the same.

Date: 2014-01-09 09:08 pm (UTC)
madfilkentist: My cat Florestan (gray shorthair) (hex)
From: [personal profile] madfilkentist
Keep in mind that the Cloneable interface and clone method specify practically nothing beyond saying the clone is "the same" in some sense but not == to the original, so you'll need to enforce your own consistency rules. Java cloning is powerful but dangerous.

We all recall how much debugging was required after someone implemented MilesVorkosigan.clone().

Profile

billroper: (Default)
billroper

January 2026

S M T W T F S
     1 2 3
4 5 6 7 8910
11121314151617
18192021222324
25262728293031

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jan. 8th, 2026 06:58 pm
Powered by Dreamwidth Studios