How to use ChatGPT to write code

How to use ChatGPT to write code

Greetings, developers!  Have you ever had a case of “coder’s block” where you need a little help or inspiration to write code?  Or need some help understanding code that you inherited from someone else?  Well, you’re in luck!  From generating boilerplate code to debugging or explaining existing code, ChatGPT is a no-brainer way to be a faster, more efficient software engineer.

OpenAI’s ChatGPT falls into the category of generative AI, which is a type of artificial intelligence that generates new or novel content based on data it’s been trained on.  That means ChatGPT can not only write essays, jokes and poems for you; it can also be your pair programmer.

So let’s dive in and see how you can use it to work smarter, not harder.

Table of contents

  • Can ChatGPT write code?
  • Should you code with ChatGPT?
  • Is ChatGPT coding trustworthy?
  • How does ChatGPT help to grow your coding skills?
  • How else can I use ChatGPT as a developer?
  • How to use ChatGPT as a programmer: ChatGPT Prompt Examples
  • Will ChatGPT replace programmers? Can I use ChatGPT to do my job for me?
  • Conclusion

Can ChatGPT write code?

As a large language model, ChatGPT is all about text.  And that text includes—lucky for you!—code.  It has the ability to write code snippets in popular programming languages such as JavaScript, Python, C#, PHP and Java.  Or generate sample CSV input for a data science problem.  Or create bash scripts or infrastructure-as-code templates in JSON or YAML.  And a lot more.

Let’s see an example.  Maybe you need some boilerplate code for a simple index.html page that says “Hello World.”  Using a natural language request, you can turn ChatGPT loose to solve this problem.

ChatGPT generating hello world

Not only does ChatGPT generate some (copy-pasteable) code, but it also explains what the code is doing, which can be especially helpful if you’re new to the language.

Need to style this webpage using some CSS?  Let’s ask!

ChatGPT generating CSS

I know what you’re thinking: “I can use AI to write CSS?  YESSS!  If you tell me it can write regular expressions too, then I’m totally sold.”  (Hint: it can!)

Should you code with ChatGPT?

So.  Just because you can write code with ChatGPT, does that mean you should?  There are a variety of opinions on this.  On one side, people argue that using an AI tool to write code makes you lazy and unable to think for yourself.  On the other side, folks say that leveraging ChatGPT can make you a faster programmer, freeing up your time to focus on the more important things like architecture, best practices and business requirements.

I can see both sides of the argument.  Let’s take a look at some pros and cons.

The pros

  • It’s quick and easy to generate code using ChatGPT.  If you were going to StackOverflow or Google to find a code snippet anyway, why not shorten the amount of time it takes to find it?
  • ChatGPT generally gets syntax right, potentially saving you time chasing compile-time or runtime errors that we humans can introduce if we’re coding from scratch.
  • By using ChatGPT for coding, you might get introduced to some alternative ways of doing things that you wouldn’t have thought about otherwise.
  • ChatGPT makes it faster and easier to learn new languages and concepts, with explanations and code all in the same interface.

The cons

  • While artificial intelligence has come a long way, and ChatGPT is quite frankly mind-blowing, it’s not 100% correct all the time.  It makes mistakes just like us.  And sometimes it’s really confident when it makes those mistakes.  So you still need to check it, test it and debug it-–just like you’ve done in the past.
  • ChatGPT lacks the overall context for what you’re building and why.  Sure, it can give you a code snippet, or even an entire code file.  But it won’t understand conventions or best practices for your company or project.  It doesn’t know how that code will interact with other code.  It doesn’t understand requirements around performance, security, privacy, accessibility and so on.  Again, as a human, you’re ultimately responsible for the code that ships.

Is ChatGPT coding trustworthy?

Let’s see what ChatGPT has to say about its own trustworthiness.

ChatGPT talking about if ChatGPT code is safe

Even ChatGPT is aware of its own limitations!  Just as you wouldn’t rely on your phone’s autocorrect functionality to send an important text message, you shouldn’t rely on ChatGPT for writing perfect code.

Take secure coding, for example.  Remember that ChatGPT works by “learning” a huge set of existing data.  In the case of ChatGPT version 3, it was trained on data through the end of 2021.  Because the cybersecurity landscape is constantly changing, and new vulnerabilities appear every day, ChatGPT won’t have the latest intelligence to inform the code it writes.

ChatGPT has no knowledge of your specific requirements for authentication, secrets management, third-party components, vulnerability scanning and so on.  It might give you boilerplate code with a placeholder for hard-coded credentials.  If you don’t know that those should be stored in an environment variable or in a secrets management service, then this is a huge security risk for your application.

Ultimately, while ChatGPT is good for writing code snippets and simple applications, human developers have the critical job of putting the pieces together and applying best practices.

How does ChatGPT help to grow your coding skills?

When you need to learn a new programming language or feature, what do you do today?  You might search for tutorials, watch courses, try writing and running code to see what it does.  This likely involves jumping around to several different resources, which can be time-consuming.  And how many times do you encounter errors trying to run code you found online?  For me, it’s almost always!

