X
Innovation

I used ChatGPT to write the same routine in 12 top programming languages. Here's how it did

We know ChatGPT can code, but how will it fare when a dozen languages are thrown at it in one session? The answer: pretty darned good.
Written by David Gewirtz, Senior Contributing Editor
Reviewed by Alyson Windsor
lang-1
David Gewirtz/ZDNET (with a little help from ChatGPT)

Over the past year, we've all come to know that ChatGPT can write code. I gave it a number of tests in PHP and WordPress that showed both the strengths and weaknesses of ChatGPT's coding capabilities.

But how far does ChatGPT's coding knowledge extend? For this article, I threw the classic "Hello, world" programming assignment against the twelve most popular languages in O'Reilly Media's popularity rankings for 2023.

Also: How LinkedIn's free AI course made me a better Python developer

Because "Hello, world" can often be coded in one line, I added a slight wrinkle, having ChatGPT present "Hello, world" ten times, each time incrementing a counter value. I also asked it to check the time and begin each sequence with "Good morning," "Good afternoon," or "Good evening."

This gives give us a look at program flow and some intrinsic functions as well, but still keeps the code small enough that I can include a dozen screenshots in this article.

Here's the prompt:

Write a program in ____ that outputs "Good morning," "Good afternoon," or "Good evening" based on what time it is here in Oregon, and then outputs ten lines containing the loop index (beginning with 1), a space, and then the words "Hello, world!".

For each programming language, I also asked ChatGPT to describe its primary use. Here's the prompt I used for this query:

For each of the following languages, write a one-sentence description of its primary use and differentiating factor: Java, Python, Rust, Go, C++, JavaScript, C#, C, TypeScript, R, Kotlin, Scala.

Now, let's look at each language.

1. Java

ChatGPT describes Java as, "A general-purpose language used primarily for building desktop, web, and mobile applications, and known for its 'write once, run anywhere' philosophy."

Also: The best AI art generators to try

Java was originally developed by Sun Microsystems, but when Oracle bought Sun, it also bought Java. While the Java spec is open, the language is owned by Oracle. This has led to some spectacular legal fireworks over the years.

Here's ChatGPT's code:

java
Screenshot by David Gewirtz/ZDNET

2. Python

ChatGPT describes Python as, "A general-purpose language used for data analysis, artificial intelligence, web development, and automation, and known for its readability and ease of use."

Also: How to write better ChatGPT prompts

My advice: if you plan to learn to code for AI applications, learn Python. Almost all AI code has tight Python integration.

Here's ChatGPT's code:

python
Screenshot by David Gewirtz/ZDNET

3. Rust

ChatGPT describes Rust as, "A systems programming language used for building high-performance and reliable software, and known for its memory safety and thread safety guarantees."

Here's ChatGPT's code:

rust
Screenshot by David Gewirtz/ZDNET

4. Go

ChatGPT describes Go as, "A systems programming language used for building scalable and efficient network and server applications, and known for its simplicity and built-in concurrency features."

Also: How to make ChatGPT provide sources and citations

Go is open source, but it's managed by Google. Go. Google. Get it?

Here's ChatGPT's code:

go
Screenshot by David Gewirtz/ZDNET

5. C++

ChatGPT describes C++ as, "A systems programming language used for building operating systems, game engines, and high-performance applications, and known for its control over hardware and memory."

Here's ChatGPT's code:

cpp
Screenshot by David Gewirtz/ZDNET

6. JavaScript

ChatGPT describes JavaScript as, "A client-side scripting language used for building interactive web applications, and known for its widespread use in web development and its ability to run in web browsers."

Also: Future ChatGPT versions could replace a majority of work people do today

