Software Development Exam  >  Software Development Notes  >  ChatGPT for Students  >  How to Use ChatGPT to Write Code

How to Use ChatGPT to Write Code | ChatGPT for Students - Software Development PDF Download

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.

How to Use ChatGPT to Write Code | ChatGPT for Students - Software Development

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!

How to Use ChatGPT to Write Code | ChatGPT for Students - Software Development

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.
How to Use ChatGPT to Write Code | ChatGPT for Students - Software DevelopmentEven 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.
How to Use ChatGPT to Write Code | ChatGPT for Students - Software DevelopmentHow to Use ChatGPT to Write Code | ChatGPT for Students - Software DevelopmentAnd voila!  An explanation that can help us better understand the code.

How about getting some help refactoring and cleaning up our code?
How to Use ChatGPT to Write Code | ChatGPT for Students - Software DevelopmentHow to Use ChatGPT to Write Code | ChatGPT for Students - Software DevelopmentAnd 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.
How to Use ChatGPT to Write Code | ChatGPT for Students - Software DevelopmentAs 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.

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.

The document How to Use ChatGPT to Write Code | ChatGPT for Students - Software Development is a part of the Software Development Course ChatGPT for Students.
All you need of Software Development at this link: Software Development
22 videos|29 docs|3 tests

Top Courses for Software Development

FAQs on How to Use ChatGPT to Write Code - ChatGPT for Students - Software Development

1. Can ChatGPT write code?
Ans. No, ChatGPT is not designed to write code directly. It can assist programmers by providing suggestions, answering questions, and helping with problem-solving, but it does not have the ability to write code on its own.
2. Should you code with ChatGPT?
Ans. Coding with ChatGPT can be a helpful tool for developers, especially when seeking guidance or brainstorming ideas. However, it is important to note that ChatGPT is an AI model and may not always provide accurate or optimal code solutions. It is recommended to use ChatGPT as a supplementary tool rather than relying solely on it for coding.
3. Is ChatGPT coding trustworthy?
Ans. While ChatGPT can offer valuable insights and suggestions, its coding advice should be evaluated critically. ChatGPT's responses are generated based on patterns it learned from training data, which may not always guarantee trustworthy or secure code. It is essential to review and validate any code generated by ChatGPT before implementing it.
4. How does ChatGPT help to grow your coding skills?
Ans. ChatGPT can assist in enhancing coding skills by providing real-time feedback, suggesting alternative approaches, and offering insights into coding best practices. By interacting with ChatGPT, developers can learn new techniques, gain different perspectives, and improve their problem-solving abilities.
5. How else can I use ChatGPT as a developer?
Ans. Apart from coding assistance, ChatGPT can be used as a developer in various ways. It can help with documentation, language-specific queries, debugging assistance, code review suggestions, and even generating code snippets. However, it is important to remember that ChatGPT's responses should be carefully reviewed and validated to ensure their accuracy and reliability.
22 videos|29 docs|3 tests
Download as PDF
Explore Courses for Software Development exam

Top Courses for Software Development

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

Objective type Questions

,

ppt

,

Viva Questions

,

Extra Questions

,

How to Use ChatGPT to Write Code | ChatGPT for Students - Software Development

,

pdf

,

Semester Notes

,

Free

,

MCQs

,

Exam

,

How to Use ChatGPT to Write Code | ChatGPT for Students - Software Development

,

How to Use ChatGPT to Write Code | ChatGPT for Students - Software Development

,

study material

,

Sample Paper

,

Summary

,

shortcuts and tricks

,

Important questions

,

practice quizzes

,

video lectures

,

mock tests for examination

,

past year papers

,

Previous Year Questions with Solutions

;