473,380 Members | 1,385 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,380 software developers and data experts.

oo2generics...

Hi

This may be an extremely silly and stupid question, but I would like to be
able to transform my existing software, developed traditionally with c++ and
object-oriented design (classes, inheritance, virtuals, etc...), to a modern
C++ design using generics, i.e. a template-centric design.

Is there a prescription for how to do such a transformation? Has such an
exercise been described in an article or tutorial before? I can imagine a
pattern language is waiting to be uncovered. Wake me up if I'm just
dreaming...

Paul Stevens

PS. I know you could advice me to "go get the books by Alexandrescu, Sutter,
Vandevoorde&Josuttis, Austern, Meyers, etc - then go meditate over them"
but, if possible, I'd much rather like to get concrete tips and advice on
how to make the transformation.
Jul 22 '05 #1
4 1064
Paul Stevens wrote:
This may be an extremely silly and stupid question, but I would like to be
able to transform my existing software, developed traditionally with c++ and object-oriented design (classes, inheritance, virtuals, etc...), to a modern C++ design using generics, i.e. a template-centric design.
Why?

If you answer, I will ask "Why?" again, at least 4 more times. Be prepared
to answer in detail - if your code already works don't increase risk by
screwing with it.
Is there a prescription for how to do such a transformation?
Yes, it's called "refactoring".
Has such an
exercise been described in an article or tutorial before? I can imagine a
pattern language is waiting to be uncovered. Wake me up if I'm just
dreaming...


Read the book /Refactoring/. It uses Java, because that is such a
technically superior language (wink wink), and it does not show "refactor to
template".

What it says over and over again is to write dozens of unit tests,
preferably before writing the tested code. Then make <10 edits towards a
refactor, run all the tests, and make them pass. If they fail, tap <Undo>
until the tests pass, and try again. Folks using these techniques, among
others, report an order of magnitude fewer defects. (That's still not an
excuse to bust up working code.)

In your case, I would look at the existing code, write a unit test for it,
then write a test for a new version that uses a template, then write a
little new code to make that test pass, then refactor it to clean up its
design, then repeat until all behavior migrates into the new code.

If I had a reason to do this.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #2

"Phlip" <ph*******@yahoo.com> wrote:
Why?

Why? You do not need to care ;-)
Yes, it's called "refactoring".

Unfortunatly, that book only covers refactoring issues when shifting from
procuderual programming to OO. It does not cover the shift moving from OO to
generic programming. That is not so strange since Generics in Java did not
at that time.
It uses Java, because that is such a technically
superior language (wink wink),


Oh, is that so... Sigh. But I'll admit that Fowler's Refactoring book is a
very good read!

PaulS
Jul 22 '05 #3
Paul Stevens wrote:

"Phlip" <ph*******@yahoo.com> wrote:
Why?


Why? You do not need to care ;-)


On the contrary: the answer to "how" depends strongly on the answer to
"why". If you don't know where you're going it doesn't matter how you
get there.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #4
Paul Stevens <so*****@microsoft.com> wrote:

Hey! Microsoft is cool these days!
Phlip wrote:
Why?

Why? You do not need to care ;-)


Generics are a compile-time hack to accomplish what dynamically typed
systems, like Python, Ruby, or Smalltalk, accomplish by making classes
objects. C++ can't put that conceptual burden into its compiler, so instead
it provides elaborate and relatively typesafe macros, called templates.

In Ruby, to achieve Construction Encapsulation, you can pass the name of a
class to a method, and it will call new to create an object. In C++, this
requires the Prototype Pattern.

If the answer to "Why?" is I feel like noodling around, then noodle, and
blog your results. However, if you have a business reason, then generics
will not automatically give you a smaller footprint, shorter compiles,
faster executables, or more comprehensible code.

Try www.boost.org, to see if you can import any of their smart pointers and
such...

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.