I had seen others say that code conversion was possible. It had worked well enough for small code snippets, so I thought, why not try with something more substantial. While working on three-svg-js, I was looking for something to support clipping and came across the clipper2. At the time, there was only a port of clipper1 to JavaScript. I figured, this would be a perfect opportunity to at least give it a try. If it didn’t work, at least I would learn something.
I was not familiar with the clipper2 code base at first, but the examples made sense to me. I figured all the C# code would need converting.
I started by asking Chat GPT to convert the whole file, 1000+ lines long. But its lazy, due to buffer sizes, so would only convert the first 100 lines. I then proceeded to convert one function at a time. It was reasonably accurate. In the end, it took well over 100 prompts over 3 days to convert all the code.
Some advanced syntax like nullability it did not understand at all and always got wrong. I had to clean the C# code of attributes and #ifdefs before prompting.
The original C# includes a lot of ref and out parameters. Sometimes Chat GPT would return an object, but sometimes it incorrectly convert this concept and I’d have to hand fix the code.
The original C# includes a lot of functions with the same name, but different parameters. There isn’t supported in JavaScript. After looking at the code base as a whole, it turns out by dropping support for ClipperD, the duplicate functions would mostly go away.
The original C# includes classes with multiple constructors. Again, not supported by JavaScript. The best solution was to turn arguments into object with optional parameters, but Chat GPT wouldn’t do this part for me. Instead it mangled the code, so I stopped using it for constructors, just the code inside them.
The original C# had read only structures. Chat GPT converted to classes which is reasonable.
When encountering .NET interfaces like IComparer or Iterator, Chat GTP would do its best, but the code was unusable. This part I had to find a solution myself.
The original C# code had custom operators (== and !=). Chat GTP converted to static methods which is reasonable, but required code cleanup later
It was more challenging for longer functions. The second and subsequent code snippets wouldn’t convert well since it was missing the beginning of the function.
In total, 5300 lines for the library and 400 lines of test code were converted to TypeScript with about 2 weeks of effort.
After all the converted code started to compile, it took about another week of effect to get it working. This was mostly because I didn’t understand the original C# code and all the capabilities of clipper2 or how they worked.
For someone familiar with the original code base and how it works, this effect would be much smoother. Since clipper2 had been ported from C++, the author had limited their use of C# advanced syntax. If you’re using modern advanced C# syntax features or have lots of long functions, it might not go so well.
In summary, using Chat GPT definitely helped saved time. It was also good at suggesting how a language feature can be represented in the target language, although not always. It was also frustrating at times.
Had the original code base been 20,000 lines, I probably would not have attempted or given up before finishing.