Live data from Hacker News

ASK HN: How to engineer a JavaScript to Python migration?

news.ycombinator.com

21–30 of 42 posts

Re: ASK HN: How to engineer a JavaScript to Python migration?

#21
post #8

I did a reasonably big rewrite from JavaScript (Nashorn, long story) to Kotlin/JVM recently (with 60x speedup and elimination of huge variance in runtime). Keys to success in a larger scale translation: - don't redesign anything, do a port (see also, Typescript compiler to Go port) - leverage LLMs interactively: per chunk (e.g. function), copy the old code into a comment in the new code, then use LLM completion to qu…

> - don't redesign anything, do a port (see also, Typescript compiler to Go port)

> Don't get tempted into doing refactorings as you go.

I would say those are the most important. We did so many migrations in the past 30 years and the only ones that went ok were the ones that held to these rules. If you don't, you are rapidly stuck in a lot of pain and probably you won't be able to get out.

Re: ASK HN: How to engineer a JavaScript to Python migration?

#22
Unless you’re dealing with a lot of third-parties who can’t port their code, all of this seems like overkill. Just port the workflows to Python instead of trying to transpile them.

If you have an ecosystem to keep compatibility with, I would look at compiling the JavaScript to WASM and running the WASM from Python, or some kind of sandboxing to continue running the JavaScript as-is.

Re: ASK HN: How to engineer a JavaScript to Python migration?

#23
1. Ensure there are tests for EVERYTHING important on the JS side of things. 2. Port the tests (if necessary -- if there is a REST interface, just use the same tests) 3. Port parts of the code and run against the tests.

This way you have an accurate idea of how your code is working before and after the port.

Re: ASK HN: How to engineer a JavaScript to Python migration?

#24
post #11
post #10

I once needed to convert 2000 line excel formula to PHP but PHP linters sucks and are generally not helpful so I converted it to JS first and then I just added $ signs in front of variable names, few minor tweaks and it worked. It was easier than to go directly to PHP.

Do you literally mean Excel formula and not VBA? That's mind-blowing.

Lot of insurers etc do their work in Excel, i've seen 10000s of 'lines' of formulas in 1000s of sheets needing to be translated into Java. Most of them try, every few years, one of those 'why can't we just run Excel on the backend?' with one of those tools, commercial or not, spend a bunch of money, find it's a crap idea (scalability, maintenance etc) and then port it to a 'real language'.

Re: ASK HN: How to engineer a JavaScript to Python migration?

#25
post #8

I did a reasonably big rewrite from JavaScript (Nashorn, long story) to Kotlin/JVM recently (with 60x speedup and elimination of huge variance in runtime). Keys to success in a larger scale translation: - don't redesign anything, do a port (see also, Typescript compiler to Go port) - leverage LLMs interactively: per chunk (e.g. function), copy the old code into a comment in the new code, then use LLM completion to qu…

> - don't redesign anything, do a port (see also, Typescript compiler to Go port) > Don't get tempted into doing refactorings as you go. I would say those are the most important. We did so many migrations in the past 30 years and the only ones that went ok were the ones that held to these rules. If you don't, you are rapidly stuck in a lot of pain and probably you won't be able to get out.

Do add TODO comments about proposed refactorings for later though.

Re: ASK HN: How to engineer a JavaScript to Python migration?

#26

It is not necessary to automate it. Do it manually, say it requires a lot of attention and care and bill the hours. If they force an LLM on you, say that you are thrilled to use an LLM, give it a try and arrive at the conclusion that unfortunately LLMs are not up to the task. Bill the hours needed to arrive at that conclusion. There's no need for software engineers to automate themselves away. Look at lawyers, they k…

Or - do the best job you can with whatever tools you think will help and sleep better at night.

Re: ASK HN: How to engineer a JavaScript to Python migration?

#27
post #26

It is not necessary to automate it. Do it manually, say it requires a lot of attention and care and bill the hours. If they force an LLM on you, say that you are thrilled to use an LLM, give it a try and arrive at the conclusion that unfortunately LLMs are not up to the task. Bill the hours needed to arrive at that conclusion. There's no need for software engineers to automate themselves away. Look at lawyers, they k…

Or - do the best job you can with whatever tools you think will help and sleep better at night.

Even the Python2 => Python3 translation with the automated tool "2to3" wasn't a great success. Why would Javascript => Python even work?

The best tool is a manual translation.

Re: ASK HN: How to engineer a JavaScript to Python migration?

#28
Check out PythonMonkey [1], it's an actively maintained project which embeds the SpiderMonkey JS engine inside a Python library. It reuses the same memory buffers whenever possible and allows for pretty impressive interop like executing functions back and forth [2].

At my last job we used PythonMonkey to port our complex distributed computing JS Library to Python enabling us to reuse all the code and keep almost all the performance.

1. https://pythonmonkey.io/ and https://github.com/Distributive-Network/PythonMonkey 2. https://distributive.network/jobs/python-monkey

Re: ASK HN: How to engineer a JavaScript to Python migration?

#29
post #15

I would simply dockerize the Airflow tasks and keep them in JS as-is. Then you write the short DAG description in Python but make the task executor launch the Docker containers. And then you're done.

This was my immediate thought. Just because Airflow is written in Python doesn't mean the tasks you're running need to be in Python.

Separate the concerns: migrate the task orchestration to Airflow (or whatever) while keeping the actual Javascript task code largely unchanged.

Post reply on HN