Technical · AI

Using ChatGPT in my daily Job as Software Developer.

February 17, 2023

For the last couple of weeks, I have been using ChatGPT (the Artificial Intelligence Text Generator) for some of my tasks as Software Developer on multiple projects. This article shows some conclusions on the good and bad results and some recommendations to fellow developers.

ChatGPT

Unless you have been living under a rock (or their equivalent, out of social media, news cycles, or even basic internet conversations), you should know by now that “ ChatGPT (Chat Generative Pre-trained Transformer[2]) is a chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI’s GPT-3 family of large language models and has been fine-tuned (an approach to transfer learning) using both supervised and reinforcement learning techniques.” Wikipedia

A super chat that answers questions by producing text using statistical models. You produce a question on natural language like: “What is the distance between the earth and the moon?” And ChatGTP will produce a text like this:

“The average distance between the Earth and the Moon is about 238,855 miles (384,400 kilometers). This distance can vary slightly depending on the Moon’s position in its elliptical orbit around the Earth. At its closest point (perigee), the Moon can be about 225,623 miles (363,104 kilometers) away from the Earth, while at its farthest point (apogee), it can be about 251,000 miles (405,500 kilometers) away.”

This is a clear improvement over an Internet Search presenting a list of web pages and then you need to check those pages and build your paragraph for that response.

Because of the size of the training model and the accurate results, people have commanded ChatGPT to build multiple types of text, from marketing and poems to writing code.

I have been using ChatGPT on some daily tasks as a Software Developer.

Case I. C# Multithread

I ran a few tests of writing a simple program with ChatGPT and here are some notes.

I started with this prompt:

“How to write on a text file from multiples processes without blocking the file, using c#”

And I got this.

