I often see forum questions from people asking about converting code from one language to another. It’s often FoxPro to C#, FoxPro to VB, FoxPro to Java, or VB to C#. I don’t recommend converting from one language to another, especially when they are so different from each other, such as the FoxPro conversions.

Earlier this year, I had an assignment to convert C++ code to C#. Because the C++ routine was new and still under development by another team member, I decided to keep everything as close to the same as possible as I thought it would make C# changes easier when the C++ code changed. The program I had to convert took text in one proprietary format and converted it to another. The C++ code used lots of substring manipulations of each line.

But there are subtle problems that cause issues later. For example, while C# is zero-based, C++ claims to be, but isn’t fully zero based. When dealing with substrings, C# starts counting character at zero, while C++ starts at one. You can’t simply subtract one each time because you have to consider other factors such as length of the string, length of the substring, etc.

Well, the “off-by-one” errors have come back to bite me several times. I spent about four hours yesterday to fix one of these issues. I needed to verify that the incoming data was the correct format, what the C++ routine was doing at that point, what the possible side effects would be as a result of the change, compile, unit test, then integrate test the routine. This was four hours that I couldn’t spend on another, high priority project. And this was after the dev on the other team spent over a day trying to figure out where the error was happening.

The end result is that I have decided that I will never again simply convert. It’s always a better idea to understand the original code and rewrite in the second language. This will also allow you to take advantage of any constructs in the second language that don’t exist in the first.