Oct 16 2005

Violating Encapsulation

admin @ 2:03 pm

One of the core tenets of object-oriented programming is encapsulation. It’s one of OO’s most powerful ideas. More powerful than inheritance. Unfortunately it’s also one of the most ignored.

In “OO in One Sentence: Keep It DRY, Shy, and Tell The Other Guy” Andy Hunt & Dave Thomas (The Pragmatic Programmers) talk about good OO code being shy:

“The best code is very shy. Like a four-year old hiding behind a mother’s skirt, code shouldn’t reveal too much of itself and shouldn’t be too nosy into others affairs.

But you might find that your shy code grows up too fast, shedding its demure shyness in favor of wild promiscuity. When code isn’t shy, you’ll get unwanted coupling.”

Something I see all the time, on every team I’ve been involved with, is code like the following (classes are generalized from examples):

MyThing[] things = thingManager.getThingList();
for (int i = 0; i < things.length; i++) {
  MyThing thing = things[i];
  if (thing.getName().equals(thingName)) {
    return thingManager.delete(thing);
  }
}

This code is tightly coupled to the implementation of myThing in that it gets the name property, knows that it’s a string, etc. This is a classic approach from the old days or procedural programming. It is NOT OO.

How about:

MyThing[] things = thingManager.getThingList();
for (int i = 0; i < things.length; i++) {
  MyThing thing = things[i];
  if (thing.isNamed(thingName)) {
    return thingManager.delete(thing);
  }
}

This code still knows the details of myThing. Why should it? All it should know is how to ask a thing manager to delete a thing given its name:

return thingManager.deleteThingNamed(thingName);

thingManager is closer to MyThing, in fact they’re likely packaged together, so it’s not as bad for it to have somewhat more intimate knowledge of MyThing.

This is just one example of the approach of treating objects like new age data structures: Ask an object for information about its state, process that, make decisions based on that, then do something to/with the object.

In the above example, the code

  • asks the ThingManager for its list of Things
  • iterates through those Things
  • asks each one for its name
  • checks if the name is what it is looking for
  • if so it asks the ThingManager to delete that thing

The proper approach is to ask the thingManager “Delete a thing named this if you have one. Let me know if you were able to do that.”

As a side note, writing code this way makes it much more understandable.

Do you write code like the first snippet? If so, you’re not doing OO! Stop telling people that you are. Or better, start doing OO. Better still, get your whole team doing OO.

Tags: 

Oct 11 2005

A new member of the family

admin @ 8:11 pm

So.. I was in an Apple Store near my client in the Detroit area last night to get a short, white firewire cable for my external travel drive (why it came with a long, thick, black cable is beyond me).

Well, I had seen the cable there before so I just got it off the rack and got into line to pay for it.

Now, it strikes me that supermarket designers must had a bolt of lightning style epiphany when they came up with the idea of putting the candy racks right at the tills.

And Apple is not one to pass by a good idea.

So… back to last night… I’m standing there in line with my short, white firewire cable.. waiting my turn. It’s a nice looking place so I glance around at the shelves, the latest iPod gadgets, the other ubercool people buying mac stuff.

Oh.. what’s this? A table next to the till with “stuff” on it. iPods and such… marked down… ah-ha… they have a clearance table.. demos & refurbs… nice discounts too.

Oh. Oh my. Oh dear. What’s that underneath the table? Oh. I feel the sweat start.

It’s a pile of powerbook boxes. 12″ powerbooks.. bah.. nothing interesting.. my 14″ iBook will go head to head with that. 17″ powerbooks.. gangly monsters… laptops on steroids.. abominations… nothing interesting there. iBooks.. got that covered already.

But what’s that? That in between sized black box? Could it be? No.. it couldn’t.. but alas, it is.

A 15″ PowerBook. hmm.. it can’t hurt to just look at it…

It’s nicely discounted. But still, it’s probably an entry level.. with a wee hard disk, and a combo drive. Nope… it’s top of the line… 1.67GHz, 80G, SuperDrive, Bluetooth 2.0, wifi, gigbit ethernet.. blah blah blah. 512M installed.

“You want to have a closer look at that unit?” GULP! “Um.. ya.. sure… it prob doesn’t have tiger installed”.. turn it on.. it dives into the perky tiger installer.

“And it’ll take 2 1G sticks?” “No I it has 512M soldered in and room for 1 stick”.. oh.. I’m off the hook.. but yet.. “Can you verify that….” “My mistake.. the 12″ has 512 soldered in.. this one takes 2 sticks.. there’s a 512M stick in it”. Damn.

Feebly.. “it has a superdrive?” “Yes”.. ok I give up. I resign myself to fate, destiny, the will of the gods… and hand over my credit card.

So now I have in front of me a shiny, almost new, 15″, fully loaded, Aluminum PowerBook :)

And a short, white firewire cable.

Tags: