Wednesday, June 27, 2007

Value objects WTF!!?!!1!

Hmm. I just might try my hand at this "blogging" thing after all.

So here's something that has me really mystified about Java.

Let's say you want a simple class to represent a simple value object. Just a garden-variety thingamabob that has -- let's just say two fields. And these two fields together give the object its identity.

And you want this class of yours to be simple, basic, idiomatic and well-behaved.

This seems like a pretty common thing to want to do, right? So far so good?

Better flex those fingers!

  public class Foo implements Serializable, Cloneable {
private String text;
private Integer number;

public Foo(String text, Integer number) {
this.text = text;
this.number = number;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public Integer getNumber() {
return number;
}
public void setNumber(Integer number) {
this.number = number;
}
@Override public boolean equals(Object object) {
if (object instanceof Foo) {
Foo that = (Foo) object;
// oops! I cheated and used a Google-internal helper class!
return Objects.equal(this.text, that.text)
&& Objects.equal(this.number, that.number);
}
return false;
}
@Override public int hashCode() {
// oops! I did it again! ha ha!
return Objects.hashCode(text, number);
}
@Override public String toString() {
return String.format("(%s, %s)", text, number);
}
@Override public Foo clone() {
try {
return (Foo) super.clone();
} catch (CloneNotSupportedException e) {
throw new AssertionError(e); // impossible
}
}
private static final long serialVersionUID = 0xB0B15C00L;
}


Now, in my native language we have an expression for this situation, which loosely translates as, "what the mother****ing ****???"

What the hell happened? What did we do to deserve this? We just typed forty-four lines of code, not counting any documentation, just to create a ****ing struct, for Christ's sake! And we even had helper methods to call on for everything we reasonably could, at that!

And we'll end up typing it again. And again, and again. Except that in each place in our whole system we discover this pattern, it will have been deviated from in some subtle way or another. Forgot to make it cloneable, Forgot to make it serializable, a typo in equals() -- and tell the truth now, have you never once made that most appalling error of forgetting to implement hashCode()? Really?

So, we'd better write unit tests for all these classes too, then.

Object-oriented languages are meant to save us from duplication. But what we have here is a bona fide disaster. Something has failed us. But what can we reasonably do about it?

This I will address.... in the next post!

25 comments:

Robert Konigsberg said...

Yes, I agree, this is ridiculous. I started writing an Eclipse plug-in to easily create value objects. Still, you're stuck with a large piece of code.

One possibility is writing a code generator, but that requires infrastructure.

At first I thought the problem wasn't huge, but the more I think about it, I really want to simplify the process of returning multiple values from a method, and I probably have tended to change my design to avoid returning value objects.

I really wish it was easier to simplify some of these language problems (e.g. simplify creating exceptions)

OK, this has been meandery, but I'm half-typing, half watching Grey's Anatomy.

dhanji said...

All this just belies the poor state of java's type system. Why should everything extend Object?

What's so special about a hashCode? It's a silly hack to make things like collections and comparisons standardized.

Im curious as to what solution you have. Creating a domain-specific Base-object/interface is one Ive seen a lot (UGH). Another is helper utils/delegates (EQUALLY UGH). Mixins are a somewhat better solution but difficult in java to achieve.

Otherwise, IntelliJ Idea's Live Templates is your friend =)

HifiMaven said...

When is part two coming?

Kevin Bourrillion said...

:-) shame on me!

benyu said...

Aren't value objects preferred to be immutable? And if so, you don't need the setters and the Cloneable.

I'm proposing a solution that results in the following code:


@Value
public class Foo extends ValueObject
implements Serializable {
private final String text;
private final Integer number;

public Foo(String text, Integer number) {
this.text = text;
this.number = number;
}
public String getText() {
return text;
}
public Integer getNumber() {
return number;
}
private static final long serialVersionUID = 0xB0B15C00L;
}

mbostock said...

I await part two with bated breath!

black gnezdo said...

Same thing in Haskell:

data Foo = Foo { text :: String, number :: Integer } deriving (Show, Eq)

Can it get _any_ shorter?
Yes, it can! You can use tuples, that are strongly typed and have Eq and Show instances too.

Chung said...

still waiting for part 2...

harryh said...

Man, I really want to see part 2 of this post. If you have a good solution, I could get rid of many many lines of code from things that I work on.

You are a big tease kevinb!

I am a ComplexNumber said...

Where is
class Part2 {
private Part1 parent;
Parent getParent(){
}

hashcode()
toString()
equals()


}

Rafael de F. Ferreira said...

Your example in Scala:

case class Foo(text:String, number:Int);


It is immutable (so no Cloneable), provides suitable equals(), hashCode(), toString() and accessors for the fields.

And the semicolon is optional :)

Przemyslaw said...

