0 Comments Posted in:

Over the years I've ported a lot of code from one language to another. I once converted an MP3 decoder from Java to C# which was the beginning of the NLayer project. As part of the awesome annual Advent of Code event, I've ported both my own and other people's solutions to the daily challenges in various directions between languages such as C#, F#, Python, and TypeScript.

On the face of things this can seem like a bit of a pointless excercise. Why not just use the original language? Or why not find a library in your language of choice that does the same thing? Or why not use the interop features of your preferred language to call into code written in the other language?

Of course all of that is possible, and may well be the most sensible thing to do in a professional development context. But in this post, I want to make the case that there is tremendous benefit taking time to port code, because converting code between programming languages is a great way to learn new languages and improve your development skills.

One common motivation for porting is that you have found a cool piece of code in another language that does something you wish you could do in your own language. And in the process of porting it, you'll find that you quite quickly learn the key syntax constructs of the source language - how variables, assignments, loops and branches work. So its a great way to start learning a new language.

Every now and then, you'll run into language constructs that don't easily translate. For example, I've found that converting from a dynamic or a functional language into C#, often leads me to explore techniques that I hadn't made much use of before. So the learning goes both ways - I became a better C# programmer by learning F# for example.

As part of the process you'll become adept at troubleshooting and paying attention to small details. Sometimes very minor things such as the exact difference between != and !== can make a huge difference to the outcome of your program.

One downside is that sometimes you'll discover features so elegant in another language that you'll feel less satisfied with the language you are currently using. After learning F#, I find myself wishing that discriminated unions were a first-class concept in C#, and the way Python implements tuples is so much more elegant than C# (although it has caught up a little in recent years).

Porting in the other direction can also be a great way of learning a new language. Start with some code in the language you know and try to achieve the same thing in the new language. One downside you'll run into if you take this approach is that the code you end up creating will often not be "idiomatic" to the target language.

Of course, I've focused primarily on porting between languages, but the same benefits apply when porting between frameworks. Try solving the same problem with a different frontend framework (e.g. Vue, React, Blazor, or MAUI), or a different ORM or database type (e.g. relational vs document). There is always a huge amount to be learned by tackling a familiar problem with a different tech stack.

Let me wrap up with a few additional simple open source examples of things I've ported.

Asterisk was originally a BBC basic game that I implemented in VB6, WinForms with C#, Silverlight, WPF, and IronPython! It's probably overdue an attempt to create it in Blazor. I also ported a simple snake game called nibbles from QBasic into WinForms and Silverlight.

Asterisk Screenshot

TypeScript Tetris was me taking a version of TypeScript I originally wrote as a Java desktop and porting it to TypeScript.

Typescript Tetris

When I created my original Skype Voice Changer app, I ported a bunch of audio effects from the built-in REAPER JSFX effects programming language into C#. And I also wrote an article (sadly now lost) for the now defunct Microsoft Coding4Fun website where I ported a C++ auto-tune algorithm into C#.

Skype Voice Changer

More recently I tried porting some C++ VST audio effects over into REAPER's built-in JSFX programming language, and made a tutorial video you can watch here.

In summary, I can highly recommend porting code as a great way to learn new languages, pick up new ideas for tackling problems, and having fun experimenting with new programming techniques and frameworks.

Vote on HN