30

I know Joel says to never do it, and I agree with this in most cases. I do think there are cases where it is justified.

We have a large C++ application (around 250,000 total lines of code) that uses a MFC front end and a Windows service as the core components. We are thinking about moving the project to C#.

The reasons we are thinking about rewriting are:

  1. Faster development time
  2. Use of WCF and other .NET built-in features
  3. More consistent operation on various systems
  4. Easier 64 bit support
  5. Many nice .NET libraries and components out there

Has anyone done a rewrite like this? Was it successful?


EDIT:

The project is almost 10 years old now, and we are getting to the point that adding new features we want would be writing significant functionality that .NET already has built-in.

7
  • 3
    I'm very curious about answers to this one, as I'm in a similar situation. Jun 10, 2009 at 17:40
  • 2
    as I am, tho in my case, it's moving off really expensive proprietary unneccissiary runtime libraries that we code into via C (not ++)
    – Firoso
    Jun 10, 2009 at 17:42
  • 4
    It depends on "Why" you are doing it. Why break something which works? I would suggest not doing it unless you have a real good reason. I don't have experience in converting a large application like this but it sounds scary to me ;-)
    – Shoban
    Jun 10, 2009 at 17:43
  • 5
    Do you have full knowledge of what all 250k lines are doing? Will you have to guess or reverse-engineer requirements out of some of it? If you have a good grasp on what it all does, a rewrite will be much easier. If not, it's going to be a painful process.
    – dss539
    Jun 10, 2009 at 18:47
  • 3
    Since I'm personally in the process of doing this I'd just like to add one valid and common reason for doing this at all: the place I'm working at has two old school C guys who constantly complain about being too busy and four C# guys who've very little to do. Porting C++ -> C# is an obvious gain in productivity and knowledge transfer in addition to any other benefits, and this may be considered an inevitable consequence of not upgrading your codebase for 10 years. Keep your code alive people.
    – annakata
    Jun 10, 2009 at 18:50

15 Answers 15

49

Have you thought about instead of re writing from scratch you should start to separate out the GUI and back end layer if it is not already, then you can start to write pieces of it in C#.

the 250,000 lines were not written overnight they contains hundreds of thousands of man years of effort, so nobody sane enough would suggest to rewrite it all from scratch all at once.

The best approach if you guys are intend on doing it is piece by piece. otherwise ask for several years of development effort from your management while no new features are implemented in your existing product (basically stagnating in front of competition)

7
  • 4
    Managers think the conversion is line for line... Jun 10, 2009 at 18:06
  • 10
    Yes, do it piece by piece and start by modularizing your current application, so that you can reuse some C++ components (e.g. as DLLs) from your C# rewrites
    – none
    Jun 10, 2009 at 18:06
  • 8
    I agree. While not C++ to C#, we're in year 5 of a 1 year project to rewrite our legacy app. Could've been done in chunks without the customer knowing. Of course, that's not as hype-worthy as a brand new system. Jun 10, 2009 at 19:27
  • 3
    I agree. Modularization is best. You should pull out parts of the system that can benefit most from C#, and leave the rest for later. SnapConfig is correct, in that redoing the entire thing at once is a resource hog and will hault development in its tracks. Jun 10, 2009 at 19:28
  • 1
    This is the way my organization approached rewriting in C#. It's been very successful for us.
    – Rob
    Jun 10, 2009 at 19:46
20

My company actually did that. We had a C++ code base of roughly that size, and everybody (programmers, management, customers) more or less agreed that it wasn't the best piece of software. We wanted some features that would have been extremely hard to implement in the old code base, so we decided (after many discussions and test projects) to rewrite it in .NET. We reused the code that was modular enough using C++/CLI (about 20% of it - mostly performance-critical number-crunching stuff that should have been written in C++ anyway), but the rest was re-written from scratch. It took about 2 man-years, but that number really depends a lot on the kind of application, the size of your team and on your programmers, of course. I would consider the whole thing a success: We were able to re-architect the whole system to enable new features that would have been near-impossible with the old code base. We also could avoid problems we often had in the old software by re-designing around them. Also, the new system is much more flexible and modular in the places where we learned that flexibility was needed. (Actually I'm sometimes surprised at how easily new features can be incorporated into the new system even though we never though of them when we designed it.)

So in a nutshell: For a medium-sized project (100k-500kloc) a rewrite is an option, but you should definitely be aware of the price and risk your taking. I would only do it if the old codebase is really low-quality and resists refactoring.

Also, there's two mistakes you shouldn't do:

  1. Hire a new .NET programmer and let him/her do the rewrite - someone new can help, but most of the work and especially the design has to be done by developers who have enough experience with the old code, so they have a solid understanding of the requirements. Otherwise, you'll just repeat your old mistakes (plus a couple of new ones) in a different language.
  2. Let a C++ programmer do the rewrite as their first C# project. That's a recipe for disaster, for obvious reasons. When you tackle a project of that size, you must have a solid understanding of the framework you're using.

