What advice (tips, techniques, gotchas, et cetera) do you have for someone who is porting a code library from one language to another, or rewriting it to work in a different compiler, environment?

  • My Advice: Don't.
    – JohnFx
    Mar 15, 2011 at 19:19
  • @JohnFX - Don't what?
    – Moshe
    Mar 15, 2011 at 19:21
  • 3
    don't "port" -- i.e., clone something into a new language retaining features of the original language. Totally rewrite it so that it has the same test results.
    – S.Lott
    Mar 15, 2011 at 19:24
  • @S.Lott That's what I meant by "port".
    – Moshe
    Mar 15, 2011 at 19:27
  • 2
    What did you mean by port? Here's a hint. Don't comment on your own question. update your question to be complete and crystal clear. Never comment on your own question because it's your question. You own it and you can make it perfect. Feel free to delete the comments which form a hard-to-follow trail when your comment assumes we know what you're talking about with words like "That's what I meant". What does "that" refer to?
    – S.Lott
    Mar 15, 2011 at 19:30

6 Answers 6


Every platform (language and system libraries) has its own persona. The way a Ruby programmer designs their API takes advantage of the unique features and culture that make Ruby what it is. This is distinctly different than the way a C or C++ developer would design their API, which is in turn different from the way a Java or C# developer would do it.

You want to take the ideas from the original platform, and mold them into the concepts that make the new platform what it is. Nothing feels less natural than using a library designed in a completely different manner than the other libraries in the platform. There's a number of programming platforms that are actually well designed, and elegant. You want to leverage those strengths for your port.


Know the native capabilities of both your "to" and "from" language/framework.

I was porting a calendar library to iOS from Javascript. iOS has a robust calendar class whereas JavaScript had nothing which was of use to me. (I was converting from a religious calendar to the Gregorian calendar and vice versa.)

I spent time rewriting the entire library in Objective-C only to realize that my code was largely redundant. (By the way, I did a poor job and was getting inaccurate results. That's when I took a step back and realized my mistake.)

Several tens of lines of conversion methods were replaceable with six or so lines of native framework code.


If this is a critical project (porting often is), write a complete test plan (ideally with unit tests in the target language) BEFORE porting each piece of code and the entire system.

There is no way in the world that the porting will be 100% accurate in the first try, so you need to make sure in advance that you're catching everything.

If possible, execute the same plan for the original code because there are no guarantees that it was written correctly, and, in fact, having an equivalent behavior may depend on an incorrect implementation.

  • +1 for test plan with unit tests in the target language.
    – Aditya P
    Mar 16, 2011 at 4:57

-> Focus on the functionality not the way it has been coded.

-> Think before write, because some elements can be already handled by language, platform etc.

-> Use the advantage of new platform.


"Porting" -- as you appear to be using the word -- is just TDD with annoying legacy code.

Get the test cases right. Confirm that the legacy code passes the test cases.

Then discard the legacy code.

Then write new code that passes the test cases in the new language.


Build a class diagram/hierarchy tree so you know which to port first. Map the basic data types (int -> Int32?). Do the same for the data structures.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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