PDF download Download Article
Get ChatGPT to write usable code for applications and websites
PDF download Download Article

By now, you've heard that ChatGPT can write code. But can the AI chatbot generate effective code? While ChatGPT isn't an experienced software engineer, the chatbot can help you write, debug, test, and improve code in languages like Python, JavaScript, Java, Go, Ruby, C++, C#, PHP, Swift, TypeScript, and SQL. This wikiHow guide will teach you the best ways to use ChatGPT to write code and boost your productivity as a software developer.

Things You Should Know

  • ChatGPT can clean up existing code by correcting mistakes, simplifying complex ideas, and ironing out bugs.
  • To save time as a developer, use ChatGPT to create scaffolding, templates, and boilerplate code for your applications.
  • ChatGPT does make mistakes, so it's not a replacement for a software engineer. Always test ChatGPT code before implementing.
Section 1 of 2:

How to Use ChatGPT for Coding

PDF download Download Article
  1. One of the best uses for ChatGPT in software development is to create scaffolding for your programs. Tell ChatGPT what type of program you want to write, and paste in any libraries, dependencies, file names, and other details to include. You can type your query naturally, using complete sentences and paragraphs, and ChatGPT will respond with a template for your code in seconds.
  2. Spend less time looking up syntax examples on Stack Overflow , asking ChatGPT to write quick functions, routines, and other code. For example, if you ask ChatGPT to "write a Python function that reverses a string using a slice," ChatGPT will not only generate the code, but also explain how it works. You can then use the code as a boilerplate throughout your projects.
    Advertisement
  3. Ask ChatGPT how to change your existing code to make it faster, sleeker, and/or do other things. Paste your code into the chatbot, along with what you want to change about the code. For example, "I want to display the results in numerical order based on the ID column," or "Add these items to the to the list at position 3."
  4. Make your programs more compact by sending ChatGPT your code and saying, "Simplify this code." To rework your code without changing its original structure, say, "Refactor this code for efficiency." ChatGPT will clean up your code to make it cleaner and faster. The chatbot will also summarize the changes it made to your code, which can be helpful for writing cleaner code in the future.
  5. Whether you're confused about a code snippet or an entire program, you can ask ChatGPT to break it down for you. Simply paste code into ChatGPT and ask questions like, "What does this function do?" or "How does this algorithm work?"
  6. ChatGPT can be helpful in identifying and repairing errors in your code. Ask questions like, "What's wrong with this code?" or "How do I fix the bug in this program?" and paste the code into the field. If ChatGPT needs more information, it will prompt you to enter additional details, and provide fixes as needed.
  7. Even if your program works, there might be a better way to code it. Send ChatGPT your code and ask, "Is there a better way to accomplish <your goal>?" ChatGPT will make recommendations, such as suggesting alternative algorithms, and explain all of its suggestions in detail.
  8. If you're already well-versed in one programming language, you can use ChatGPT to convert code to a language you're less familiar with. For example, try pasting a C++ program into ChatGPT and asking "translate this code to Java."
  9. Instead of spending time writing your own test cases for functions, ask ChatGPT to write them for you. ChatGPT will perform several test cases on the function you specify and report the results. [1]
  10. Instead of writing your own how-to documentation or comments for your program, simply paste the code into ChatGPT, and type, "Explain this code." You can then add ChatGPT's explanation to your program's comments, or even use it in your official documentation.
  11. Advertisement
Section 2 of 2:

Getting Effective Code from ChatGPT

PDF download Download Article
  1. ChatGPT relies on a combination of the context you provide and its training data. If you don't specify exactly what you need, ChatGPT won't produce code you can use. Be very specific about what you want your program or website to do, what language you want to write it in, and what the program is for. For example, if you want ChatGPT to build you a website for your pet-sitting business that allows potential customers to view your services and inquire about bookings, try: [2]
    • "Build a simple one-page website for my pet-sitting services, which include dog walks and overnight house-sitting stays. Create a table that contains my services and rates, which are $60 per night for overnight stays, and $20 for one-time 30-minute walks. Create a contact form that customers can fill out to request my services. On the form, allow the customers to select their desired dates from a calendar. Send customer responses to me via email at me@myemailaddress.com."
    • With this example, ChatGPT will provide the HTML and CSS code for a simple website that you can upload to your web host. You'll just need to make a few minor tweaks to personalize the website, but the code is now yours to play with.
  2. Because ChatGPT is conversational, it can reference up to 3000 words from the current conversation, which gives you plenty of room to add to your code request. [3]
    • Before, we asked ChatGPT to code us a website for a pet-sitting business. What if we also want to add our grooming rates to the table on the page? All you'll need to do is say, "Add grooming services for $100 to my rate list," and ChatGPT will regenerate the code to include your changes.
    • You can also add other details, like "change the title tag to Affordable Pet Sitting Services in San Francisco," and "display a "thank you" page to the customer after they submit the form."
  3. If the AI chatbot creates code that's confusing or flat out wrong, tell it so. Ask ChatGPT for clarification, and it will check its own work against its training data and adjust the code accordingly. You can also provide more context to get better answers. [4]
    • Remember that ChatGPT is not a software engineer—it does not understand nuance, nor can it determine context that it can't find in its training data. It also relies primarily on data that was available on the internet prior to 2021, and cannot search the web for newer content on demand. [5]
  4. While ChatGPT can provide surprisingly good code for an AI chatbot, it often makes coding mistakes that it does not (or refuses to) correct. If ChatGPT's training data contains bad code examples, ChatGPT will interpret those bad examples as truths. Because of this, ChatGPT is not (yet) reliable enough to trust for writing programs on its own. Use ChatGPT as a resource while coding, but not as a replacement for a software engineer (or learning to code yourself).
    EXPERT TIP

    Tyrone Showers

    Technologist
    Tyrone Showers is a Technologist and the Co-owner of Taliferro Group, an IT consulting company based in Seattle, Washington. With over 35 years of professional experience, he specializes in API Design, e-Commerce, Operational Efficiency, and website development. He has a B.S. in Computer Science from DeVry Institute of Technology.
    Tyrone Showers
    Technologist

    Test any code generated by ChatGPT, following best practices in software development. Experts warn against relying solely on ChatGPT responses when coding. Examine ChatGPT's codes to reveal its tendencies and recognize patterns in its output. Use this knowledge to guide GPT with effective prompts and avoid issues.

  5. Advertisement


Expert Q&A

Ask a Question
      Advertisement

      Video

      Tips

      • Your conversations with ChatGPT may be used to improve the quality of the service, so don't divulge anything private. [6]
      • ChatGPT's training data cuts off at 2021. If you're working on a program that requires libraries or coding conventions newer than that, ChatGPT will not provide accurate code.
      • If ChatGPT is at capacity , try refreshing the page, or logging back in another time.


      Submit a Tip
      All tip submissions are carefully reviewed before being published
      Name
      Please provide your name and last initial
      Thanks for submitting a tip for review!
      Advertisement

      Expert Interview

      Thanks for reading our article! If you’d like to learn more about computer programming, check out our in-depth interview with Kevin Burnett .

      About This Article

      Thanks to all authors for creating a page that has been read 85,001 times.

      Is this article up to date?

      Advertisement