Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > oo2generics...

Reply
Thread Tools

oo2generics...

 
 
Paul Stevens
Guest
Posts: n/a
 
      09-22-2004
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.


 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      09-22-2004
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


 
Reply With Quote
 
 
 
 
Paul Stevens
Guest
Posts: n/a
 
      09-22-2004

"Phlip" <(E-Mail Removed)> 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


 
Reply With Quote
 
Pete Becker
Guest
Posts: n/a
 
      09-22-2004
Paul Stevens wrote:
>
> "Phlip" <(E-Mail Removed)> 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)
 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      09-22-2004
Paul Stevens <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments