Rather watch this as a video? Click here to view
You might be thinking: another article about ChatGPT? Really?
There's been no shortage of press dedicated to this tool, but let's get specific. Let's talk about how you can use it as a software developer.
I've been using this tool since its release. The output has been outstanding. It has made me better at software development. That's right, this tool has improved my work tremendously. Is it because now I generate all my code with ChatGPT and work 10x faster?
No.
But it has made me more productive. So I will share some tips with you that will help you learn more and leverage this tool for your software work.
What is ChatGPT?
Generative Pre-trained Transformer 3, or GPT-3, is a language processing model developed by OpenAI. ChatGPT is a GPT model trained on conversational data. It's designed to be a hyper-intelligent chat bot. It uses articles, research papers, books, documentation, and more to predict answers to questions. We'll get into that a bit more.
You can try out ChatGPT here for free.
If you're wondering what all the hype is about, you can sign up and immediately use it through an easy-to-use chat interface:
No, I didn't use ChatGPT to write this article. It's written by a human being (this time). However, I did have ChatGPT write an application for me the other day. I'm impressed.
If you want to learn more about ChatGPT and how it works, check out this awesome course on ChatGPT by Amber Israelsen. It covers what you need to know to understand and use it better.
What it is
You've probably heard about people using ChatGPT to generate content, rewrite their bios, or come up with catchy names for a company. It's great at that. Many folks have been using it as a supercharged search engine.
ChatGPT thinks like a person and speaks like a machine. It learns from words and predicts the next word or set of words. This is an important distinction because many think it is a giant database, or "Google on steroids." It's not. It's a much different tool for another purpose: predicting text.
Put simply:
ChatGPT doesn't do an internet search for answers; It predicts Answers based on its training data - Amber Israelsen
That's great news for you as a developer. If you're searching for the meaning of an error message, Google and Stack Overflow are still your best bet. However, if you want to know, "what's a better way to write this?" ChatGPT is the tool to use. Let's explore that deeper.
How to use it as a developer
You can extract valuable information from ChatGPT using prompt engineering. Asking the tool what you want from it can be tricky, but changing the questions will improve your answer. Let's look at some prompts we can use to improve our code.
Prompt 1: What's a better way to do this?
This is one of my favorite prompts, so I'm listing it first.
After a few years, you're dusting off your C# skills and are writing an application. You create a vehicle class with a few properties:
public class Vehicle
{
private int _numberOfWheels;
private string _typeOfCar;
private string _color;
public int NumberOfWheels
{
get
{
return _numberOfWheels;
}
set
{
_numberOfWheels = value;
}
}
public string TypeOfCar
{
get
{
return _typeOfCar;
}
set
{
_typeOfCar = value;
}
}
public string Color
{
get
{
return _color;
}
set
{
_color = value;
}
}
}
Is this valid code? Sure. It uses getters and setters, and you can implement data protection on your properties. It will compile and work just fine.
However, this isn't how we implement properties in C# anymore. But you have no way of knowing that, especially if you haven't written C# since version 3.0.
So out of curiousity, you go into ChatGPT and paste in the following:
What's a better way to write this class? <insert code here>
ChatGPT takes this prompt and spits out the following:
Not only does it tell you about auto-implemented properties, but it tells you why you should use this suggestion. Now you know that you can perform the same thing that 41 lines of code used to do in just six lines of code. It tells you why that's important. You just learned something.
Let's look at another example.
You have an array of data in Python that you'd like to iterate through. You write the following code to do it:
my_array = [1, 2, 3, 4, 5]
for i in range(len(my_array)):
print("The value at index", i, "is", my_array[i])
Does it work? Yes. It makes sense, and it is pretty concise. But is there a better way?
What's a better way to write this <insert code here>
ChatGPT returns this:
While less drastic than the first example, you just learned a new way to do something and why it matters.
Prompt 2: How do I (do something)
This is yet another way to use ChatGPT. However, there is a caveat attached. You can always ask it how to do something:
how do I connect to postgresql with php?
And for many common things, it will usually spit out an answer that's correct.
You can have it generate a variety of great boilerplate code for various tasks.
Not only will it write the code for you, but it will explain the reasoning behind it:
This can be incredible if it's a concept you are unfamiliar with. ChatGPT can generate things like this much faster than a Google query.
As I said earlier, I pretended to know nothing about Go, JavaScript, and HTML and created an entire web application with ChatGPT. Even if I knew little about these technologies, I could assemble an application using ChatGPT as the primary resource.
But what's the caveat I mentioned earlier?
You must understand the code you are generating.
I can't stress this enough. Don't blindly say, "how do I do this" and dump the code into your application. Generate the code, study it, and understand it. You can also use ChatGPT for this, as we'll see with the next prompt.
Prompt 3: How does (this) work in the code you just generated?
You can dig deeper into what ChatGPT generates with this prompt. ChatGPT keeps a conversational context by bundling your queries in a single conversation. That means you can keep iterating on the results, just like in a real conversation with a human.
This gives you a deeper explanation of how things work before you use them.
This is one example of how ChatGPT has made me a better developer. I have gone down some rabbit holes for sure and gained a deeper understanding of the things I'm working on. I suggest doing this. Especially if you are generating code you don't fully understand.
Prompt 4: Rewrite this code with syntactic sugar
This is another one of my favorites. If you don't know, Syntactic sugar is programming language syntax that is concise and (sometimes) easier to read and express. It's not for everyone, but I like it, especially when it de-clutters my code.
Let's take this example in C#:
int? nullableValue = null;
int value;
if (nullableValue.HasValue)
{
value = nullableValue.Value;
}
else
{
value = 0;
}
In this code, we're looking for values that may be null, and if they are, we'll assign them a value of 0. If they have a value stored, we'll pass that value.
Pretty straightforward, right? But one look at this code tells you there's a shorter way to do this. So we'll ask ChatGPT:
And we get this, which is much better:
int value = nullableValue ?? 0;
This is yet another way ChatGPT can teach you and help you be a better programmer. Keep in mind this isn't bulletproof. I have seen some code generated using this that didn't work as expected. But it's pretty cool when it does!
Summary
ChatGPT is cool and a lot of fun to play with. However, it can also be a useful tool to improve productivity, as we've shown. It can teach you new ways to write code.
We didn't cover some of the other ways it helps, such as generating documentation, providing answers to support questions, and more. I'll share a follow-up article to this, so keep checking back.
It might sound like a dumb fad, but it isn't. From here forward, developers need to learn this tool. This is a significant turning point for software development, and I can only see it improving.
Check out this great course if you'd like to learn more about ChatGPT.
Questions, comments? Let me know!