(I think these two mistakes might reasons why so many rewrites fail.)

1
  • 4
    I think you're 100% right about the first of the two mistakes you list, bang on. Rewriting a project is a great opportunity to fix a lot of things, but only if you know exactly what went wrong with the first iteration. The guy who developed the first one (if he's still around) will have insights a newbie to the s/w just won't have yet.
    – bobobobo
    Jun 10, 2009 at 20:12
19

Its been tried before, not only C++ => C#, but VB6 => VB.NET, C++ => Java and any other old => new that you can think of. it never really worked. I think that because ppl don't consider that transformation for what it really is (a total rewrite) they tend to take it lightly.

The migration story from C++ => .NET should be thru CLI, carefully deciding what managed and whats remains unmanaged and s-l-o-w-l-y "fixing" piece by piece.

12

Expression Blend was originally an MFC app. The current version uses WPF for the UI but the engine is still all native. I saw a great talk by principal architect Henry Sowizral about a year ago where he described the process of the migration. Make the engine UI agnostic and you will be able to support whatever the latest UI technology is. The Expression team at one point had what he referred to as the hydra-headed version. Two front-end UIs running simultaneously with one underlying engine - in this way they could see where behavior had unintentionally deviated from the previous version. Since the UI subscribed to events and notifications, changes made in a WPF toolwindow were reflected in the old MFC toolwindow.

EDIT: Looks like some powerpoints are available here or as html here.

5

I've been through a project that did exactly what you're describing with approximately the same size codebase. Initially, I was completely onboard with the rewrite. It ended up taking 3+ years and nearly turned into a death march. In general, I now agree far more with the incrementalists.

Based on our experience, though, I will say that such a rewrite (especially if you're able to reuse some C++ business logic code in .NET), is not as technically dangerous as it may seem. However, it can be very socially dangerous!

First, you have to make sure that everyone fully understands that what you are undertaking initially is a "rewrite" (or "remake") not an upgrade or "reimagining." The 1998 Psycho was a shot-for-shot remake of the 1960 original. The 2003 Battlestar Galactica was a reimagining of the 1978 original. See the difference?

In our case, the initial plan was to recreate the existing product in .NET. That would not have been technically daunting, since we understood the original well. However, in practice, the urge to add and fix and improve just a few things proved irresistible, and ultimately added 2-3 years to the timeline.

Second, you have to make sure that everyone from the execs to sales staff to the end users is ok with your current product remaining unchanged during the development of the remake. If your market is moving is such a way that you won't be able to sustain your business during that period, then don't do it.

So the main obstacles for us turned out to be social, rather than technical. Users and business interests became very frustrated with the lack of visible progress. Everyone felt compelled to push for their own pet improvements and features, too, so our final product bore only a superficial resemblance to the original. It was definitely a reimagining rather than a remake.

In the end it seems to have turned out ok for us, but it was a real grind, and not something we'd choose to do again. We burned through a lot of goodwill and patience (both internal and external), which could've largely been avoided with an incremental approach.

4

C++ won't automatically translate to C# (not so you'd want to maintain it, anyway), and you're talking about using different frameworks.

That means you're doing a total rewrite of 250K lines of code. This is effectively the same as a new 250K-line project, except that you've got the requirements nicely spec'd out to start with. Well, not "nicely"; there's doubtless some difficult-to-understand code in there, some likely because of important issues that made elegance difficult, and the overall structure will be somewhat obscured.

That's a very large project. At the end, what you'll have is code that does the same thing, likely with more bugs, probably fairly badly structured (although you can refactor that over time), with more potential for future development. It won't have any of the new features people have been asking for during the project (unless you like living dangerously).

I'm not saying not to do it. I'm saying that you should know what you're proposing, what the cost will be, and what the benefits would be. In most cases, this adds up to "Don't do that!"

4

I did something similar. Part of my job involves developing & supporting some software called ContractEdge. It was originally developed in Visual C++ 6 by a team in India. Then I took over the development role after it was basically done in 2004. Later on, when Windows Vista was made available as a Beta I discovered that ContractEdge would crash in Vista. The same thing happened in the release candidate.

So I was faced with a decision. Either hunt for the problem in tens of thousands of lines of mostly unfamiliar code, or take the opportunity to rewrite it in .NET. Well, I rewrote it in VB.NET 2.0 in about 2 months. I approached it as a total rewrite, essentially scrapping everything and I simply focused on duplicating the functionality with a different language. As it turns out I only had to write about 1/10th the number of lines of code as the original. Then we held a one month long beta program to iron out any remaining bugs. Immediately after that we launched it and it's been a big success ever since, with fewer problems than the C++ version it replaced.

