Top Ways to Use ChatGPT for Development

Ilyas O.
7 min readJan 10, 2023
A Journey for Developers Using ChatGPT
The possibilities of AI in the future are limitless — Credit: Author via Midjourney

The uses of ChatGPT are endless and impressive, and the capabilities of the platform are extraordinary. You can do almost everything with it virtually. I think this is the first tool that brings the power of artificial intelligence to ordinary folks like me. However, this is just the beginning.

Consider ChatGPT your personal assistant, anything you have in mind try asking it. Whether you need help with copywriting, coding, creating blogs, translation, data classification, checking grammar, or writing books and articles. This tool capabilities are simply limitless.

Next, we will see some examples of how ChatGPT could make your work and life easier.

Your Personal Writing Assistant

I know a lot of developers and software engineers who love writing. You can use ChatGPT in order to help you check errors, grammar or juste rate your article before publishing.

Take a look in the example below:

If you noticed, the paragraph I gave was the one in the headline. For me, it looks efficient and I don’t think I need to add more than that. However, how about a potential improvement? Instead of asking your friends, let’s ask ChatGPT:

You can always ask it for improvements and help for your articles, copywriting and anything in your mind.

Coding

As an example, imagine your employer asks you to get all the tweets data each minute using Spring scheduler with Retrofit as a way to consume the Twitter API.

I will provide examples using Java, as I am a Java Developer, but I will be grateful if you can apply the same concepts with any Technology or Programming language of your choice.

Here is the results:

That’s really a good answer I got from ChatGPT. However, I told it to use a cron expression instead of fixedRate. Also, It did not import everything, but that’s nothing serious, let’s not expect a program to do everything!

I don’t know why, but It did import Retrofit with the version 2.10.0, and that’s incorrect, this version is NOT yet released. The latest one is 2.9.0

Imagine, you got the code you are looking for, but you are not yet convinced because you want to make sure to use Java 17 LTS features while using Lombok.

Honestly, this is really surprising, This latest code looks better than the previous one, and it’s easy to understand. (I didn’t show you the full responses I got, because the Article will be so long)

Still, The code is imperfect and not clean. (There is no perfect code)

Don’t expect a machine to do everything in your behalf, always think about using programming concepts, design patterns…ect, to make your code cleaner and efficient.

Code Explanation and Code Performance

In this section, let’s try to compare between three Java methods and see if ChatGPT is able to distinguish between them and tell us about the most efficient in terms of speed.

Let’s take this method as example, it’s sole purpose is to filter duplicate numbers from a given array, and then return a result without duplicates.


public static int[] filterDuplicates(int[] data) {
var count = 0;
var result = new int[data.length];

for (int element : data) {
var isDuplicateFound = false;
for (var i = 0; i < count; i++) {
if (element == result[i]) {
isDuplicateFound = true;
break;
}
}

if (!isDuplicateFound) {
result[count] = element;
count++;
}
}
return Arrays.copyOf(result, count);
}

Let’s see if ChatGPT can recognize this code:

It’s impressive that it can understand each line of code, this means it can also write similar or even better code. That’s amazing.

In addition, you can ask it about a specific line of code if you feel like you did not get it.

Now, Imagine you had different versions of the method filterDuplicates and you were curious to know which is the better solution in terms of execution speed.

As an example, let’s take the following code and ask ChatGPT which method is the fastest:

// Method 1
public static int[] filterDuplicates(int[] data) {
var count = 0;
var result = new int[data.length];

for (int element : data) {
var isDuplicateFound = false;
for (var i = 0; i < count; i++) {
if (element == result[i]) {
isDuplicateFound = true;
break;
}
}

if (!isDuplicateFound) {
result[count] = element;
count++;
}
}
return Arrays.copyOf(result, count);
}

// Method 2
public static int[] filterDuplicates2(int[] data) {
List<Integer> list = new ArrayList<>();
for (int value : data) {
if (!list.contains(value)) {
list.add(value);
}
}
return list.stream().mapToInt(i -> i).toArray();
}

// Method 3
public static int[] filterDuplicates3(int[] arr) {
Set<Integer> set = new HashSet<>();
List<Integer> duplicates = new ArrayList<>();
for (int i : arr) {
if (set.add(i)) {
duplicates.add(i);
}
}
return duplicates.stream().mapToInt(i -> i).toArray();
}

Of course, the first method will win in terms of execution speed. But, I didn’t expect I will get this type of response from ChatGPT. That’s incredible!

Besides its ability to explain code, it’s also capable to improve it, rewrite it and also simplify it.

Unit Testing

You all know the great value about unit testing, by improving the quality and reliability of code. Without it your code is INCOMPLETE.

Let’s create a unit test from a given method that returns the closest number to 0 from an Array.

public int computeClosestToZero(int[] array) {
if (array.length <= 0) {
return 0;
}
int min = Integer.MAX_VALUE;
for (int element : array) {
if (Math.abs(element) <= Math.abs(min)) {
min = element;
}
}
return min;
}

Here is the result:

Simply put, this is a pretty good result. You can also use different Testing Frameworks such as Mockito, ect., you only need to specify to ChatGPT the thing you want to implement exactly.

In case you want ChatGPT to help you implement unit tests, but you have a big architecture where each Class depends on other Classes. You will need to provide it with all necessary information to produce a good unit test output.

Thus, use it wisely as a tool, Don’t expect it to write everything for you.

Still, I’m curious to know how ChatGPT use the code we send. Does it store it somewhere and use it when needed?

Writing documentation

Asking ChatGPT to write documentation for a piece of code usually yields good results. It even includes comments for each line of code.

Don’t let ChatGPT or any upcoming tool to use you or get on you. I believe in the near future there will be many tools like this and some people will lose their Jobs.

I surely don’t trust ChatGPT’s output, but we need to continually learn new things and improve our skills by using it. It will be great for us to NOT use such a tool for any code we want to implement. Don’t rely on it too much or one day you will find yourself just doing copy-paste without improving yourself.

USE IT WISELY, to improve your productivity

I personally believe that a machine can’t replace a Human, because it doesn’t have the ability to feel and understand people feelings, vision and dreams. We all need to figure it out by how to exploit these tools to our benefit and success.

--

--