AlexSavAlexandrov
AlexSavAlexandrov

Reputation: 873

Techniques for translating code by hand

In case I want to translate a program's code in a different programming language, I have these options: to use special software or to do it on my own (please tell me if there are other ways).

Personally I'm a beginner in C++, C# and Java. I'm not familiar with other languages. I'd like to try to do a translation of one of my C++ programs to C# and/or Java. But before doing that I'd like to learn about a technique or two about translating. I'd like to learn it from someone who is familiar with such thing.

So, can you tell me techniques for translating code from one programming language to another? And if there is something that I should know before I start translating, please tell me.

Upvotes: 1

Views: 715

Answers (5)

user1150819
user1150819

Reputation: 1

Isn't it a hedeque to "translate" it by yourself, if there is a software that can do it for you? Unless if there is a special purpose for having that skill of translate from one language to another....
Pay attention that not all languages have the same computable power,therefore it may not work sometimes....
Perhaps it would be better to stay focus on one main language.

Upvotes: 0

emory
emory

Reputation: 10891

If you are into Test Driven Development then you have all the tests for the original implementation. Just keep coding in the new language until you pass all the tests once again. Don't worry too much about Kerrek SB's counterexample. If and when you run into problems like that, just write more tests.

On the other hand, if you don't already have a suite of tests, then why are you wasting time on porting when you should be testing.

Upvotes: 1

escargot agile
escargot agile

Reputation: 22389

Some of the worst C# code I've seen was written by programmers that were thinking in C or C++ while writing C# syntax.

C# and Java are fundamentally different from C++. I don't like the term "translating" because this is the wrong attitude. You actually need to rewrite the program after you've learned how to think in the terms of the new language.

Upvotes: 4

LeleDumbo
LeleDumbo

Reputation: 9340

There are tools, but I never find one that's perfect. At least if it's convertible syntactically, libraries would be a problem. I prefer to have deep enough knowledge between the languages (and of course, the project being converted), and convert everything by hand. The advantages of this would be you could create a clone of the project, using each language's best techniques (that may be either impossible or not optimal in other languages). The disadvantages are, however, you might be doing something wrong doing the conversion (causing incompatible end result) and it's slower.

Upvotes: 2

Kerrek SB
Kerrek SB

Reputation: 477570

  1. Learn both languages. Understand each language's idioms and philosophy. Acquaint yourself with common coding styles and design patterns.

  2. Read your source program, and understand what it is doing.

  3. Think about how you would express the same overall goals in the target language, using the knowledge and experience gained from (1).

  4. Rewrite the program in the target language.

(Counter-example: Your source is Java, and you see String a = new String;. You open C++ and say, std::string * a = new std::string;. Wrong, go back to (1).)

Upvotes: 11

Related Questions