8 minutes to read - Apr 24, 2023

9 Practical Uses of ChatGPT in Programming

VISIT
9 Practical Uses of ChatGPT in Programming
ChatGPT has so many potential coding uses, there are probably many that haven't even been imagined yet. Find out about some that have.
Table of Contents
19 Practical Uses of ChatGPT in Programming
1. Generate Efficient Algorithms
2. Generate Placeholder and Filler Content
3. Format Data
4. Translate Code to Another Language
5. Optimize a Block of Code
7. Document Your Code
8. Fix Any Bugs
9. Generate Code From Scratch
2ChatGPT: A Useful Companion for Programmers

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.​​​​

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.

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 Placeholder 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 instance, 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:

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. Format Data

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.

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

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 have you needed 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.

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:

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.

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.

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.

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 it 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.

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-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 many 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 program yourself. 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.

Article source
Maxwell is a loyal Android fan who writes primarily for MakeUseOf's Android section
loading...