Also, along with CSS (one of the web's main visual design languages), JavaScript is directly responsible for 87.45% of the profanity I've uttered over the past nine or so years.

Here's ChatGPT's code:

javascript
Screenshot by David Gewirtz/ZDNET

7. C#

ChatGPT describes C# as, "A general-purpose language used for building Windows desktop applications, games, and web applications, and known for its object-oriented features and integration with the .NET framework."

Also: Have 10 hours? IBM will train you in AI fundamentals - for free

C# is a proprietary language offered by Microsoft. I taught it at the UC Berkeley extension for more than a decade. It is a very interesting gateway language for anyone wanting to get work programming for predominantly Microsoft environments.

Here's ChatGPT's code:

csharp
Screenshot by David Gewirtz/ZDNET

8. C

ChatGPT describes C as, "A systems programming language used for building operating systems, embedded systems, and high-performance applications, and known for its efficiency and low-level control."

Also: How to use ChatGPT to write Excel formulas

They say you never forget your first love. Before teaching myself to program C back in the days of wooden ships and iron programmers, I never truly loved a programming language. But then I started to use C and my world changed. It was love. Something about the concise simplicity of the language just spoke to me on a deep and primal level. That's probably an overshare, but it's also true. C will always be my first syntactic love.

Here's ChatGPT's code:

c
Screenshot by David Gewirtz/ZDNET

9. TypeScript

ChatGPT describes TypeScript as, "A superset of JavaScript used for building large-scale web applications, and known for its optional static typing and advanced language features."

Also: How to use ChatGPT to write code

TypeScript is also a Microsoft product, but unlike C#, it's open-source and non-proprietary.

Here's ChatGPT's code:

typescript
Screenshot by David Gewirtz/ZDNET

10. R

ChatGPT describes R as, "A language used for statistical computing and data analysis, and known for its powerful data manipulation and visualization capabilities."

Here's ChatGPT's code:

r
Screenshot by David Gewirtz/ZDNET

11. Kotlin

ChatGPT describes Kotlin as, "A general-purpose language used for building Android apps, server-side applications, and web applications, and known for its conciseness and interoperability with Java."

Also: I spent a weekend with Amazon's free AI courses, and highly recommend you do too

Kotlin was developed by JetBrains, a small company that also makes PhpStorm, my current favorite development environment.

Here's ChatGPT's code:

kotlin
Screenshot by David Gewirtz/ZDNET

12. Scala

ChatGPT describes Scala as, "A language used for building scalable and distributed applications, and known for its support for functional programming and its integration with the Java Virtual Machine."

Here's ChatGPT's code:

scala
Screenshot by David Gewirtz/ZDNET

Also, notice the output for Scala (shown above). I've never programmed in Scala and don't know very much about it. Apparently neither does ChatGPT, because while the AI provided syntax coloring for all the other languages, it didn't seem to have that information on hand for Scala.

Final thoughts (and a little Forth)

As a computer languages geek, I found this exercise super fun. That said, there are some things to keep in mind. First, I didn't test all the code. Testing this many outputs is outside the scope of this article. That said, I did read through the generated code and -- for most languages -- the code looked good.

Also: How to use Copilot Pro AI to generate formulas and analyze data in Excel

When I last ran these tests, almost a year ago, ChatGPT got almost everything right (notwithstanding the above disclaimer). However, when asked to render code in Forth (a very funky, but fun language), it generated code that looked like Forth but labeled the window "Perl." It definitely did not generate Perl.

However, this time, it both generated Forth (the colons are dead giveaway) and labeled it appropriately. Like Scala above, ChatGPT didn't seem to have the syntax coloring tables for Forth, but otherwise it seems to be doing fine.

forth
Screenshot by David Gewirtz/ZDNET

As with all my other visits down the rabbit hole with ChatGPT and coding, I'm impressed, but also wary. My recommendations stand: use it as a tool, but test, test, and test.


You can follow my day-to-day project updates on social media. Be sure to follow me on Twitter at @DavidGewirtz, on Facebook at Facebook.com/DavidGewirtz, on Instagram at Instagram.com/DavidGewirtz, and on YouTube at YouTube.com/DavidGewirtzTV.

Editorial standards