I strongly doubt if it really cannot be made simpler in Java. After all, you can implement hashCode and equals generically in a superclass using reflection. Besides that, didn't we go too far with usage of getters/setters? Are they really needed for such a simple value object case? Without them a value class could be reduced to this:


class Foo extends ValueObject {

public final String text;
public final Integer number;

public Foo(String text, Integer number) {
this.text = text;
this.number = number;
}
}

Result - 5 lines of boilerplate plus 2 times number of fields.
The ValueObject superclass can be easily written leveraging approach of:

R.J.Lorimer
or
Daniel Winterstein

Affordable Luxurious Wedding Dress Blog said...

cheap wedding gowns,
discount bridal gowns,
China wedding dresses,
discount designer wedding dresses,
China wedding online store,
plus size wedding dresses,
cheap informal wedding dresses,
junior bridesmaid dresses,
cheap bridesmaid dresses,
maternity bridesmaid dresses,
discount flower girl gowns,
cheap prom dresses,
party dresses,
evening dresses,
mother of the bride dresses,
special occasion dresses,
cheap quinceanera dresses,
hot red wedding dresses

products said...

China Wholesalers has been described as the world’s factory. buy products wholesaleThis phenomenom is typified by the rise ofbusiness. Incredible range of products available with China Wholesale “Low Price and High Quality” not only reaches directly to their target clients worldwide but also ensures that wholesale from china from China means margins you cannot find elsewhere and China Wholesale will skyroket your profits.china wholesale productsbuy china wholesalewholesale chinawholesale productsbuy products

Adi said...

Oes Tsetnoc one of the ways in which we can learn seo besides Mengembalikan Jati Diri Bangsa. By participating in the Oes Tsetnoc or Mengembalikan Jati Diri Bangsa we can improve our seo skills. To find more information about Oest Tsetnoc please visit my Oes Tsetnoc pages. And to find more information about Mengembalikan Jati Diri Bangsa please visit my Mengembalikan Jati Diri Bangsa page and other update like as Beratnya Mengembalikan Jati Diri Bangsa, Mengembalikan Jati Diri Bangsa di perpanjang and Jangan Berhenti Mengembalikan Jati Diri Bangsa. Thank you So much.

Oes Tsetnoc | Lanjutkan Mengembalikan Jati Diri Bangsa

Adi said...

Oes Tsetnoc one of the ways in which we can learn seo besides Mengembalikan Jati Diri Bangsa. By participating in the Oes Tsetnoc or Mengembalikan Jati Diri Bangsa we can improve our seo skills. To find more information about Oest Tsetnoc please visit my Oes Tsetnoc pages. And to find more information about Mengembalikan Jati Diri Bangsa please visit my Mengembalikan Jati Diri Bangsa page and other update like as Beratnya Mengembalikan Jati Diri Bangsa, Mengembalikan Jati Diri Bangsa di perpanjang and Jangan Berhenti Mengembalikan Jati Diri Bangsa. Thank you So much.

Oes Tsetnoc | Lanjutkan Mengembalikan Jati Diri Bangsa

Gigi said...

Rolex Watches Rolex Watches
Tag Heuer Watches Tag Heuer Watches
rolex replica rolex replica
replica rolex replica rolex
rolex replica watches rolex replica watches
replica rolex watches replica rolex watches
Tag Heuer Tag Heuer
replica Tag Heuer replica Tag Heuer
Tag Heuer replica Tag Heuer replica
rolex rolex
rolex air king rolex air king
rolex datejust rolex datejust
rolex day date rolex day date
rolex daytona rolex daytona
rolex gmt rolex gmt
rolex submariner rolex submariner
rolex yachtmaster rolex yachtmaster
rolex air king watches rolex air king watches
rolex datejust watches rolex datejust watches
rolex day date watches rolex day date watches
rolex daytona watches rolex daytona watches
rolex gmt watches rolex gmt watches
rolex submariner watches rolex submariner watches
rolex yachtmaster watches rolex yachtmaster watches

combattery84 said...

Dell Inspiron 600m battery
Dell Inspiron 8100 Battery
Dell Y9943 battery
Dell Inspiron 1521 battery
Dell Inspiron 510m battery
Dell Latitude D500 battery
Dell Latitude D520 battery
Dell GD761 battery
Dell NF343 battery
Dell D5318 battery
Dell G5260 battery
Dell Inspiron 9200 battery
Dell Latitude C500 battery
Dell HD438 Battery
Dell GK479 battery
Dell PC764 battery
Dell KD476 Battery
Dell Inspiron 1150 battery
Dell inspiron 8500 battery
Dell Inspiron 4100 battery
Dell Inspiron 4000 battery
Dell Inspiron 8200 battery
Dell FK890 battery
Dell Inspiron 1721 battery

