Prompt ChatGPT to Excel - 5 Proven Code Generation Tips

By Nick Hauenstein + ChatGPT

I conducted an "experiment" of sorts to explore the capabilities of ChatGPT, a prominent chatbot and large language model created by OpenAI. I must disclose that my inquiry did not conform to a standard experimental design, lacked controls and rigorous procedures, and consequently did not support the repeatable examination of my findings -- which would not strictly be repeatable anyway with a non-zero temperature. My primary objective was to test the extent to which ChatGPT could develop a complex app. While I found that its abilities were somewhat limited in this regard, I did discover the importance of using specific prompt engineering strategies when interacting with ChatGPT for this purpose. Therefore, the main purpose of this article is to share those techniques that can help you make the most out of your experiences with ChatGPT when applying it to the task of code generation, or reasoning about code.

If you just want to play with the apps it generated and see the prompts used to generate them, click here.

Disclaimer: Don't try this at work unless the lawyers and compliance folks say it's okay. I'm all for exploring new things, but I also try to balance that with not getting sued or losing my job. So, let me be clear: if you're thinking about trying this out at work, you better make sure the lawyers and compliance folks are on board with it. I'm not a legal expert (and even if I were, I probably wouldn't be very good at it), but I do know that there are all kinds of laws, regulations and policies that might apply in this context. I don't want you to get in trouble just because you read this article and thought it sounded like a good idea. So, don't try this at work.

Tip 1: Know the limits

Now, let's talk about the limitations of ChatGPT. It's important to note that, as advanced as this chatbot may be, it still has its boundaries. One of the most significant limitations to be aware of is the 4,000-token restriction on conversation history. Essentially, this means that ChatGPT can only remember so much of what you've discussed before. Furthermore, you can't always expect ChatGPT to remember method signatures or exact identifiers from previous interactions. It's possible that the bot's responses may not always be as accurate or precise as you need them to be. Therefore, it's vital that you include all necessary context inline with the prompt to help ChatGPT generate the most appropriate responses possible. This can help ensure that you get the best outcomes from your chats with ChatGPT, and ultimately make the most of this powerful tool.

Tip 2: Be clear in your intentions

It's crucial that you're clear with ChatGPT about what you want it to do. Specifically, you need to ensure that you're asking ChatGPT to provide you with code - not to build an application, and not to give you instructions on how to build an application. If you're not explicit about your intentions, you may end up getting a long-winded explanation about how to create an app from scratch - even if that's not what you were looking for! In the worst-case scenario, you may even get a message from ChatGPT saying something like, "I'm just an AI language model, I don't have the ability to build apps myself." While this may be true, it's certainly not what you want to hear if you're hoping to get some coding help from ChatGPT. So, remember to keep your prompts focused and specific - this will help you get the most out of your chats with this cutting-edge chatbot. As a note to the reader, while this paragraph was generated based on my notes, I never told ChatGPT to refer to itself as a "cutting-edge chatbot".

Tip 3: Do the decomposition work for ChatGPT (classes/functions/files)

Remember there is a 4,000 token limit to what ChatGPT can reason about while creating an app. As a result, you will have to break the problem down into chunks that can be reasoned about independently. This will mean applying the same skills that you do when it's just you the human writing code. Inform ChatGPT that you have broken the code down across specific files with certain names. Ask ChatGPT to sketch out the outline for class rather than writing the full implementation. Ask ChatGPT to implement a function/method at a time after that. Decomposing keeps you within limits and prevents ChatGPT from "hallucinating" method signatures, identifiers, selectors, etc... that don't actually exist in the relevant files. If you end up with long code snippets being generated and then getting cut-off, you can simply say "continue" for ChatGPT to continue generating the rest of them.

Tip 4: Specify complete details for every important UI element

When it comes to working with ChatGPT to design a user interface, you'll want to be very detailed and specific about what you're looking for. This means specifying exactly what kinds of elements you want to include - buttons, text fields, dropdowns, and so on - as well as giving each of them clear and meaningful labels. Additionally, you'll want to provide ChatGPT with information about validation logic (e.g., making sure users enter valid email addresses) and visibility logic (e.g., showing certain elements only when certain conditions are met). And don't forget to explain the purpose of each element, too - this will help ensure that ChatGPT is generating UIs that are tailored to your specific needs. Finally, if you want to use any specific libraries or dependencies - such as React or Bootstrap - be sure to call them out so that ChatGPT can work with them as needed. By taking the time to be detailed and specific in your prompts, you can help ensure that ChatGPT is generating user interfaces that are perfectly tailored to your requirements.

Tip 5: Ask ChatGPT to use a knowledgeable persona

As you probably know, language models like ChatGPT are incredibly powerful - but they're not infallible. In fact, sometimes the bot's responses can be downright confusing or unhelpful. This is particularly true if you're not clear about what kind of knowledge or expertise you're asking for. For instance, if you ask ChatGPT a question like "What is 3/7 chicken, 2/3 cat and 2/4 goat," you might not get the response you're hoping for - after all, ChatGPT doesn't know what you're talking about! But, if you take the time to ask the bot to "immerse yourself in the persona of an expert of riddles named 'The Riddler'" who is skilled in wordplay, logic, deduction, and mathematics, you might just get a response that blows you away. In fact, as The Riddler, ChatGPT might just tell you that the answer is "Chicago" - because as a riddle expert, it expects all questions to be riddles!

Similarly, if you're working on an app that requires specialized knowledge or expertise, it can be helpful to ask ChatGPT to take on the persona of someone who is knowledgeable about that specific topic. For example, you might ask ChatGPT to pretend to be a student of a certain computer scientist if you're working on an app that requires knowledge of their work. By doing this, you can help ensure that the responses you get from ChatGPT are accurate, insightful, and directly relevant to your project. Overall, by taking the time to set the right context and give ChatGPT the right cues, you can unlock the full potential of this powerful tool - and have some fun while you're at it!

Conclusion

It's true - ChatGPT wasn't explicitly designed to write code. However, that doesn't mean it can't be incredibly helpful when it comes to programming tasks. In fact, many large-language models have shown real promise in this area, and with the right approach, you might find that ChatGPT can be a valuable ally in your coding efforts. By taking the time to apply best prompt engineering practices and guiding ChatGPT in the right way, you can unlock the full potential of this chatbot and get results that you might not have thought possible.

Of course, even with the best guidance and prompts, ChatGPT isn't a magical solution that will solve all your coding problems overnight. There will still be times when you need to rely on your own expertise and experience to write effective code. But even when things don't go perfectly, using ChatGPT can be a fun and engaging way to work through tough coding challenges. After all, it's certainly more interesting than talking to a rubber duck or staring at your screen for hours on end! So why not give it a try and see what kind of creative solutions you can come up with?

Share this Article