1

I am refactoring a widely used class. Given how widely it is used, my intend is to create a version 2 of the class. I will keeping the interfaces same/similar [I am forced to make some changes, otherwise it will become ugly with new changes] so that switching from old class to new class becomes easy. And we can switch applications using the old class to new class one by one.

Now I am not sure how to manage name / namespace in this case.

Eg:

Currently, lets say, the class is under a namespace 'app'

namespace app {

    class Important {

        ...
    };
}

I would like to keep the class name same or very similar so that the meaning is clear.

namespace app {

   // This looks okay (conveys the meaning), but is ugly.

   class Important2 {
      ...
   };
}

namespace app {

   namespace v2 {

       // I think this will be confusing. It will given a feeling that the v2 
       // applies to app namespace. There are lot of classes under 'app' 
       // namespace which are not changed.
       class Important {

       };
   }
};

Is there a better approach?

Thanks!

MGH
  • 475
  • 7
  • 19
  • Take the class name and look up a synonym for it and use that, or nest a namespace within this app namespace and then declare the function with the same name in the nested namespace. – Francis Cugler Mar 01 '19 at 04:13
  • ABI stability matter? Is the bame highly unique? Can you do sweeps of old code, renaming *old* type? – Yakk - Adam Nevraumont Mar 01 '19 at 05:06
  • @Yakk It's header only library / class. So it is fine I guess. I can rename old type everywhere. But the name is something everyone 'knows'. So changing to 'important2' still preserves the meaning and I a leaning towards that. – MGH Mar 01 '19 at 05:22

2 Answers2

0

I think the best solution to your problem is to used the gof factory design pattern, where you have an interface and a number of implementations for example Important, Important2 etc.
You could then tell your consumer that you will be deprecating Important soon but a compiler warning

Damian
  • 4,395
  • 4
  • 39
  • 67
0

You could resolve via simple namespace alias. A sample could be in this answer https://stackoverflow.com/a/41420638/781933, for a switch on/off approach (driving migrations on a per-project base).

If instead you have to go through intermediate compositions rather than a complete replacement you could adopt intermediate solutions with compositions and inline namespaces.

You can find a specific example of version management via namespacing in the Stroustrup's The C++ Programming Language.

rfb
  • 1,107
  • 1
  • 7
  • 14