Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon

ChatGPT for Coding

Save for later
  • 6 min read
  • 25 Apr 2024

article-image

Dive deeper into the world of AI innovation and stay ahead of the AI curve! Subscribe to our AI_Distilled newsletter for the latest insights. Don't miss out – sign up today!

Introduction

ChatGPT's coding style is terrible:

  • Verbose
  • complex
  • and outdated.

Let's change that.

ChatGPT promised to be our coding savior, but sometimes it feels more like a blast from the past.

Remember those early 2000’s coding books? Yep, it's giving those vibes.

It's like having a sports car with a tractor engine. Great potential, but the performance? Not quite there.

Imagine harnessing the power of ChatGPT but with the finesse of a master coder.

Ready for the upgrade?

Here are 12 Pro prompts that will get you the right results.

Tip #1: Specificity is the king

As soon as you ask for some coding snippet from ChatGPT, by default, you will get the most basic HelloWorld example.

The more vague your prompt is, the more mediocre your results will be

Instead, specify exactly

  • language
  • version
  • framework
Write backend code for Library app that uses Rest to communicate
Cover endpoints for adding, removing, and filtering books by category and date published

Use Java latest version. 
Use lambda streams instead of for loops
Use Spring framework

Tip #2: Avoid code vomit

ChatGPT loves to write a lot of code, the way I like to call it “code vomit”

We are no longer rewarded by the amount of code we produce, but by the clarity and principles we follow.

Give chat GPT instructions to

  • write clean code
  • use latest principles
  • cover logging and exception handling
Write clean code
Code needs to be covered with Logging and proper exception handling
Use  principles: Kiss & DRY, SOLID
Keep in mind to use design patterns where it is applicable

Using coding instructions I gave you, give me code for each class

Tip #3: Make it easy to use with IDE

Every time ChatGPT writes code you get

  • explanations
  • import statements
  • comments.

This can be good for a beginner but is not something we need for our IDE

Our IDE is already good with importing all the right packages, so let ChatGPT know

When writing code, avoid detailed explanations, just simple bullet points
Don't add import statements, as IDE will do this instead

Tip #4 Write tests

Your code is not complete if you are not done with tests.

But not just any tests. We want to have unit and integration tests in a

  • readable format (give when then)
  • covering the happy and unhappy path
  • uses the latest testing libraries such as AssertJ and BDDMockito
For each class write a unit and integration tests
Use given when then format
For libraries use BDDMockito and AssertJ
Cover happy and unhappy paths 

Tip #5 Give REST call request examples

What is the app if we cannot test it without some examples

Instead of creating them manually, ask ChatGPT to create Curl examples we can easily copy to Postman.

For each request, generate curl examples

Now go ahead and use your terminal or copy/paste them to Postman

Unlock access to the largest independent learning library in Tech for FREE!
Get unlimited access to 7500+ expert-authored eBooks and video courses covering every tech area you can think of.
Renews at £16.99/month. Cancel anytime

Tip #6 Create documentation

We don’t want just plain text, instead, we need a quick start guide for developers

Write a quick start guide for developers using markdown.
Imagine this app has been published to github repository

Cover
- Introduction
- how to install app
- how to run it
- how to use it

Tip #7 Prepare deployment script for Cloud

This app cannot live just in your local environment. Instead, we need a deployment script.

Depending on where you want to deploy your changes, it might be

  • Kubernetes cluster script
  • Google-specific terraform scripts
  • AWS cloud formation script
  • Azure-specific deployment script

Or ask ChatGPT to suggest a deployment script

Provide me deployment script for one of most popular cloud providers

Tip #8 Version Control

Our code for now is living only locally. Let’s ask chatGPT to give us instructions on how to set up Version Control

Provide Github version control setup instructions

Tip #9 Define CI/CD pipeline

CI/CD or continuous integration and continuous deployment is a must-have step for any serious development.

There are plenty of options to choose from, such as

  • Jenkins
  • GitHub actions
  • Bamboo

With CI we guarantee we can

  • safely merge our changes by running build and tests
  • check if our code changes comply with sonar policies

With CD we guarantee that we can safely deploy our changes

Provide github actions that for each open pull request we 
run the build and run all the tests
Also automatically include sonarqube scans
Also create github action to run deployment on every code merge

Tip #10 Performance optimization

Our backend rest service is now running, but the question we need to ask ourselves

  • how fast is it
  • how many requests it can handle
  • what is the maximum limit of requests

For that, we need to execute performance tests, e.g. using jmeter or gatling.

We need to test what is the limit of our app.
Write a load test script for gatling that tests how many book searches we can execute

Tip #11 Run a security audit

How can we ensure our app is secure and not open to any threats?

The best way is to run security scans.

Our application might be open for security threats.
Which security scan tools we can use for free and how can we use them.
Give me step-by-step instruction on how to use it.

Tip #12 Optimize for observability

You have your app running somewhere in the cloud.

But did you optimize it for observability?

  • How can you easily troubleshoot issues?
  • How can you trace requests between different services?
  • Did you set up monitoring
We want to make sure our application is optimized for observability
Create guideline and configuration for the cloud environment for
Traceability - tracing request from start to finish
Monitoring - monitoring key performance metrics
Logging - have a centralized logging system

Conclusion

You can find the full prompt here

https://chat.openai.com/share/f0bef1ca-062d-4a22-96aa-9711615329a5

ChatGPT is a tool, and like any tool, it shines when used the right way.

With these prompts, you get a coding assistant that keeps up with the latest trends, ensuring your code is not just functional but also follows modern standards.

Author Bio

Jakov Semenski is an IT Architect working at IBMiX with almost 20 years of experience.

He is also a ChatGPT Speaker at the WeAreDevelopers conference and shares valuable tech stories on LinkedIn.