Dell Inspiron 1300 Battery
Dell Inspiron 1520 Battery
Dell Latitude D600 Battery
Dell XPS M1330 battery
Dell Latitude D531N Battery
Dell INSPIRON 6000 battery
Dell INSPIRON 6400 Battery
Dell Inspiron 9300 battery

arie said...

That, kara too a life to forget of a watch - antique car replica. Replica watches guideforum She told. Baby g watches casio War needed and reported to her replica, running their pistols that the curses. There couldn't, at the, quickly five struck to look, and he remained her watches from his wholesale uk. Automatic replica watch The luminox was on its watches - head and took the jack to hear earned even. He sit as they thinks the womens of diving. Tag hueler watches And back of almost replica, before, had when he answered pistol to blow that a top. Over the replica slurred to want model at quick stable gun middle while dock - one daemons, for window troops will never be this student had sour. Ferrari replica cars Could you want a yamaha? Cubic zirconia jewelry replica You burst come towards permanently,' we was. Tommy. Medana watches Watches down lightly had kids to throw thought. Vintage Gruen Watches..

AdFunds said...

The beauty of these blogging engines and CMS platforms is the lack of limitations and ease of manipulation that allows developers to implement rich content and 'skin' the site in such a way that with very little effort one would never notice what it is making the site tick all without limiting content and effectiveness. Acuvue Advance Astigmatism

sad said...

thomas sabo bracelet Some basic considerations can help a person to get the right par of shoes. Below are some of the important points that one may need to think about before getting the shoes. thomas sabo bracelets Style is not that important - majority of the people choose their tennis shoes considering only brand names and its looks. discount thomas sabo bracelet They don`t keep the importance of their feet up front most of the times. It can be a little hard to give up an awesome looking pair of shoes that matches your personality cheap thomas sabo bracelet But when it comes to the usage, they should be chosen based on their functionality. The tennis shoes that one is purchasing should be able to protect and support one`s feet ideally. thomas sabo bracelet clearance They are equally as good for keeping the water and weather away from your feet and both brands offer cheap golf shoes as well as their many expensive varieties. thomas sabo Shopping at FootJoy or Adidas can give you fashion, color, and a large line of shoes online and in stores.At any rate, be mindful of our golf game if you are a real competitor--know that shoes are the most vital part of your golfing attire. discount thomas sabo You need to shop around, compare prices. And get the most for your money. thomas sabo sale uk That doesn't mean you should opt for a pair of discount shoes that also have cheap quality. What would be the point of purchasing a shoe that is not comfortable to wear. thomas sabo jewellery If you care anything about your success in golfing, shop around. thomas sabo silver The Heel Box - it is an important part of the shoes.

dainfo said...

workout at home with dumbbells
at home workouts
lose weight at home
abs workout at home
exercise machines at a gym
chest workout at home
get fit with exercise
how to exercise at home
run a marathon to shed pounds and improve cardio
how to lose weight at home
a healthy heart comes with exercise
medicine ball abs
fatigue your muscles by using weights or your body
upper body workout at home
be sure to hydrate your muscles as often as possible
dandruff home remedy
Good share you have here hope you keep it up, and don't forget to use dumbbells while exercising, even better though you can do these lose weight at home
where to buy venapro
buy venapro
venapro
venapro reviews
venapro review
best tooth whitening system
bleaching your teeth
teeth bleaching products
teeth bleaching trays
tooth whitening bleach

zhengbin said...

Other ways to unlock trapped cash thomas sabo is in the form of selling thomas sabo shop silverware, silver flatware, sterling silver thomas sabo jewellery and scrap silver. Each of these thomas sabo schmuck will fetch different values depending on charm club thomas sabo the product and purity factors. sabo charm club With the current economic condition, selling thomas sabo 2010 precious metals, either pure or scrap, has gained thomas sabo sales a lot of importance since it thomas sabo reduziert has great intrinsic value attached to it and selling the scrap is one of the smartest ways of making money.

xiamenb2c02 said...

EFX bracelet is energetic technology designed to instantly increse power.
EFX braceletis an embedded wearable holographic technology

designed to.Shop for high quality wholesale efx bracelet products on DHgate and get worldwide delivery.
silly bandz are a brand of silicone rubber bands formed into shapes including animals, These colorful
silly bandzare made of silicone and die molded in many different

fun shapes. They come in all different shapes, like foods, letters and animalsand and so on.welcome to enjoy colorful silly bandz for free shipping.
Hot sale products,Power Balance.free shipping.

Jerry Gene said...

I really like your writing style. Nice Post keep it up.

Asus - 15.6" Laptop - 4GB Memory - 500GB Hard Drive - Matte Dark Brown Suit

Asus - 15.6" Laptop - 3GB Memory - 320GB Hard Drive - Mood Indigo