February 21, 2024

Byte Class Technology

Byte Class Technology & Sports Update

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

I used ChatGPT to write the same routine in 12 top programming languages. Here’s how it did
lang-1

David Gewirtz/ZDNET (with a little help from ChatGPT)

Over the past few months, 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.

Also: Okay, so ChatGPT just debugged my code. For real.

But how far does ChatGPT’s coding knowledge extend? In this article, I’m going to throw the classic “Hello, world” programming assignment against the twelve popular languages in O’Reilly Media’s popularity rankings for 2023.

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

Also: How to use ChatGPT: What you need to know now

That should give us a look at program flow and some intrinsic functions as well, but still keep 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.

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

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

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

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

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

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{18875d16fb0f706a77d6d07e16021550e0abfa6771e72d372d5d32476b7d07ec} of the profanity I’ve uttered over the past nine or so years.

Here’s ChatGPT’s code:

javascript

Screenshot by David Gewirtz/ZDNET

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: How to use ChatGPT to build your resume

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

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

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

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

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: What is Auto-GPT? Everything to know about the next powerful AI tool

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

Here’s ChatGPT’s code:

kotlin

Screenshot by David Gewirtz/ZDNET

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

Final thoughts

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: AI could automate 25{18875d16fb0f706a77d6d07e16021550e0abfa6771e72d372d5d32476b7d07ec} of all jobs. Here’s which are most (and least) at risk

But, ChatGPT does sometimes lose the thread. Out of curiosity, I decided to ask it to generate the same code in Forth (a very funky, but fun language). While the code generated was Forth (the colons at the beginning are a dead giveaway), the code block was labeled “perl” (another very fun and funky language). This code is definitely not Perl:

perlfortherror

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.

As with all my other visits down the rabbit hole with ChatGPT and coding, I come away 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.