9 Practical Uses of ChatGPT in Programming

anlene
10 Min Read

Since OpenAI announced the launch of ChatGPT, the phenomenal AI chatbot has found a space for itself in many fields, including the complex, fast-moving field of programming.


But what exactly can you do with ChatGPT as a programmer? Beyond the hype, what practical ways can you use the AI ​​chatbot in your day-to-day workflow as a programmer? We’ve put together nine practical ways to integrate ChatGPT into your workflow as a programmer.

MAKEUSEOF VIDEOS OF THE DAYSCROLL TO CONTINUE WITH CONTENT

​​​​​


1. Generate Efficient Algorithms

While some programmers are rapidly outsourcing the entire software development process to ChatGPT, this could be a recipe for disaster. ChatGPT can generate some impressive code. But it’s best to use it as a companion tool while human programmers keep control of the bulk of the development process.

Example of an Algorithm generated by ChatGPT

A good way to do this is to let the chatbot help you lay out an efficient algorithm while you work on the rest of the puzzle to create a robust program. You can ask ChatGPT to generate an algorithm either in plain text, using ASCII art, in a tree format, using boxes, or any other creative visualization technique you can think of.

2. Generate Placeholders and Filler Content

As a programmer, you’ll likely need placeholder data to work with from time to time. Whether it’s database data to test your APIs or long-form text to populate web pages, ChatGPT can generate different kinds of dummy data to suit your needs.

ChatGPT can generate filler content in SQL, JSON, CSV, and dozens of other formats. It can even create native data structures like arrays and lists, in any popular programming language of choice.

While you can find a lot of dummy data generators online, very few, if any, offer the level of data customization that you can get from ChatGPT. You could, for example, ask ChatGPT to generate a 2,000-record CSV file containing English first names, Chinese second names in their anglicized form, and US phone numbers:

Sample of a custom CSV file generated by ChatGPT

You can get as specific as possible with the type of dummy data you want. This is something you may not achieve easily with the free dummy data-generating tools you can find online.

3. Data Formats

Sometimes, you’ll have a lot of plain text that you need to format into a compatible data type. Maybe you need some plain text formatted into a CSV or a JavaScript object like an array. Or maybe you need to convert or format data structures in one language to similar data structures in another.

You can spend time and effort writing regex functions to handle it, or you can provide instructions in English and let ChatGPT do the heavy lifting.

The screenshot below shows a paragraph of unlabeled and disorderly data given to ChatGPT.

disorderly data to be parsed by ChatGPT

Asked to parse this data and present it in a table, ChatGPT respondents with the following:

Sample of data parsed by ChatGPT

ChatGPT was able to accurately label and format the data in a tabular format. It’s worth noting that this would be relatively hard to achieve using a regex-powered formatting function.

4. Translate Code to Another Language

How often do you need a solution to a programming problem in a particular language, but can only find code in other languages? ChatGPT can help you port any piece of code to a language of your choice.

PHP data encryption code

The screenshot above contains a PHP encryption and decryption function that uses AES-256 in CBC mode with a randomly generated initialization vector. We asked ChatGPT to port the code to JavaScript, and here’s the result:

ChatGPT ported code to Javascript

The AI ​​chatbot has been trained in many programming languages ​​and can port code between many of them with a high degree of accuracy. You can also port deprecated or legacy code to newer, more stable code in the same language. All you need is to provide ChatGPT with the right prompts.

5. Optimize a Block of Code

Whether you are building large resource-intensive applications or smaller projects, your code can always use a bit of optimization for better performance. ChatGPT can be very resourceful for code optimization. You can either prompt the AI ​​chatbot to suggest ways to optimize a block of code or ask it to generate an optimized version of the code.

Javascript function to find sum of even numbers in an array

In the screenshot above we have a JavaScript function that takes an array of numbers and returns the sum of all the even numbers in the array. The code works, but it could use a few optimizations. We asked ChatGPT to optimize the code and here’s the result.

Optimized version of a Javascript code

ChatGPT will typically tell you what was wrong with the original code and the optimizations that were made. For our code snippets above, below is a screenshot of optimizations that ChatGPT made.

optimizations made to a javascript

Perhaps one of the most exciting things about optimizing code with ChatGPT is that the chatbot will poke holes in bits of your code you may have thought were already perfect. You can also ask the chatbot to optimize specific sections of your code to suit your unique needs.

6. Write Tests for Code

One of the best ways to ensure your code is bug-free, caters to a range of exceptions, and can handle edge cases, is to write unit tests for it. Of course, writing tests can sometimes be a time-consuming and even confusing task. But why stress when ChatGPT can help out?

Simply copy-paste the code you wish to write a unit test for, and provide ChatGPT with detailed instructions on how to write the test. Of course, we wouldn’t advise outsourcing the entire test writing to ChatGPT.

7. Document Your Code

Code documentation can be challenging, but it’s a vital part of software development you shouldn’t overlook. ChatGPT can produce impressively detailed documentation for code in a wide range of programming languages. You can even ask it to create the documentation in HTML format if you wish to put it online.

Apart from standalone documentation, you can also get the chatbot to comment your code to make it easier for you, or others, to work on in the future. We prompted ChatGPT to comment a simple encryption function in Python that uses a Caesar Cipher, and it got every line right.

Code Commented by ChatGPT

8. Fix Any Bugs

From misplaced brackets and commas to logical errors, ChatGPT can spot errors that could take you days to identify in a few seconds. Simply copy and paste the problematic code and ask ChatGPT to identify errors in the code.

You may fear you’re dealing with logic errors, which are typically harder to spot. If so, just paste the problematic code and describe to ChatGPT what you’re trying to achieve with it and the current outputs you’re getting.

A good practice for hunting down bugs with ChatGPT is to provide as much details about the error as possible. Relevant details might include the language, frameworks, and libraries your code uses or details of the server you’re running it on.

For example, to fix a piece of code in PHP, you might need to provide the type of database involved, the PHP version, server details, and the packages or libraries used.

9. Generate Code From Scratch

One of the standout uses of ChatGPT is for generating computer code. You can always take the tedious, honorable path of writing every bit of your own program. But you won’t be less of a programmer if you outsource a tiny part of it to ChatGPT.

However, you should use AI-generated code with caution, even when it’s free of syntax errors. Logic errors may prove very costly if you deploy AI-generated code on mission-critical software systems.

ChatGPT: A Useful Companion for Programmers

Programming is an intricate activity that requires a lot of moving parts to get right. ChatGPT can be a powerful ally, allowing you to simplify the process of building these intricate parts by tapping into vast amounts of knowledge and expertise in seconds.

Although you shouldn’t treat the AI ​​chatbot as a magic tool to write all your code, it provides an incredibly powerful way of getting things done significantly faster, without reinventing the wheel.

Share this Article