Blog Stats
  • Posts - 3
  • Articles - 1
  • Comments - 4
  • Trackbacks - 2

 

What's in a name (Impl classes)

I'm just in a mood today.

A friend and I were discussing dependancy injection and I got off the subject talking about Impl classes. If you are not familiar with what I'm talking about some typical examples would be:

ProcessImpl
BusinessImpl
FactoryImpl
ImplementationImpl

And it goes on like this.

If you've ready any book on software development and object oriented programming and didn't just get out of college with your CS degree and are on your first job, then you should know two things. One that a class is representing an object, and two that code (things like class names) should be self describing (this is one of the golden rules). So let's look at an example:

BirdImpl

By looking at the name, this object is suppose to represent a bird implementation - say a Robin, or a Blue Jay, or a Cardinal.  Just looking at this alone, how could this be more self describing?  How about instead of calling it BirdImpl we call it Robin, BlueJay or Cardinal; novel idea I know.

What I'm saying is it's either a Bird or it is not a Bird; putting Impl on it doesn't add any extra value, but let's play devils advocate. Let's say the reason I named the object BirdImpl is because it a generic (abstract) class that can represent any type of bird, so if I use it in a factory, in my code I know that this can be any type of bird:

BirdImpl bird = BirdFactory.Create("Robin");
//or:
BirdImpl bird = BirdFactory.Create("Stork");

Well to that I would say have your generic (abstract) class called Bird instead of BirdImpl. To drive home my point, I drive a Car, to be specific a Saturn, to be more specific a SaturnL200; I DO NOT drive a CarImpl (Saturn), and I DO NOT specifically drive a SaturnImpl (SaturnL200)...if I want to speak to someone about my Saturn in GENERIC terms I say things like 'My Car is painted black', NOT 'My CarImpl is painted black'. If I want to go for a drive and I had to write this in code I might say:

Car _car = CarFactory.Create("Saturn");
Brian.Drive(_car);
//Or:
//Saturn _saturn = CarFactory.Create("SaturnL200");
//Brian.Drive(_saturn);

I would not say:

CarImpl _car = CarFactory.Create("Saturn")
Brian.Drive(_car);
//Or:
//SaturnImpl _saturn = CarFactory.Create("SaturnL200");
//Brian.Drive(_saturn);

I hope I have made my point in naming conventions and self describing code.

You may also say I could of named the variable carImpl or saturnImpl for the variable, but this would be redudant - or stating the obvious:

Car carImpl = CarFactory.Get("Saturn");

You mean carImpl is an implementation of an instance of Car???? Really??? No joke??? I thought carImpl was an instance of a Tree object. Like I said, redudant - if you write self describing code at least..

Some may say the reason use 'impl' is because it's an implementation of an interface called Car, so something that implements this interface is a CarImpl.  But this again is confusing an not self describing.  How about naming the interface ICar rather than Car, of if you're working with something more specific such as a Saturn object that implement ICar you can continue to just call it a Saturn.

ICar _car = CarFactory.Get("Saturn");

Any questions?


Feedback

# re: What's in a name (Impl classes)

Gravatar I agree with you mostly regarding naming conventsion. The "Impl" moniker seems...silly. If I am going to use an interface then I will create that interface as "IThing" and the implementation will be "Thing".

Also, I'd like to say that I think Stroustroup is taken a little too seriously in regards to his statement that "we should all be programming against interfaces". Oh don't get me wrong...I'm all for using interfaces. But, as with everything else, the best solution is almost NEVER binary (all or none).

As a final note, I think there is a general problem with the way in which OO systems are considered. We have been told from day one that OO systems represent the "real world". When, in the "real" world was the last time you saw a "FileParser" walking around? (just kidding)

The problem here is that the "real" world context breaks down in software. However, for some reason, developers attempt to "force" a real world implementation on their software systems. I shall describe with my de facto example.

Consider magazines. If you model magazines in software you might have a Magazine class (that inherits from or implements a "Periodical" class or interface). Now consider that a grouping of these magazines might be a MagazineCollection. How might one sort the collection?

In the VB 6 world, there would be a "Sort()" method on the MagazineCollection class. And I'm sure many of you reading this would agree. However, this does NOT model the real world. You can easily prove it to yourself by commanding that stack of magazines on your desk (dare I call them a MagazineCollection?) to sort themselves. Go ahead, give it a try. You can do that all day and no amount of quantum fluctuations will make it so.

So how does the "real world" MagazineCollection get sorted? Well what sorts them out here? We do. And in what capacity? As a magazine sorter. So, to model the "real world" we need a MagazineSorter class.

All in all, Brian, I think that the naming convention problem is partially (and maybe mostly) because of the problems related to our (flawed) ways of thinking about modeling systems based the "real" world...whatever that means. 2/20/2007 3:16 PM | Codesailor

# re: What's in a name (Impl classes)

Gravatar You bring up good points, but the de facto example doesn't always apply. Sometimes objects have to be 'smart' and know how to take care of themselves and this is where the line blurs. CSLA does a great job of providing this kind of model.

The problem with the scenerio that you've described is the the the object is really just a DTO (data transfer object) - it's stupid. If a developer doesn't know to call a method on the manager to validate it, you may end up with invalid data in your database. Sure you can combat this by building business rules into your database but now your duplicating your efforts; one in your Manager and one in your SP's...multiply this if you want to validate in your GUI too. I can dig deeper into that thread if anyone's interested.

Now imagine an object that is smart and knows how to persit itself, knows how to validate itself, and prevents the object from going into the data store without being valid. Everything about the object, it's data, it's validation rules, it's DAL logic are all encapsulated in one object. This is the advantage of CSLA.

Now it's no silver bullet and certainly not the right tool for every job, but with built in remoting, N-level undo, built in validation, and the list goes on, it's a powerful option to consider when designing a business application. 2/20/2007 8:18 PM | Brian

# usedcarauction

Gravatar In searching for sites related to web hosting and specifically comparison hosting linux plan web, your site came up. :) 9/15/2009 4:37 AM | autoauctions

# Great News

Gravatar You made some good points there. I did a search on the topic and found most people will agree with your blog. 10/11/2009 9:36 PM | Reebradub

Post a comment





 

 

 

Copyright © Brian Johnston