In our particular scenario I think the rewrite worked out well. The decision was made easier based on the fact that nobody on our team was as familiar with C++ as they were with .NET. So from that perspective, maintainability is now far easier. Nowadays I do think C++ is too low-level of a language for most business software. You really can get a lot more done in .NET with less code. I wrote about this subject on my blog.

3
  • Good point. Do your really need all 250+ lines?
    – gbn
    Jun 10, 2009 at 18:59
  • 1
    Exactly. I can almost guarantee a total rewrite in C# would shrink the size of the project drastically. That by itself isn't usually a good enough reason to rewrite an application. But if the application is starting to experience growing pains in other ways, it may very well be time to consider it. Jun 10, 2009 at 19:20
  • 1
    @gbn,TheSteve - The project is now almost 10 years old. There are definitely growing pains, and we are getting to the point we would start writing significant functionality the .NET framework has already built-in.
    – TWA
    Jun 10, 2009 at 19:25
2

Total rewrite for the sake of rewrite? I would not recommend it.

2
  • We would not be doing it just the the sake of a rewrite. Reasons are listed in the question.
    – TWA
    Jun 10, 2009 at 19:28
  • My apologies for the short answer. In my experience, most major rewrites have been sponsored by the business that I am servicing - and most of that has happened because we estimated refactoring a very large portion of the codebase.
    – Raj More
    Jun 11, 2009 at 18:29
2

In addition to other responses, I would not take "faster development time" for granted. Sure, for most "business" data-centric applications it will probably be the case, but there are many areas where .NET will not bring in significant productivity increases, plus you need to take the learning curve into account.

2
  • You're right, .NET isn't the best solution for any kind of problem, but in a project that size, there's usually lots of "OOP architecture stuff" or "glue code" or whatever you call it. That's where the .NET module system, unified type system, GC metadata, events (...) really shine in contrast to C++. And you can still write modules in C++/CLI anyway.
    – Niki
    Jun 10, 2009 at 18:49
  • @Niki - Yes, our application will definitely benefit from the new features of the .NET framework.
    – TWA
    Jun 10, 2009 at 19:20
2

We've done a big C++ >> C# migration as we move to .NET. It's a quite tough project. Management would hardly bite the funding for it, so you have to go for a compromise. Best approach is to leave the innermost (or lowest) layers in C++ and cover the upper part with C#, with better APIs designed with newer concepts like readability and API-usability in mind, safe-guarded with unit tests and advanced tools like FxCop. These are obviously great wins.

It also helps you layer your components a bit better as it forces certain cuts. The end product is not nice as you might end up copying a lot of code in C++ because years and years of coding contains many bug fixes and many undocumented and hard-to-understand optimizations. Add to that all the pointer tricks you could do in C (our code has evolved from C into C++ over time). As you stabilize you find yourself more and more reading the C++ code and moving it into the C# - as opposed to 'cleaner design' goals you had in mind in the beginning.

Then you find out that interop performance sucks. That may call for a second rewrite - maybe use unsafe C# code now. Grrr!

If all the team members come from C++, the new code is also look like a C++ design. Try to go for a mix of C# and C++ developers in the team, so you can get a more .NET-alike API at the end.

After a while, the project may lose interest and mgmt may not fund the entire re-write so you end up getting a C#-sugarcoated C++ code, and you may still have unicode/64-bit issues unresolved. It really calls for a very very careful planning.

2

I was involved in a very similar size project. It was necessary to rewrite the GUI front end because of new hardware and new requirements. We decided to port this to .NET using C++/CLI. We were able to reuse more then halve of the code and porting it work quite well.

We were able to take advantage of .NET where it made the most sense. This made major parts of the code much cleaner. We found the book "Pro Visual C++/CLI and the .NET 2.0 platform" by Stephen R. G. Fraser very helpful.

1

Have you considered a port to C++.NET? It might be less painful.

1

I'm currently rewriting a rather large web application.

One thing to remember is that when converting from one language to another especially something like C++ to .Net is that you may end up with less, and probably cleaner, code due either due to language advances or framework code.

That's one advantage for future maintainability, even aside from the opportunity to re-architect the less robust aspects of the old application.

1

Some additional comments.

Depending on the lifespan of your application you may be forced to rewrite it in a modern language since I suspect that C++ developers will become increasingly hard to find.

Just moving the app to a new language will not reap that great rewards. You'll probably want to do a redesign of the app as well! Do not underestimate the effort required to do this. I would guess the effort for a redesign + rewrite could be as much as 50% of the effort for the original implementation. (Of course, 50% is a totally unscientific guess).

It's way to easy fool yourself into thinking "Well, C# and WPF are just so much more productive that rewriting this mess would be a piece of cake!"

0

Interestingly most of the answers from people who have done this seem positive. The most important thing IMO is to have good unit tests so that you can be sure your rewrite does what you want it to do (which may not be exactly the same as what the old code did).

1
  • so one like to say they failed! Oct 14, 2009 at 16:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.