ChatGPT for Developers: Efficient Prompt Engineering and Strategies

Arian Garshi
6 min readMay 31, 2023

--

Most of us are well aware of how ChatGPT is a game-changer. It’s powerful, and it’s going to shake things up across many industries. So much so, that many of us are genuinely worried about the future of our jobs. However, my experience has shown that the smart move for us developers is not to fight the change, but to rather embrace it and accept how they are going to change how we work as developers.

As a full-time developer, I use ChatGPT (And Github Copilot) pretty much every single day. In the beginning, I had many frustrations. Both because I had yet to learn the best approach to leveraging this tool, and because ChatGPT was nowhere near as smart as it is now.

Today, I feel much more comfortable relying on ChatGPT. It helps me get more done in less time. I’ve spent some time experimenting with it, and I’ve found ways to make my work smoother and less repetitive. In this article, I’m going to share some of the tricks I’ve learned. I hope these tips will help other developers make the most of this great tool. As I am a front-end developer, most of the examples I use are related to Javascript, but the concepts are applicable to any programming language and context.

leveraging the ability to hold multiple conversations

ChatGPT lets you have multiple ongoing chats simultaneously. This is perfect for organizing different tasks and functionalities. Let's look at an example:

Here, I have multiple conversations that I am using as standalone tools. The first one simply adds JSDoc comments above any function that I write in the conversation. The second one is for rewriting my JS functions to be more efficient. The third one takes in error messages and related code snippets and finds the bugs for me. The fourth one takes in code snippets that I do not understand and explains them in clear language. As you may realize, this makes working with ChatGPT incredibly easy and organized, as I no longer have to keep writing long prompts about what I want it to do again and again. Let’s take a more in-depth look at each of these conversations.

Prompt Engineering

Prompt engineering refers to the practice of constructing prompts or instructions for language models in order to prompt desired responses. It involves crafting specific guidelines, examples, or demonstrations that help guide the model’s generation process.

Prompt engineering is crucial because language models, although highly capable, often require clear instructions or hints to generate accurate and relevant responses. By providing explicit prompts, developers and researchers can shape the behavior and output of the model to align with their desired goals. I have started each of the earlier-mentioned conversations with very specific prompts that sets ChatGPT to different “modes”. We will now take a closer look at the specific prompts that I use.

JSDoc Documenter

JSDoc is a document markup language used in JavaScript to provide a structured and standardized way of documenting code. It allows developers to describe the purpose, parameters, return values, and other relevant information about functions, classes, and variables in their codebase. Although useful, it can be cumbersome to keep writing documentation for every function I make, so to make the process more pleasant, I used the following prompt to automatically generate the documentation:

As a Javascript developer, I will continuously send you functions. I want you to produce JSdoc comments for each one I send in strict accordance to the JSDocs official guidelines.

Here is an example of what the results look like

I simply inserted the function into the text field, and based on the function name and the functionality, ChatGPT reasoned what the documentation should be, and returned the function prefixed with JSDoc. If you really want to impress your project manager, you could go through all of the functions in your project and quickly and easily generate documentation like this to make the codebase more understandable and maintainable!

Function Enhancer

This tool is a favorite of mine. Although always I strive to make clean and minimalistic functions, I still find it helpful to check them with ChatGPT to see if there is room for improvement. And often, there is! what is amazing about this is that I constantly learn how I could be a better programmer by having a super quick and smart code reviewer in my pocket. The prompt I used to set this up is the following:

As a javascript developer, I will continuously be sending you functions that I want you to rewrite so that it is easier to read, less redundant, and as efficient as possible while still keeping the exact same functionality.

Let’s look at an example:

What's great about this is that ChatGPT actually explains exactly what needs to change and why. How one should write for clarity can be subjective, so the explanation makes it easy for you to consider the argument for change, so you can make an informed decision about whether to dismiss the suggestions or if you are better off using the rewritten version of the code.

Code Debugger

Looking for bugs in code is something we all dread. Especially when the bug is caused by super minor and trivial syntax or naming issues (which they often are). This is where the strength of ChatGPT shines through. simply by inserting the bug that the browser and following up with the snippet or even the entire React component that caused the issue, more often than not, it finds the issue and suggests solutions. Here is the prompt I use:

As a developer, I will continuously be sending you error messages prefixed with “Error:” and the relevant code that produced the error prefixed with “Code:”. I want you to examine the code carefully and explain to me exactly what causes the bug. I then want you to rewrite a corrected version of the code. I may ask follow up questions, but when I am done, I will write “finish”. when I do that, you can stop the conversation about that specific scenario and go back to waiting for me to send more code to debug.

As you can see, this prompt is longer and more complex than the others. This is because my experience has shown me that often, I may need to ask more questions about why the bug happens and what other solutions than the ones that ChatGPT suggested are possible. It does not always get the fix right the first time, and I need to pry a bit more and even help lead it in the right direction before a final and correct solution is suggested. Once we reach that stage, I write “finish” to end the conversation so that it is ready for the next bug I encounter.

Code Explainer

This tool has made the process of getting onboarded into new environments and codebases so much easier. ChatGPT is really good at abstracting the logic of any code and explaining it clearly in human language. If I come across something new that I am not familiar with, the first thing I do is copy/paste it into this conversation, and it instantly makes it super easy to understand and work with the code. Here is the prompt:

As a developer, I will continuously be sending you code snippets that I want you to explain to me in a clear and easy to understand language. At the end, let me know if you spot any bugs or redundancies in the code and suggest how I may fix them

What is great about this is that it suggests how one can improve the code. Cleaning up the code as you go through it is a really easy way to impress the project manager of the new project that you are joining!

Conclusion

As you can see, ChatGPT is so much more than a simple chatBot. When used strategically, it becomes a very capable pair programmer that you can have in your toolchain as you progress through your career! The prompts that I suggested are merely a start. Based on your job description and goals, you can create many mini-experts in any sub-area of your workday and make your life so much easier! So, seize the opportunity, explore the potential of ChatGPT, and witness the transformative impact it can have on your professional endeavors.

--

--

Arian Garshi
Arian Garshi

Written by Arian Garshi

Software engineer & UX-designer

No responses yet