TechRepublic : A ZDNet Tech Community

Programming and Development

Host: Justin James
Contact

Object-oriented programming (OOP) has become the dominant type of programming over the last 10 years or so; a major driver of this has been Java, VB.NET, and C#. But what I keep seeing more and more is that too many programmers using object-oriented languages really do not understand OOP.

A huge misunderstanding is the concept of encapsulation. Encapsulation is meant to hide the underlying implementation of a concept from the code that makes use of the object — not to expose it. The idea is that the consuming code does not need to know or care about the particular implementation “under the hood,” so it can be changed without affecting anything else. Encapsulation works hand-in-hand with polymorphism.

Between encapsulation and polymorphism, code can (and should) be extremely loosely coupled. But really it isn’t in many pieces of code that I have seen. Far too many objects end up simply being equivalent to a Pascal “type” structure, a strongly typed collection of other types, gathered in one spot for easy reference. Another fairly common misuse of OOP is to write “classes” that play out like hashtables of hashtables of hashtables. While that type of programming made sense in Perl for a variety of reasons (not the least of which is the lack of a comprehensible object system in Perl), it is not in the spirit of OOP.

Why are these techniques a problem? For one thing, they require too much knowledge by the consumer of the underlying functionality. The first example leads to these statements that look like this:

FooObject.Property.Item(Index).Method(Parameter 1, Parameter2).Property = BarObject.Method(Parameter 1).Property.ToKludgeObjectType()

If the consumer has no idea what any of those sub-properties, methods, and so on do, they cannot get too far using either of these classes. On top of that, by exposing so many of the internals to the consumer in what will need to be a full-access manner, the encapsulating object hands over full control. This can cause some real damage, particularly if those contained classes have properties or methods with negative consequences.

The second type that I describe typically looks something like this:

FooObject("Property").Item(Index).Method(Parameter 1, Parameter 2)("Property") = CType(BarObject.Method(Parameter 1).Item("Property"), KludgeObjectType)

Wow… what an utter disaster. Can you imagine working with this? The worst part is that, with all of those collection lookups, you can throw compile time checking out of the window. The person who writes this manages to combine a very dangerous aspect of dynamic languages (lack of compile time checking) with the worst of compiled languages (design time knowledge of types) into something that derives none of the benefits of either type of language. One “fat finger mistake” in all of those lookup values will compile just fine, but come run time, they will become errors of one sort or another. It would not be so bad if a simple mistake just lead to an outright exception like “Index out of range,” but if the mistake just happens to be purely logical (e.g., the item named does exist, but the wrong item was named), there is a solid chance that the code will keep running but with silent and hard-to-detect corruption of data and/or logic.

I know part of the problem here is simple laziness. I am guilty of it plenty of times. For me, what usually happens is that I am using an OO language for something that I really just need a procedural or mostly procedural language for. Let’s face it, not every application requires too much OO design — they really just need a way of quickly passing a bunch of loosely related data around. But these “shuttle classes” always seem to take on a life of their own, and the next thing you know, you are writing static/shared classes with static/shared members to process the data within the “shuttle classes.” Why? Because you really meant to be using Perl, Pascal, or maybe even Smalltalk or a simple shell script, not C++, Java, C#, or VB.NET (at least for this one part of the project, that is).

Folks, this is where the rubber stops meeting the road. Those earliest experiences with programming will always color our current thought process regarding programming. And nearly universally, we were introduced to programming with a procedural language or a very procedural use of a non-procedural language. Add to that the fact that contrary to popular belief, Java, VB.NET, and C# are not purely noun-oriented OO. After all, they have some primitive verbs in there. What we really just want to do sometimes is to extend the language itself (add new primitive verbs) –that is where those anti-OO static classes of static methods come from, and it is the only way we can really add a new verb to the language. Those pseudo-procedural-functions end up needing a ton of parameters passed to them (just like procedural code), since the data that they are working on is no longer included in the class that they are members of.

