Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
My Advice: Don't. –  JohnFx Mar 15 '11 at 19:19
    
@JohnFX - Don't what? –  Moshe Mar 15 '11 at 19:21
2  
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 '11 at 19:24
    
@S.Lott That's what I meant by "port". –  Moshe Mar 15 '11 at 19:27
1  
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 '11 at 19:30
add comment

6 Answers

up vote 0 down vote accepted

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
    
+1 for test plan with unit tests in the target language. –  Aditya P Mar 16 '11 at 4:57
add comment

-> 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.

share|improve this answer
add comment

"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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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