ChatGPT can reduce the amount of time it takes to do all of this.  It effectively aggregates information from the many sources it was trained on, and presents them to you in a single interface.  It also explains what the code does, and sometimes even gives you alternative ways of doing things.  It’s like having a real-time tutor with you as you’re writing and reading the code.

Another huge bonus of learning through ChatGPT is the fact that it uses natural language processing for input and output.  Meaning, just ask questions in plain English, as if you were asking a mentor or a friend.  You don’t have to know exact technical terms or syntax to ask the question.

You can also refine and build out your conversation over time.  If you didn’t quite “get” a concept that it explained, you can ask something like, “Can you explain that in a simpler way?”  Or, “Can you explain that for a 5-year-old?”  Being able to tailor responses to your current level of understanding is a pretty remarkable benefit.

How else can I use ChatGPT as a developer?

So far, we’ve focused on using ChatGPT to write code, but there are several other ways it can make you faster at your job.  For example, it can explain code for you, help you refactor, write test cases, and even fix bugs.

Let’s say you found some code on StackOverflow, but you don’t quite understand what it does.  Let’s ask ChatGPT to help.

Asking ChatGPT about code snippets
Asking ChatGPT about code snippets answer

And voila!  An explanation that can help us better understand the code.

How about getting some help refactoring and cleaning up our code?

More refactoring with ChatGPT
ChatGPT refactoring code

And let’s see one more example of how to get help with unit testing.  We’ll ask ChatGPT to generate unit tests for the Person class referenced above.

ChatGPT writing unit tests

As you can see, there are tons of ways you can use ChatGPT to speed up everyday tasks.  As always, you’ll want to validate the output it gives you, but as a starting point, this is pretty hard to beat.

Check out this short video for additional ways to use ChatGPT as a developer.

How to use ChatGPT as a programmer: ChatGPT Prompt Examples

As AI tools become more and more prevalent in our lives, the way to get an “edge” is to know how to use them effectively.  For ChatGPT specifically, that means you’ll need to be able to write effective prompts to get the answers you want, and quickly.  Here are some templates you can use.

To write new code

Prompt template: Act as a [technical role].  Write [a description of the thing you want to write, using as much detail as you can.]

Example: Act as a front-end developer.  Write code to validate a contact form that includes name, email address and message.  The email address is required and should be in the format of a valid email.  If the form is successfully submitted, display a success message.

To write new code for a specific tech stack

Prompt template: Act as a [technical role].  The tech stack is [describe the different technologies you’re using].  Write [a description of the thing(s) you’re building].

Example: Act as a full-stack developer.  The tech stack is AWS Lambda, DynamoDB, API Gateway and HTML.  Write a Lambda function to calculate the sum of two numbers input by the user and save them to DynamoDB.  Write an HTML file with input boxes for two numbers.  When the user clicks submit, pass the two numbers to API Gateway.

To debug existing code

Prompt template: I am getting the following error in my code [paste in the error].  How can I fix it?

Example: I am getting the following error in my code: Module not found: Error: Can’t resolve ‘./app’.  How can I fix it?

To evaluate different technologies

Prompt template: Act as a [technical role].  Describe two different technologies to [accomplish some task].  Give me pros and cons, along with examples.

Example: Act as a data analyst.  Describe two different technologies to do exploratory data analysis.  Give me pros and cons, along with examples.

Will ChatGPT replace programmers? Can I use ChatGPT to do my job for me?

ChatGPT is very impressive, and it launched the “AI revolution” into overdrive, with new tools and features coming out every day.  And because it’s continuously learning through user feedback and new data, it will continue to get better and better.  Which begs the question: Will ChatGPT take my job?

As a programmer, remember that your ultimate job is to translate business requirements into code. That requires a lot of analysis, creativity, critical thinking, weighing different options, architecting, optimization and so on.  Humans are much better at those skills than robots are, and I don’t think ChatGPT threatens that.

Also, as we’ve seen from the examples above, you need to understand how a technology works in order to ask the right questions and validate the answers that ChatGPT gives you.  In other words, you need to be a skilled developer to effectively use ChatGPT for developer-related things.  But let it do the tedious work that you likely have to search for anyway.

In short, I think ChatGPT will be used to accelerate work, just like IntelliSense, static code analysis, autocomplete, compile-time error checking, automated testing, IDEs and other developer tools have for years.  It’s another tool in the toolkit to make you a better, faster programmer.

Conclusion

And that’s a wrap!  Hopefully, by now, you have a good idea of how ChatGPT can be used to boost your day-to-day software development skills.  Whether you’re a seasoned developer who wants to do less typing, or a newbie wanting to learn a new programming language, ChatGPT is a powerful tool to make it happen.  It’s like having an expert pair programmer at your side (though don’t forget you still need to validate the expert!).

Add a Comment

Your email address will not be published.