using (FileStream fileStream = new FileStream("file.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
{
using (StreamWriter writer = new StreamWriter(fileStream))
{
writer.WriteLine("This is a test.");
}
}

With a nice explanation of key elements like FileShare.ReadWrite

Also, it has provided a sample using Mutex

using (Mutex mutex = new Mutex(false, "MyFileMutex"))
{
mutex.WaitOne();
try
{
// Access the file here
}
finally
{
mutex.ReleaseMutex();
}
}

Those should work but my current code is using File.AppendAllText and I don’t want to change my IO strategy in the program, so I change the question to be more specific:

“How to write on a text file from multiples processes without blocking the file, using c# and the appendAllText”

Then I got this

File.AppendAllText("file.txt", "This is a test.");

And again, the mutex sample

using (Mutex mutex = new Mutex(false, "MyFileMutex"))
{
mutex.WaitOne();
try
{
File.AppendAllText("file.txt", "This is a test.");
}
finally
{
mutex.ReleaseMutex();
}
}

Now, besides the simple fact of the code being written and that It saves me to read a few websites or StackOverflow answers, the most impressive aspect of ChatGPT is that it understands the context of the conversation so when my next prompt was

“After using FileStream and StreamWriter, I don’t see the changes on the text file in realtime on windows explorer or opening the file”

ChatGPT was able to understand what we were talking about and keep working on the same line. Providing an explanation and even a new sample

using (FileStream fileStream = new FileStream("file.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
{
using (StreamWriter writer = new StreamWriter(fileStream))
{
writer.WriteLine("This is a test.");
writer.Flush();
}
}

And alternatives

using (FileStream fileStream = new FileStream("file.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
{
using (StreamWriter writer = new StreamWriter(fileStream))
{
writer.WriteLine("This is a test.");
}
fileStream.Flush(true);
}

And then again when that test was hard to confirm and the next prompt was only

“New lines are not appended at the end of the file.”

ChatGPT was able to follow it and provide yet another sample.

using (FileStream fileStream = new FileStream("file.txt", FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
{
using (StreamWriter writer = new StreamWriter(fileStream, Encoding.UTF8, 1024, false))
{
writer.WriteLine("This is a test.");
}
}

And back to one of the original elements of the question, a sample with File.AppendAllText.

File.AppendAllText("file.txt", "This is a test." + Environment.NewLine);

Is it smart? no, it is not.

Does the code make sense? some code makes sense, some doesn’t. (on a small note ChatGPT was very confused labeling the code, even though it was always C#, sometimes it has been labeled as Java or Python)

And as with any “smart” random text generation, the presented option could or couldn’t be the most appropriate for the task at hand.

It would be your job as a developer to know what the conversation is about and keep pushing in a direction that works for you,

For example, we ended this conversation with a better suggestion by asking.

“What about using ReaderWriterLock, can you provide an example to write a file not locking it from multiple processes ?”

ChatGPT responded with

“Yes, you can use the ReaderWriterLock class in C# to write to a file from multiple processes without blocking the file. The ReaderWriterLock class allows multiple readers to access a file simultaneously while providing exclusive access to a single writer. Here is an example of how you can use the ReaderWriterLock class to write to a file from multiple processes:”

class FileAccess
{
private static ReaderWriterLock _rwLock = new ReaderWriterLock();
public void WriteToFile(string fileName, string text)
{
_rwLock.AcquireWriterLock(Timeout.Infinite);
try
{
// Write to the file
File.AppendAllText(fileName, text);
}
finally
{
_rwLock.ReleaseWriterLock();
}
}
}

Again, with a nice explanation of key elements.

In this case, it would be the developer's job to understand the pros and cons of the proposed ChatGPT solution and keep guiding the ChatGPT to an answer that provides a better solution per the requirements.

ChatGPT was somehow useful, but their initial responses weren’t usable, the prompt was modified to help to find more accurate responses and new direction was provided.

ChatGPT-generated code wasn’t anything close to ready for Production as it should be understood as a sample and in some cases, not even a functional sample.

Again, ChatGPT doesn’t understand what the code is and just creates what probabilistically should be a math solution for the next word. if there is enough good content for that case then the solution could be appropriate.

Case II. Just Commands.

In the last couple of weeks, I have replaced Google Search with ChapGPT for anything like query syntax, or OS Commands. I found great value as almost every time it goes straight to create the right solution, and it saves me a lot of time reading manuals, reference documents or even opening my own cheat sheets

I.e, with this prompt

“Write a query to update table account_details, field account_unique_id with content from table accounts, field unique_id using postgresql”

I got this

UPDATE account_details
SET account_unique_id = accounts.unique_id
FROM accounts
WHERE account_details.account_id = accounts.id;

Not just a valid solution but with my very own tables and column names.

Another example,

With this prompt

“Can you write a group by query in PostgreSQL, using date as group criteria on a time date field?”

I got this

SELECT date(timestamp_field) as date,
sum(some_field) as total_sum
FROM your_table
GROUP BY date
ORDER BY date;

or, OS commands like

“Write a command to flush DNS cache on mac”

ChatGPT has produced this

sudo dscacheutil -flushcache

or

“How to force flush DNS on a windows operative system computer”

ChatGPT has produced a full step-by-step with commands:

  1. Open Command Prompt as an administrator by pressing the Windows key + X and selecting “Command Prompt (Admin).”
  2. Type “ipconfig /flushdns” (without quotes) and press Enter.
  3. You should see a message that says “Successfully flushed the DNS Resolver Cache.”

Alternatively, you can also use the Windows PowerShell, by running the following command:

  1. Open PowerShell as an Administrator
  2. Type “Clear-DnsClientCache” and press enter.

This will clear the DNS cache on your computer, and it should take effect immediately.

Another Query to ChatGPT

“How to force chrome browser to reload DNS”

and again ChatGPT has produced a full set of instructions to achieve that goal.

To force Chrome browser to reload DNS, you can use the following steps:

  1. Open Chrome browser.
  2. Type “chrome://net-internals/#dns” into the address bar and press Enter.
  3. Click on the “Clear host cache” button.
  4. A message box will appear saying “Successfully cleared the DNS cache.”

Alternatively, you can also try to clear the browser’s cache and cookies, which also may help to reload the DNS.

  1. Click on the three dots on the top right corner of the Chrome browser
  2. Click on More Tools and then Clear Browsing Data
  3. Select ‘Cached images and files’ and ‘Cookies and other site data’
  4. Click on Clear Data

This will clear Chrome’s DNS cache, and it should take effect immediately. Keep in mind that clearing the cache and cookies will remove all your browsing data, so make sure to save any important information before doing this.

And here I can see a real use case and companion for ChatGPT in my daily routines as a Software Developer.

ChatGPT can write commands, instructions, and fragments in a very effective way, with the right input it will generate not standard website samples type of sentences or commands, but commands with my very own data cases.

Conclusions

After using ChatGPT for a few weeks on my daily Software Development tasks, I can replace Google search for anything related to commands, manuals, instructions, or small fragments of code with ChatGPT

With guidance and multiple interactions, ChatGPT was able to write a few classes that can be used as a starting point for some projects, but the final decision, guidance, and understanding of related and side effects will stay on the Developer side.

Some of the code generated by ChatGPT wasn’t useful at all, and in many cases, it has the wrong labels on it (i.e., python instead of C#)

I would continue using ChatGPT on my development task and it would probably replace 75 to 95% of google searches on any given day.

Ethical considerations.

I believe that AI requires a very deep ethical analysis. I am not an expert on either AI or Ethics but here are some items a layman can dream on:

ChatGPT (or any other text generation tool) should be able to identify the sources of the content generated. This could be difficult and based on the way the model is trained people may say it is not possible. But when the content of the generated text has more than 5% of the content from a web page, post, blog, book, or conversation. It should be listed there.

I don’t like a future when people just trust whatever this mathematical model display as true, without having the opportunity to explore the sources and decide on their own.

On this same line, creators should be recognized as such, and not just use their work to create new content, just like what happens with other human beings using other people’s work.

All content generated by ChatGPT (or any other generation tool) should be labeled as such and ideally not used to train additional models. It makes no sense to me a future when all content is generated without an understanding of the context and only using statistics and math. Models should be trained by humans generated content (bad or good)

Some measurements need to be in place to prevent bad actors to influence the model by creating content or promoting content that is fake and altering the results of the model.

Poetry and other creative content supposedly based on human experiences and created by ChatGPT should be clearly labeled as not human. There is nothing beautiful in a poem built on a math formula with words placed not by emotion but by the statistical significance of their position on a sentence.

Originally published on Medium in 23blocks.

Related reading

More in the Digest — a weekly roundup of AI and agents news — or start with AI Strategy.