ChatGPT and how to use it as a software engineer
ChatGPT created by OpenAI is a computer program that uses artificial intelligence to understand and generate human-like responses to text inputs. It can engage in conversations, answer questions, and provide information on a wide range of topics. Think of it as a chatbot that is trained to have a deep understanding of language and the ability to communicate in a natural and human-like way.
ChatGPT can write pretty good code. This is both exciting and scary. How can you use ChatGPT to write code as part of your daily coding practice? That’s what we’re going to explore.
Using ChatGPT as a learning tool
If you are starting to use a new tool or language or framework, ChatGPT is a good place to start. I am impressed with the extra detail it gives when generating responses, giving additional steps on what todo with the code or replacing static strings with actual values for it to work with your application, like in the uploading files to an S3 Bucket example further down. Let say, you have never used the programming language Java before:
Question: “write a java program that returns hello world”
START RESPONSE:
Sure! Here’s a simple Java program that prints “Hello, World!” to the console:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
To run this program, you can save it as a file called HelloWorld.java
, and then compile and run it using the following commands:
javac HelloWorld.java
java HelloWorld
The first command compiles the source code into bytecode, while the second command runs the compiled program. You should see the message “Hello, World!” printed to the console.
END RESONSE
Another example is you have never used AWS Lambda functions before, you can ask ChatBPT:
Question: “write a java program for AWS lambda that returns hello world”
START RESPONSE:
Sure! Here’s a simple Java program for an AWS Lambda function that returns “Hello, World!” as a string:
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class HelloWorld implements RequestHandler<Object, String> {
public String handleRequest(Object input, Context context) {
return "Hello, World!";
}
}
This program uses the AWS Lambda Java library to implement a RequestHandler
that takes an input object and a context object, and returns a string response. In this case, the response is simply “Hello, World!”.
You can package and deploy this Lambda function using the AWS CLI or the AWS Management Console. Once the function is deployed, you can test it by invoking it through the AWS Lambda console or using the AWS SDKs.
END RESPONSE
You can extend of a previous question by asking a related question, for example if you wanted to know how to upload an image to an S3 Bucket. Notice I never mention Java but based on the conversation it knows I will want it in Java:
Question: “write a aws lamda function that uploads an image to an S3 bucket”
START RESPONSE
Certainly! Here’s an example of a Java AWS Lambda function that uploads an image file to an S3 bucket:
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.events.S3Event;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.PutObjectRequest;
public class ImageUploader implements RequestHandler<S3Event, String> {
private static final String BUCKET_NAME = "your-bucket-name"; // Replace with your S3 bucket name
public String handleRequest(S3Event event, Context context) {
try {
String key = event.getRecords().get(0).getS3().getObject().getKey();
// Create an Amazon S3 client
AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();
// Upload the image to S3
s3Client.putObject(new PutObjectRequest(BUCKET_NAME, key, null));
return "Image uploaded successfully to S3 bucket: " + BUCKET_NAME;
} catch (Exception e) {
e.printStackTrace();
return "Error uploading image to S3 bucket";
}
}
}
This function handles an S3 event triggered by a new object being created in the bucket. It retrieves the key (filename) of the uploaded image from the event, creates an instance of the Amazon S3 client, and then uses the putObject
method to upload the image to the specified S3 bucket.
Remember to replace "your-bucket-name"
with the actual name of your S3 bucket.
To deploy and configure this Lambda function, you can use the AWS CLI or the AWS Management Console. You’ll also need to set up the necessary IAM permissions for the Lambda function to access the S3 bucket.
END RESPONSE
ChatGPT and Debugging Code
Debugging code is defiantly a time suck of a software engineers time.
I provided ChatGPT with a “The file is not being uploaded” and pasted the code from the ImageUploader class above, except I changed the putObect to a deleteObject. It had no problem figuring this out.
ChatGPT will debug code where the issue located in a single file, there is no way to point it to a whole project and ask to debug it all. The reason why this is necessary for debugging is when there are multiple layers that could cause the issue. For example, in a NodeJS app, if you have middleware that could return to the client at any point or call the next function.
Will ChatGPT Replace Us?
Not now, or at least not yet. ChatGPT programs at the level of a talented and lazy first-year programming student. It might reduce the need for very entry-level programmers, but at its current level. Although companies cant just stop hiring entry level software engineers because eventually there will be none to hire, even at a senior level (as people change careers or retire). I think it will just make life easier for entry-level programmers and even programmers with more experience, to write code and look up information. It can produce code that has bugs or errors in it, someone needs to be able to run the code and determine if the code matches the use case. It’s definitely a time-saver. Only time will tell.