At the end of the day, this type of programming is indicative of one or more of the following things:

  • The programmer was “born and bred” in a non-OO environment and is untrained in proper OO.
  • The programmer does not feel like using the proper OO techniques for whatever reasons.
  • The language being used is the wrong language for the job.

I think the last item is a lot more common than most of us care to admit. After all, we turned to OO to save us a lot of headaches, increase code reuse, etc. But none of the OO promises can be fulfilled when we treat an OO language like it is something that it is not. And if we are going to program away the benefits of OO, why bother using the languages?

I guess this is yet another reason why I am fairly pro on the .NET CLR (and would be on the JVM if Sun was more supportive of other languages). It is possible (in theory) to write the right code in the right languages. Sadly, the only two languages with massive support in .NET right now are the nearly identical twins, VB.NET and C#. It looks like the first language to get formal “kid brother” status is the functional language F#. IronRuby and IronPython are still “CodePlex cousins,” in which Microsoft might donate some time, server space, and documentation but little real support. Ruby is also attractive to me because it seems to blend procedural and OO code pretty well. I really, really need to try Ruby out.

The takeaway here is that you really need to get off the couch, learn proper OO, and use it correctly. Otherwise, you are hurting your own efficiency, writing slow code (all of those collection lookups and OO tree traversals are rather expensive!), and making your code difficult to maintain. If you still cannot figure out how to write what you need in an OO-friendly manner, you may need to think about switching languages.

J.Ja

Justin JamesJustin James is an employee of Levit & James, Inc. in a multidisciplinary role that combines programming, network management, and systems administration. He has been blogging at TechRepublic since 2005. Read his full bio and profile.

Print/View all Posts Comments on this blog

