Live data from Hacker News

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

news.ycombinator.com

31–40 of 42 posts

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

#31
How to translate JavaScript to Python is a bike shed.

The thing that really matters is how are you going to ship this?

You should figure out if there is a way it can be delivered incrementally.

Make sure it's easy yo roll back from new to old on as small a chunk as possible.

Make sure rollbacks and deploys don't require manual futzing.

Make sure it's easy for outside people to KNOW the status of things without asking you.

Make sure you have a way to coordinate with feature devs on when it's OK to work on a specific chunk.

Make sure you can test if things are working after you deploy a change.

After that you'll probably come up with like 30 ways to translate the code and use all of them until you find one that's actually tollerable.

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

#32
Line by line, don't overthink it.

Programmers have an unhealthy aversion to repetitive tasks. Sometimes you just have to do work-work. Happens all the time in other industries,

clock in at 9 do the same thing for 2 hours, take a break, do the same thing for 2 hours, lunch, 2 hours break, 2 hours, go home.

Repeat this for weeks if necessary, you can plan it out and predict when it will be done, if need be ask for more resources.

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

#33
post #27
post #26

Earlier quoted context omitted.

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.

I think many people will disagree with you on this. (and just to clarify - automated/manual is not a binary choice)

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

#34
Was it Larry Wall that said "it's easier to port a shell than a shell script"?

I've done similar inter-language changes, and I have always found it easier to not change the language of the business logic. (Unless the change gives you something really big - for me I often port stuff to numpy because I need the vectorized code, but that's only for very specific problems).

If I had this task, the place where my brain would go would be to find a way to compile JS into C and then use C calling conventions to call the functions from Python. Keep the JS code around so that if you need to change anything, you change it in JS and then recompile to C.

I don't know the JS space very well, but can you get a JS interpreter that lives in Python? That way you can call JS functions from Python?

I don't like transpiling, there's always enough differences between the languages that something bad happens. When I've run into issues like this, since I'm an "old guy", I tend to try to get everything into C calling conventions and use that as my base interface.

Worst case, there has to be some good JS interpreter that can give you a C interface that you could call from Python. So you'd have Python -> C -> JS and your business logic can still live in JS (if your port is because of efficiency and you need compiled code, then you can ignore me.)

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

#35

Was it Larry Wall that said "it's easier to port a shell than a shell script"? I've done similar inter-language changes, and I have always found it easier to not change the language of the business logic. (Unless the change gives you something really big - for me I often port stuff to numpy because I need the vectorized code, but that's only for very specific problems). If I had this task, the place where my brain wo…

> can you get a JS interpreter that lives in Python

The PythonMonkey library is a full JS interpreter running in the same Python process. It allows for JS functions to be called from Python and vice versa

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

#36

Airflow allows you to run node/bun/whatever from your python step. Unless you're really hurting with performance, do you need to port those things at all?

A comment would be useful about what people disagree with so much. OP starts with "I was tasked with migrating a legacy workflow system..." rather than tasked with converting the code, so it may be useful to bring other alternatives to the table.

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

#37
post #17

If you have any async JS, that's going to seriously complicate things. Theres no AST mapping for that (python async is not the same). Pitfalls to watch our for? Tons of them. Comparison is very different, modulus is different, .sort is different, object destructuring doesn't map nicely to python, lambda's won't map nicely to python, promises won't map to python. Labelled loops won't map nicely to python. If your JS s…

Random idea: Couldn't Babel translate async code to callbacks?

You could if you want your codebase to be an unmanageable mess.

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

#38
There are type system gotchas. My favorites have to do with integers in place of JS's double-only numerics, the treatment of undefined and nulls, especially in arrays.

The LLM's are just fine at AST translation, though they might inject their quirky preferences if you don't watch them carefully. Suggestion: use the LLM's, but interactively, to translate one script at a time, starting with the simpler ones. Tell the LLM to explicitly call out potential issues. Manually review each, and incorporate the issues and preferences you learn into the prompt. If all goes well, the process will quickly converge on a cut and paste job, but don't be tempted to fully automate it if it's a hundred scripts -- different matter if it's thousands.

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

#39
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…

> for every method / control flow ported, add tests that target the newly added code, validating it does the same as the old code

Nice, and as a bonus you end up with a well tested system. Can't speak highly enough of data driven testing for this kind of system. Gives you such confidence.

> When you notice an opportunity to refactor, create a bug for it.

Did you have success in getting time to revisit all these bugs? Did you get pressure to fix them along the way (in either codebase)?

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

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

110% this. Resist the urge to make changes until everything is moved over. Any system 'enhancements' may also be viewed as bugs/defects, and reduces trust, requiring lengthier validation.
Post reply on HN