Following on from the post on ChatGPT and its impact on the finance industry last week, I wanted to talk through another interesting use case for the technology which is mainly focussed on software engineering.
When people start out in software engineering, they imagine they will spend 80% of their time writing new code, and 20% of their time reading other people’s code. Unfortunately, the inverse is true, and most of an engineer’s time is trying to understand what code does and what was the authors intention. The software engineering community has such a big push on readability, with books published such as Clean Code by Robert C. Martin (better known as Uncle Bob) emphasising code readability over pretty much all else to try and make reading code easier.
But this is where ChatGPT can help. Firstly, if you are looking for example code on how to do something, you no longer need to search Google or Stack Overflow for potential solutions. ChatGPT can give you example code and even explain what is does.
Here, I asked ChatGPT “How do I make an HTTP request in Javascript?”. The response was as follows:
There are several ways to make an HTTP request in JavaScript, but the most commonly used method is the XMLHttpRequest object, which is built into the browser. Here is an example of how to use it to make a GET request to a server:
var xhr = new XMLHttpRequest();
xhr.open("GET", https://example.com);
xhr.send();
You can also use the fetch() function, which is a more modern way to make network requests. Here is an example of how to use it to make a GET request:
fetch('https://example.com')
.then(response => response.text())
.then(data => {
console.log(data);
});
You can also use libraries like axios or superagent to make http request.
axios.get('https://example.com')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.log(error);
});
superagent.get('https://example.com')
.then(response => {
console.log(response.body);
})
.catch(error => {
console.log(error);
});
Please note that you might need to import the libraries or handle the CORS issues if you are making cross origin request.
As you can see, it has suggested several approaches, and even mentioned that you may need to import libraries or deal with CORS issues in certain scenarios. As a developer, you can now quickly get going and start testing out solutions. The other useful thing that ChatGPT can do is explain what code is doing. I have asked ChatGPT the following question:
Explain what the following Javascript does.
var xhr = new XMLHttpRequest();
xhr.open("GET", https://example.com);
xhr.send();”.
This is ChatGPTs response:
The code you provided is using the XMLHttpRequest object to make an HTTP GET request to the server at “https://example.com“.
Here’s what each line does:
- var xhr = new XMLHttpRequest(); creates a new instance of the XMLHttpRequest object, which is used to make network requests.
- xhr.open(“GET”, https://example.com); initializes the request. The first argument “GET” specifies the request method (GET, POST, etc.). The second argument “https://example.com” is the URL of the server that the request is being sent to.
- xhr.send(); sends the request to the server. If you want to send data along with the request, you can pass it as an argument to the send() method.
This code will make a GET request to the specified URL and retrieve the data from it. The response data can be handled using the xhr.onload and xhr.onerror events.
Please note that this code will only work in a browser and will not work in node.js. If you want to make http requests in node.js you might want to use the http module or https module.
As you can see, ChatGPT has summarised the code’s function succinctly and then walked through the code line by line explaining what each line does. It has even highlighted that this example code won’t work in node.js.
If I ask the follow up question, “How do I make a HTTP request in Node.js?” it provides further code examples:
There are several ways to make an HTTP request in Node.js, but the most commonly used method is the http module, which is built into Node.js. Here is an example of how to use it to make a GET request to a server:
const https = require('http');
const options = {
hostname: 'example.com',
port: 80,
path: '/',
method: 'GET'
};
const req = http.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`);
res.on('data', (d) => {
process.stdout.write(d);
});
});
req.on('error', (error) => {
console.error(error);
});
req.end();
You can also use the https module for making request over https. The options for https request is same as http.
const https = require('https');
const options = {
hostname: 'example.com',
port: 443,
path: '/',
method: 'GET'
};
const req = https.request(options, (res) => {
console.log(`statusCode: ${res.statusCode}`);
res.on('data', (d) => {
process.stdout.write(d);
});
});
req.on('error', (error) => {
console.error(error);
});
req.end();
You can also use libraries like request, axios or superagent to make http request. But these libraries are not built-in to node.js so you will need to install them first.
const request = require('request');
request('https://example.com', { json: true }, (err, res, body) => {
if (err) { return console.log(err); }
console.log(body.url);
console.log(body.explanation);
});
const axios = require('axios');
axios.get('https://example.com')
.then(response => {
console.log(response.data);
})
.catch(error => {
console.log(error);
});
const superagent = require('superagent');
superagent.get('https://example.com')
.then(response => {
console.log(response.body);
})
.catch(error => {
console.log(error);
});
Please note that in node.js you will have to handle the https certificate validation on your own if using https module.
Again, it has highlighted several approaches, with example code and even pointed out that certificate validation will be an issue if using HTTPS.
The impact of this on engineer productivity could be huge. Rather than trawling through articles online looking at poorly documented example code, ChatGPT can get you a detailed example and even explain the example code to you. You can go from zero to 1 much faster than before.
This could really open the world of software engineering to a wider audience and flatten the learning curve for new engineers. Being able to effectively prompt the AI will become a key developer skill, much like the ability to effectively search Google and Stack Overflow is today.
Article by Kris Wilson, Chief Technology Officer To discuss our technology proposition further, please contact Kris Wilson, Chief Technology Officer – kris.wilson@channelcapital.co.uk.