examples jayaramtp@... | 11/19/07
Please show me Justin James | 11/19/07
I Think what he is saying is : j-mart@... | 11/20/07
Agree Mark Miller | 11/20/07
That's what I get... Justin James | 11/20/07
Besides... Justin James | 11/20/07
You did a good job Bad Boys Drive Audi | 11/26/07
Code Complete 2 TJ111 | 11/26/07
Yes, indeed! Justin James | 11/26/07
For some other really good books about OOP jslarochelle | 11/26/07
Pro Spring and IOP Saurondor | 12/01/07
Linking ugly code to encapsulation destrehandave@... | 11/26/07
You seem to be making your own mistake there Tony Hopkinson | 11/26/07
Overuse or improper use? destrehandave@... | 11/28/07
I wrote my first piece of code in 1977 Tony Hopkinson | 11/28/07
And I thought I was old. ;-) jslarochelle | 11/28/07
One of things I got taught was to describe data Tony Hopkinson | 11/29/07
"I" confuse the issues? destrehandave@... | 11/30/07
To be fair about it? Tony Hopkinson | 11/30/07
OOP In Name Only ryanvs@... | 11/27/07
I blame my parents Tony Hopkinson | 11/20/07
Proper OOP training is important jslarochelle | 11/21/07
I never heard that Justin James | 11/21/07
jython eldergabriel@... | 11/26/07
Training? CodeCurmudgeon | 11/26/07
This is a common problem jslarochelle | 11/26/07
Colleges myluv2z@... | 11/27/07
OO before procedural, if that's even necessary ? Tony Hopkinson | 11/27/07
OO 99% Procedural myluv2z@... | 11/27/07
The original examples are what happens.. $dunk$ | 11/27/07
OO and procedural alaniane@... | 11/27/07
It certainly could be presented differently Tony Hopkinson | 11/28/07
Actually I think the "institution" is right about the order. jslarochelle | 11/27/07
Procedural & OO mix Justin James | 11/28/07
First language? Assembler SnoopDougEDoug | 11/28/07
Well, well Tony Hopkinson | 11/28/07
Joel's test Justin James | 11/29/07
Hire for attitude, train for skills Tony Hopkinson | 11/30/07
Rethink the relevance of your questions? $dunk$ | 11/30/07
Dunk Tony Hopkinson | 11/30/07
Equality & equivalency Justin James | 12/01/07
Re: Equality & Equivalency $dunk$ | 12/01/07
That was my experience as well Bad Boys Drive Audi | 12/02/07
Training? Justin James | 11/27/07
You are right. If managers could understand the value of training jslarochelle | 12/02/07
RE: Stop the systematic abuse of object-oriented programming mangagoras@... | 11/21/07
THE jvm Tony Hopkinson | 11/22/07
Of interest for those looking for a different paradigm jean-simon.s.larochelle@... | 11/22/07
Quality and quantity Justin James | 11/23/07
Justin for a quick overview check out the following jslarochelle | 11/24/07
Language? H*ll, what about debugging? SnoopDougEDoug | 11/28/07
VS2005? Justin James | 11/29/07
You can amend and continue in C# as well in 2005 Tony Hopkinson | 11/30/07
OO Nirvana comes at a price pccoder28@... | 11/26/07
Number 1 OO misuse that I see is much simpler... jreddy@... | 11/26/07
Yes correctly assigning responsibility to classes is difficult jean-simon.s.larochelle@... | 11/26/07
For OO code to work alaniane@... | 11/26/07
It's much of a muchness OO or procedural when code Tony Hopkinson | 11/26/07
I think that's difference between alaniane@... | 11/26/07
For ANY code to work wcannon@... | 11/26/07
In theory alaniane@... | 11/27/07
In theory wcannon@... | 12/02/07
Time for you to advance to patterns Bad Boys Drive Audi | 12/02/07
Just get the damn things the right way round Tony Hopkinson | 12/03/07
You have no idea Bad Boys Drive Audi | 12/04/07
Good example Justin James | 11/26/07
You guys are killing me ;-) jreddy@... | 11/27/07
I am not anti-OO programming alaniane@... | 11/27/07
Same here! Justin James | 11/27/07
ANY Nirvana comes at a price wcannon@... | 11/26/07
Very true Justin James | 11/27/07
OO Nirvana Justin James | 11/26/07
Confucious say SnoopDougEDoug | 11/28/07
RE: Stop the systematic abuse of object-oriented programming gwcarter@... | 11/26/07
Taking it further Justin James | 11/26/07
RE: Stop the systematic abuse of object-oriented programming webx@... | 11/26/07
I simply dont agree jose.a.nunez@... | 11/26/07
Static Methods and Static Classes wcannon@... | 11/26/07
For me... Justin James | 11/28/07
You can do procedural in OO SnoopDougEDoug | 11/29/07
I must confess, I have done it. Tony Hopkinson | 11/29/07
I do Justin James | 11/30/07
I also have done this and will keep doing it when appropriate jslarochelle | 12/02/07
Well Justin was giving an example of the bad use of static methods. jslarochelle | 11/26/07
OOP Poop EM1109 | 11/27/07
Actually, I too am an old timer (really old in terms of ... jslarochelle | 11/27/07
It is a poor craftsman who blames his tools SnoopDougEDoug | 11/28/07
A huge, HUGE problem... Justin James | 11/27/07
Why? normhaga@... | 11/28/07
You read it wrong Justin James | 11/28/07
Not sure if I understand? normhaga@... | 11/28/07
What I mean Justin James | 11/29/07
Thank you. normhaga@... | 11/29/07
Re: What I mean $dunk$ | 12/03/07
Don't forget code review SnoopDougEDoug | 11/28/07
But first a design review... Tony Hopkinson | 12/03/07

What do you think?

White Papers, Webcasts, and Downloads

Recent Entries

TR on Twitter

Archives

TechRepublic Blogs



Quick Reference: Linux Commands
Reduce stress and speed up resolutions with the easiest command references right at your fingertips. You'll receive a PDF file covering Linux, packed with the most common commands you'll need and use daily.
Buy Now
IT Help Desk Survival Guide, Third Edition
TechRepublic's IT Help Desk Survival Guide, Third Edition provides tools and recommendations to help you better manage help desk services, improve end-user support, troubleshoot frustrating hardware issues, identify quick fixes to vexing Windows problems, and help users make the most of Microsoft Office 2003.
Buy Now

SmartPlanet

Click Here