code-translation
Code Translation
Function-level translation between languages. The goal is observably equivalent behavior — same inputs → same outputs, same side effects, same error behavior. Not line-by-line transliteration; idiomatic code in the target.
For whole modules/packages, → module-level-code-translator (handles imports, file structure, cross-function concerns).
The three layers
- Syntax — the easy part.
for x in xs↔for (auto& x : xs). Mechanical. - Semantics — the dangerous part.
3 / 2is1in Python 2,1.5in Python 3,1in C. Integer overflow, null semantics, string encoding. - Idiom — the quality part.
[x*2 for x in xs if x > 0]→ Java stream, not a for-loop with an ArrayList. Optional but expected.
Semantic minefields — per language pair
| Source → Target | What silently changes |
|---|---|
| Python → Java | int unbounded → int 32-bit overflow. / float vs int. Dicts ordered since 3.7 — HashMap isn't. None vs checked exceptions. |
| Java → Python | int overflow at 2³¹ → Python never overflows. == on objects is identity, not equality. Checked exceptions disappear. |
| C++ → Rust | UB becomes panic or won't compile. Raw pointers need lifetime decisions. Move semantics are default, not opt-in. Implicit conversions gone. |
| JS → TypeScript | Mostly additive (types) — but == coercion stays, strictNullChecks changes nullability. |
| Python → Go | Exceptions → explicit error returns. Duck typing → interfaces. No default args. **kwargs has no equivalent. |
| C → Go | Manual memory → GC. Pointer arithmetic gone. goto cleanup → defer. No unions. |
Before translating, write down the three semantic mismatches that matter for this function. Check them in the output.
Idiom mapping — common patterns
| Pattern (Python) | Java | Go | Rust |
|---|---|---|---|
[f(x) for x in xs] |
xs.stream().map(f).toList() |
for-append loop | xs.iter().map(f).collect() |
{k: v for k, v in ...} |
Collectors.toMap(...) |
make + loop | HashMap::from_iter(...) |
try/except for control flow |
Anti-pattern — check first | if err != nil |
Result<T, E> / ? |
with open(...) as f: |
try-with-resources | defer f.Close() |
Drop (automatic) |
*args, **kwargs |
Overloads or a params object | variadic (...T) — no kwargs |
No direct — builder or options struct |
| Multiple return values | Record/Pair (awkward) | Native — (T, error) |
Tuple or struct |
Worked example
Python:
def top_k(items, k, key=lambda x: x):
"""Return the k largest items by key."""
if k <= 0:
return []
if k >= len(items):
return sorted(items, key=key, reverse=True)
import heapq
return heapq.nlargest(k, items, key=key)
Go translation:
import "sort"
// TopK returns the k largest items by the given key function.
// If k <= 0, returns an empty slice. If k >= len(items), returns all items sorted descending.
func TopK[T any](items []T, k int, key func(T) int) []T {
if k <= 0 {
return nil
}
// Go has no heapq.nlargest — full sort then slice.
// (O(n log n) vs Python's O(n log k). Acceptable unless n is huge and k is small.)
sorted := make([]T, len(items))
copy(sorted, items)
sort.Slice(sorted, func(i, j int) bool {
return key(sorted[i]) > key(sorted[j]) // descending
})
if k >= len(sorted) {
return sorted
}
return sorted[:k]
}
Semantic deltas noted:
- No default arg — caller must pass
keyexplicitly (or make aTopKIntwrapper). - Complexity: O(n log n) vs Python's O(n log k). For large n / small k, consider
container/heap. Flagged, not fixed — premature optimization. nilvs[]— both are zero-length in Go, butnilis more idiomatic for "empty result."- Copies input (Python's
heapq.nlargestdoesn't mutate either). Matches.
Verify the translation
After translating, → behavior-preservation-checker or → multi-version-behavior-comparator: run both versions on the same inputs, diff outputs. Pay attention to edge cases — empty input, k=0, k=len, duplicate keys.
Do not
- Do not transliterate control flow when the target has a better idiom.
for i in range(len(xs)): f(xs[i])→ don't index in Go/Rust, usefor _, x := range xs. - Do not silently swallow error-handling differences. If the source throws and the target returns errors, every call site changes.
- Do not assume library parity. Python's
heapq.nlargesthas no exact Go equivalent — note the complexity tradeoff. - Do not change mutability behavior. If the source doesn't mutate inputs, neither should the translation. Copy if you have to.
- Do not translate into deprecated/unsafe target idioms. Python
str.format→ JavaString.format, not concatenation. Nostrcpyin C targets.
Output format
## Target
<language + version>
## Translation
<code block>
## Semantic deltas
- <what behaves differently, and why it's acceptable or flagged>
## Idiom choices
- <non-obvious mappings — why this Go/Rust/Java pattern and not the literal one>
## Verify with
<example inputs to diff against the source — especially the edge cases>
More from santosomar/general-secure-coding-agent-skills
code-review-assistant
Performs structured code review on a diff or file set, producing inline comments with severity levels and a summary. Checks correctness, error handling, security, and maintainability — in that priority order. Use when reviewing a pull request, when the user asks for a code review, when preparing code for merge, or when a second opinion is needed on a change.
15dependency-resolver
Diagnoses and resolves package dependency conflicts — version mismatches, diamond dependencies, cycles — across npm, pip, Maven, Cargo, and similar ecosystems. Use when install fails with a resolution error, when two packages require incompatible versions of a third, or when upgrading one dependency breaks another.
4configuration-generator
Generates configuration files for services and tools (app config, logging config, linter config, database config) from a brief description of desired behavior, matching the target format's idioms. Use when bootstrapping a new service, when the user asks for a config file for a specific tool, or when translating config intent between formats.
3ci-pipeline-synthesizer
Generates CI pipeline configs by analyzing a repo's structure, language, and build needs — GitHub Actions, GitLab CI, or other platforms. Use when bootstrapping CI for a new repo, when porting from one CI to another, when the user asks for a pipeline that builds and tests their project, or when wiring in security gates.
3api-design-assistant
Reviews and designs API contracts — function signatures, REST endpoints, library interfaces — for usability, evolvability, and the principle of least surprise. Use when designing a new public interface, when reviewing an API PR, when the user asks whether a signature is well-designed, or when planning a breaking change.
2code-refactoring-assistant
Executes refactorings — extract method, inline, rename, move — in small, behavior-preserving steps with a test between each. Use when the user wants to restructure working code, when cleaning up after a feature lands, or when a smell has been identified and needs fixing.
2