oo2generics...

Discussion in 'C++' started by Paul Stevens, Sep 22, 2004.

  1. Paul Stevens

    Paul Stevens Guest

    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.
    Paul Stevens, Sep 22, 2004
    #1
    1. Advertising

  2. Paul Stevens

    Phlip Guest

    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/bin/view/Main/TestFirstUserInterfaces
    Phlip, Sep 22, 2004
    #2
    1. Advertising

  3. Paul Stevens

    Paul Stevens Guest

    "Phlip" <> 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
    Paul Stevens, Sep 22, 2004
    #3
  4. Paul Stevens

    Pete Becker Guest

    Paul Stevens wrote:
    >
    > "Phlip" <> 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)
    Pete Becker, Sep 22, 2004
    #4
  5. Paul Stevens

    Phlip Guest

    Paul Stevens <> 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/bin/view/Main/TestFirstUserInterfaces
    Phlip, Sep 22, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page