Example result: Convert code from one language to another
AI-generated example · Code Converter
Updated

Convert code from one language to another

This prompt converts code from a source language to a target language. Test the converted code, since idioms differ between languages.

text-convert-code-language — chatgpt — 48 words

Convert this code from [SOURCE LANGUAGE] to [TARGET LANGUAGE]:

[PASTE CODE]

Use idiomatic patterns in the target language rather than a literal translation, keep the same behaviour, and list any differences in libraries, types, error handling or performance I should test. Add brief comments where the approach changed.

Ask for a list of behaviour differences so you know what to test after converting.

Get a better result

What you will get

Your code rewritten in the target language using that language’s normal patterns, with the same behaviour, short comments where the approach changed and a list of differences in libraries, types, errors and speed to test.

Why it works

Asking for idiomatic code instead of a literal translation is the main line: word for word conversions run, but nobody wants to maintain them. Keeping behaviour fixed sets the rule, and the difference list tells you where that rule is most likely to break. Comments only where the approach changed keep the new code clean while still showing the reasoning.

Common mistake

The result is a line by line copy that feels foreign.

Fix: Ask it to rewrite the structure the way a native developer would, for example list comprehensions in Python or streams in Java, then compare.

Also watch for
  • Integer division or rounding gives different numbers. Ask it to check every maths operation, since languages differ on division, overflow and rounding, and to add a test for each.
  • A library it uses does not exist in the new language. Ask it to name the package it relies on and the version, then check that package page yourself before you install it.
Try a variation: convert in stages

For a big file, ask it to convert one function at a time with a test each, so you can run both versions side by side.

Still not right? Reply with this

Write a small test that runs the same inputs through the old and the new code and compares the outputs.

More chat & writing prompts

See all 465 →