ChatGPT in the real world
posted 2023.03.21 by Clark Wilkins

As a followup to my prior post, here's a real-world example of ChatGPT and one way to actually use it. I am building a tool today in Uniti.js (which I will say much more about later), and I need to iterate through a large JSON object (at present 17350 lines) of data. Unfortunately for me, the API logic that's creating this query cannot guarantee the “keys” (think of them as rows) are going to be returned in any particular order, and I actually need to sort them by an internal key groupName.

Now, maybe you are a “coder”, and already know where this is going, but I am not, and I haven't looked into this problem much at all prior to today. What's my approaching going to be?

The way we were...

A few months ago, I would start searching the web for examples and tutorials to learn how to get the answer I need. This means I have to:

  • try to compose a relevant Web search query
  • sift through lots of advertisements and possibly relevant links
  • open lots of tabs and read the linked articles hoping that (a) they're actually correct, (b) they's actually relevant to what I am doing, and (c) they actually include all of the steps required

This is a complete "s**t show", but it's now over.

The way we are...

I open a session with ChatGPT and ask “using javascript, reorder an object by an internal key”. This is what I get.

const myObject = {
  banana: {order: 2},
  apple: {order: 1},
  orange: {order: 3}
};

const myArray = Object.entries(myObject); // convert object to array

myArray.sort((a, b) => a[1].order - b[1].order); // sort array by internal key "order"

const sortedObject = Object.fromEntries(myArray); // convert sorted array back to object

console.log(sortedObject);

In this example, the myObject is converted into an array using the Object.entries method. The array is then sorted using the sort method, with a comparison function that sorts by the "order" property of the second element of each subarray. Finally, the sorted array is converted back into an object using the Object.fromEntries method.

This is a complete, understandable, and valid piece of code that I was immediately able to adapt into the tool I am working on. The entire runtime for this was around 5-10 seconds to get an answer, and 2-3 minutes to use it in my own code. In my more than 27 years of writing web-based code, I have never seen anything like this. Now, I have this code block, and I am already onto the next part of the project:

const sourceGroupCount = Object.keys( sourceGroups ).length;
const newArray = Object.entries( sourceGroups );
newArray.sort( ( a, b ) => a[1].sourceName - b[1].sourceName );
const sortedGroups = Object.fromEntries( newArray );

This is a calculator moment

If you grew up in the 1970's, you will remember the first pocket calculators, which made it possible for anyone to quickly multiple large numbers correctly and effortlessly. That's what's happening right now, whether some of you like it or not. These systems are not stand-alone intelligences (yet), but instead are intelligence augmentation tools. They can't design a project like Uniti.js (yet), but they can dramatically speed up my design work and the implementation of my ideas. This is how the world is going to be, and I would suggest you get used to it as fast as you can.

Shout out to my old friend Ron O'Leary. This one is for you.