Converting Code From One Programming Language to Another

Tips for Translating and Porting Code

Sara Khandaker
Jan 22 · 5 min read

This week I was building out a web service. I wanted to implement a specific algorithm as part of the service, but I am a strong believer in “don't reinvent the wheel”. Surely the piece of code I needed existed somewhere and I just had to find it. So I scoured the internet and to my disappointment, I was only able to find an implementation of the algorithm in a different language. I found a Javascript implementation, however, my web service was being built on a Ruby framework. Then this got me thinking about translating or converting code between languages.

Translating

So my first thought was, maybe there is something like Google Translate out there but for programming languages. I would much rather just copy and paste and click instead of manually rewriting the code.

In theory, if the programming languages are Turing complete, the code is translatable to any other. So do translators for programming languages exist? Yes! Compilers convert one programming language into another. Usually, compilers are used to convert code so the machine can understand it. If we want it to be human-readable, we need a subset of compilers called transpilers. Transpilers also convert code however the output is generally understandable by a human.

Ok, so why not just use a transpiler? It will give you a translation, however, it will not be an easy-to-read, idiomatic translation. A proper translation just doesn't work like that. This is why in high school French class, our teachers could tell if we wrote the entire essay in English and just hit the translate button. It's just not as good. It may be ok for machines but if you want code that's good for humans to read, then it has to come from a human.

It may be better to just do your conversion manually. At the end of the day, we use programming languages for humans to understand. Computers only understand 1s and 0s anyway. So if your end result works but isn't human-readable, why bother? You want to be able to debug, troubleshoot, or change the code.

That's not to say transpilers are useless. A good tool can help reduce the time it takes for you to write the translation. But in general, after using a machine-translater you will still have to do tweaks to make sure it all works smoothly. But who knows what's in store for the future! Companies right now are working on using AI to create language-independent representations of a program that can then be converted to any particular language.

Porting

So now I’ve decided I was going to change the code manually or I was going to port the code. Porting generally refers to adapting a large block of code to something else. It is the process of translating a program from one environment or tech stack to another. This process can require changing the programming language the code is written in, but not always. For example, converting an Android app so that it can work on iOS.

Remember: Even if you are rewriting code someone else wrote in another language, you should always acknowledge the original project.

6 Steps to Consider:

1. Use Tests

Before starting this endeavor, you need some criteria for success. I recommend Test-Driven Development (TDD). Write black box tests for your original code and confirm they pass. Now when you write out your newly ported code, you can ensure it works by using the same tests.

2. Ensure a Good Understanding of Both Languages

I would not recommend translating code if you are uncomfortable in one or both of the languages. To succeed, you will need to understand the syntax, idioms, and styles of both languages. This is because you don't write and design code the same way in different languages. A literal translation is not the right attitude since you should be really thinking in the new language and rewriting it. This requires a deep knowledge of the languages.

3. Fully Understand the Architecture and Design of the Code

Now that you understand the languages, it's time to understand the project. This is crucial. You must spend time understanding what the code is doing and the system the code is a part of. What are the design patterns used? How does the data flow through the system? What is the format of the data? Do you understand the algorithms used? Are there any side effects of the functions? Once you are comfortable with this, you can start coding.

5. Take Small Bites

When coding your translation, do small parts at a time. Don’t jump into pages of code right away. Go function by function. Keep the translating simple, use the same variable and function names to help you keep track. If you see a way to improve the original code or an issue, do not try to fix it now. We are just going for a copy, we can worry about best practices and conventions after we know we have a translation that passes tests.

4. Focus on Functionality and Not Code

When translating, focus on translating the functionality and not necessarily the exact code written. This can help you take advantage of the strengths of the new language. You can leverage things like built-in functions to achieve the same functionality in the new language. This can help save some time and effort.

6. Make Sure it All Works and Clean Up

When the conversion is completed, make sure it works (using your tests!). Once the tests are passing you can focus on improving the new code. You can make changes based on best practices and conventions. You can start to change variable names or whatever else until you are happy with the end result.

References

The Startup

Get smarter at building your thing. Join The Startup’s +787K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +787K followers.

Sara Khandaker

Written by

I love seafood, exploring new cities, board games and learning to love to code. https://github.com/sarakhandaker/portfolio

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +787K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store