oo2generics...

P

Paul Stevens

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.
 
P

Phlip

Paul said:
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.
 
P

Paul Stevens

Phlip said:

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
 
P

Pete Becker

Paul said:
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.
 
P

Phlip

Hey! Microsoft is cool these days!
Phlip wrote:

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,140
Latest member
SweetcalmCBDreview
Top