How to use ChatGPT for coding

A developer writing code
(Image credit: Shutterstock / Elle Aon)

If you are someone who is of the mind that there are virtually no limits to the capabilities of artificial intelligence, the fact that ChatGPT can be used to help with coding projects will do nothing to dissuade you of that notion. From simple coding snippets or applets, through websites up to algorithms, there is a great deal that the AI is able to help with – or even take care of completely.

Perhaps the most obvious use for ChatGPT is to simply get it to generate sections of code for you, but there are many more options. The tool can also be used for debugging and optimizing code that you have written, or it can be used to decipher code that somebody else has given you which is proving impenetrable.

You can also use ChatGPT to translate or convert code from one language to another, or to generate comments and documentation for your existing work. The potential is great, and you may just need to spend a little time thinking about how you can best get the tool to bend to your will and provide help with what you need to accomplish. Let's take a look and see just what ChatGPT can do in the coding arena.

Steps for how to use ChatGPT for coding

  • Ask ChatGPT to write code for you
  • Have code converted from one language to another
  • Debug and optimise your code
  • Make sense of code that you don't understand

Tools and Requirements

  • OpenAI account

Step by step guide

1. Create a website template

(Image: © Sofia Wyciślik-Wilson)

When the word coding is mentioned, most people will think of software development, but it's easy to forget that it also covers website development. If you're looking for a quick and easy way to get a site online, you can simply tell ChatGPT what it is you need; describe the style of the site and something about the content, and a few moments later you will be provided with the necessary code. Try something along the lines of: "Create a three-column webpage to advertise my copywriting services, with sections for Technology, Commercial and Advertising work". There will, of course, be quite a lot of cleanup and personalisation work to do with the resulting code.

2. Create CSS and forms for your site

(Image: © Sofia Wyciślik-Wilson)

Even if you are able to create a simple site without much help, CSS is not everyone's forte – but ChatGPT can help. You can describe the look you are aiming for to ChatGPT and it will create a style sheet for you: "create the CSS that will style H1 headings in verdana size 20, bold and red and H2 headings in Tahoma size 15 in grey". You can also have ChatGPT create forms for you: "What is the code for a visitor enquiry form that will email messages to ?" You'll be provided with the necessary HTML and PHP.

3. Have ChatGPT write code snippets

(Image: © Sofia Wyciślik-Wilson)

ChatGPT is capable of writing code in a fairly wide range of languages – in addition to HTML, there is also support for PHP, Java, Ruby, Python, C++ and more. To have code snippets written for you, just tell the site what it is you are trying to achieve – "tell me the Python code to arrange inputted data in a list sorted in ascending order". It's helpful to remember that ChatGPT is context-aware, so you can ask it to make changes or additions without the need to come up with a completely new prompt.

4. Convert existing code between languages

(Image: © Sofia Wyciślik-Wilson)

As you work on a coding project, your needs may change and necessitate that you approach things using a different language. It could also be the case that you have an old project that you would like to update for a slightly different usage. In either case, ChatGPT can help. Just paste the code you already have ready and ask for it to be translated into another language: "convert this Python snippet into Ruby", for example.

5. Have your code optimised and debugged

(Image: © Sofia Wyciślik-Wilson)

There is a lot that can go wrong with code, and there are a lot of ways in which just about any string of coding can be improved. You can ask ChatGPT to not only optimize the code you have written to help improve efficiency, but also get it to debug and explain errors. You can simply paste in your problematic code and ask for it to be fixed, or use it as a learning exercise and ask ChatGPT to explain why a particular error message is appearing.

6. Properly style your code

(Image: © Sofia Wyciślik-Wilson)

If you're the only person who will be looking at your code, it doesn't particularly matter what it looks like – what's important is that it makes sense to you. But if others will need to be able to decipher it, it is helpful to have it properly styled to improve legibility. ChatGPT can also add helpful commenting to code. Use the prompt "Correctly style the following code", and you'll be provided not only with neatly formatted code, but also an explanation of any changes that are made.

7. Learn the function of existing code

(Image: © Sofia Wyciślik-Wilson)

If you have inherited a chunk of code, or you are trying to understand something you wrote yourself a long time ago, you can turn to ChatGPT to understand just what a particular section of coding does. This is helpful when you are trying to track down bugs, but it is also a handy way to expand your knowledge. You can just paste in code you would like to have explained to you, and ask ChatGPT what it does.

8. Have databases created for you

(Image: © Sofia Wyciślik-Wilson)

Databases serve many purposes across a range of project, from websites to apps. You can ask ChatGPT to not only create a database for you in whatever form you require, but also to fill it out with sample data if you wish; just use plain language to describe what you need. You can also ask for help working with databases, such as getting the IA to provide you with queries in SQL to gather information. 

Final thoughts

As powerful and as impressive as ChatGPT may be, it is – like any programmer – fallible. It's entirely possible that there will be some errors to iron out of the code it produces, and it may be the case that it does not produce the most efficient or elegant code. 

It is the perfect tool to help out a somewhat knowledgeable programmer to get a project completed faster, or to help a beginner with getting started. It is by no means a replacement for sheer coding knowledge.

But while you may not be able to get ChatGPT to write the next version of Windows or macOS for you, it can be a major help on smaller projects. It is reasonable to say that programmers' jobs are safe for the time being, but artificial intelligence is not only providing useful help to those who write code, but also opening up the world of programming to people to whom it would otherwise be inaccessible.

Sofia Elizabella Wyciślik-Wilson
Freelance writer

Sofia is a tech journalist who's been writing about software, hardware and the web for nearly 20 years – but still looks as youthful as ever! After years writing for magazines, her life moved online and remains fueled by technology, music and nature.

Having written for websites and magazines since 2000, producing a wide range of reviews, guides, tutorials, brochures, newsletters and more, she continues to write for diverse audiences, from computing newbies to advanced users and business clients. Always willing to try something new, she loves sharing new discoveries with others.

Sofia lives and breathes Windows, Android, iOS, macOS and just about anything with a power button, but her particular areas of interest include security, tweaking and privacy. Her other loves include walking, music, her two Malamutes and, of course, her wife and daughter.

You can find her on Twitter and Mastodon.