Welcome to MSDN Blogs Sign in | Join | Help

Object-oriented Code Re-use - Fact or Myth?

One of the tenets of software development that is drilled into most everyone is this: write code which can be re-used. Object abstraction and compartmentalization - one of the hallmarks of C++ and other OO languages - was supposed to make this easier than ever. A given piece of software would only ever be written once, and then everyone could benefit from it.

In theory this should work. In practice I've never seen it done with code written in-house, at least in any way that couldn't be done with standard 'C' libraries. At any job. Ever.

Libraries and routines certainly get re-used by the developer who wrote them, and occasionally by other developers they know personally. Buying and using third party components and SDK's is also popular, as well as using toolkits such as STL. However, it's very rare for an unknown developer - without a personal referral - to pick up another unknown developer's code without being forced to.

Object oriented development encourages good practices in terms of object opacity and property protection, and this certainly has value. It promotes a certain way of thinking about solving problems which is generally considered to be good. I just don't buy into the proposition that the benefits extend any further.

If anyone has experiences which contradict my own, I would be very interested to hear them. I've only bumped into one person so far who claims otherwise, which doesn't make for a very good batting average.

Until then, I'll think twice before going out of my way to make code re-usable, since the chance of it ever actually BEING re-used is close to 0.

Published Thursday, January 19, 2006 10:24 AM by John L. Miller

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Object-oriented Code Re-use - Fact or Myth?

Isn't making your code re-usable the same as writing good code?

Reusable code is written at the right level of abstraction and is well documented. Shouldn't all code be this way?

I do understand your point as sometimes the pressure is on to produce "results" rather than nice code. I don't think as an industry we have equated that code==product. If the code is sloppy then it is always going to come back to haunt you.
Thursday, January 19, 2006 6:10 AM by Jeff

# re: Object-oriented Code Re-use - Fact or Myth?

I believe every time you use a class you reuse its code. In procedural programming, every time you call a procedure you reuse its code. It's hard to imagine any piece of modern code that does not call any class or procedure, so it's hard to imagine how to write any piece of software without reusing code. Imagine how would you write even a simple program in .Net without reusing Framework Class Library. As even value types in .Net are represented by objects (well, even without boxing, value types in .Net map to IL types which are an abstraction in the sence that they need to be mapped to processor-understandable types prior to execution, and during that process objects are used anyway), you wouldn't actually write any line of compilable code without reusing objects, and logically their code.

Yet it requires effort and thinking to organize your own code in such a way that maximizes its reusage. The higher % of your code is reusable, the more skilful developer you are.
Thursday, January 19, 2006 7:29 AM by Ruslan

# re: Object-oriented Code Re-use - Fact or Myth?

There is the concept of active and passive vocabulary - at least in the german language. The active vocabulary is what you use in free speech. If a word could be understood, but you doesn't use it, it's in your passive vocabulary.
I believe the same applies to code. Also the rules, in which a word changes from the passive to the active vocabulary are the same.
Thursday, January 19, 2006 9:19 AM by FlorianW

# re: Object-oriented Code Re-use - Fact or Myth?

These are all excellent points. I guess I should focus my assertion a little more, and say that I've seen very little code re-use *across* projects. Most of the projects I've been involved in do indeed have utility functions and classes that many people on the team use, and appropriate common functionality gets extracted into central libraries for that project.

But when you go outside your immediate workgroup, I still maintain that re-use of that code is very, very rare. Unless the code is part of a formal SDK, chances are that someone in another team will write code -- red-black tree's, for example -- from scratch rather than trusting another developer's code which hasn't been widely used, and was written by someone they don't have personal knowledge of.

I think it boils down to a social problem rather than a technical problem. Nevertheless, I still maintain that cross-group code reuse is a unicorn, except for the relatively rare formal case of SDK's
Friday, January 20, 2006 4:39 AM by John L. Miller

# Does code reuse actually happen?

My friend John Miller posts an interesting topic on his blog. Does non platform code get reused? By platform...
Thursday, March 09, 2006 3:08 AM by Tripp Parks's WebLog

# re: Object-oriented Code Re-use - Fact or Myth?

For what it's worth, at one point in time I was squarely in your camp, claiming the same things, as I NEVER saw any real code re-use.

I initially identified what I believed to be the sole reason for this.  After writing something one way, all of the best programmers I know look at the code they produced and immediately know of a better way to do it the next go around.  Thus they all purposely do not re-use anything, as even if they wanted to use "similar" functionality, they would re-write the prior implementation in the better way that they identified at the end of the prior implementation.

But, for as much as I still believe the above is a serious factor in the lack of re-use, I have also come to fully understand what "low coupling" and "high cohesion" really mean with respect to OOP.  Ignoring those two factors, code isn't close to re-usable in the first place.  Taking them into consideration will produce significantly more re-usable code, and thus code that would be re-used.

And if you don't REALLY understand the whole "low coupling" and "high cohesion" deal, then no, from the get go, your OO code will not even be close to re-usable.  FYI, read up on TDD, Inversion of Control, Refactoring, and Patterns just to start for more detailed information.

Strangely enough, in my arena, OOP isn't being used only to promote re-usability.  It is primarily being used from the angle of TDD, in an effort to produce highly testable code.  As one great blogger recently wrote, the utmost characteristic of "great code" is "how testable is it".  IMHO, this is where you should be focusing, not on the re-usability factor.
Thursday, May 04, 2006 10:09 AM by BigJimInDC

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker