From the course: Refactoring with GitHub Copilot

Refactoring data structures - Github Copilot Tutorial

From the course: Refactoring with GitHub Copilot

Refactoring data structures

Behind all your code are data structures. Some are simple, some are complex. Sometimes these tend to get out of hand. I'm going to walk through how I've dealt with these using Copilot. This is a sample data structure. Purposely broke some rules here, but you'll see why as I talk through this. First, I'll identify some of the problems. Details is ambiguous. Why can't these values like price and stock and categories and discount be at the root? Or why can't price and discount be moved to a pricing object? Also, discount is null, but on the second item, it's a string. And on the final item, it's missing entirely. Note that categories is either a string or an array. Back to a string here. I'm going to be pretty explicit to Copilot and I'll see how far I can get. I need to pull stock and categories to the root of each product. Also, categories should always be in array. Copilot suggested a function to change it. I'm actually going to ask it to change the code. I need to change the actual array. Change the value of the inventory variable. I asked it to change the array and it gave me the same function again. Well, one way to deal with this, because it wants to execute code, is I can rename the file to JSON, and then I can do that. And I'll say make this valid JSON so I can copy and paste. And now it's valid JSON. And now I can ask the same question. Let's see. I need to pull stock and categories to the root of each product, also categories should always be an array. Okay. So that removed the details object entirely. Weird, but fine. And price still exists, stock still exists, categories is still around, discount still exists. Note that it's only showing the first two items. I did have three. Somewhere I lost the third item. Another thing to be careful of when you're working with Copilot. Now one thing to call out is tags are duplicative of categories. So I can ask it to do, remove tags that duplicate category. And so, electronics is gone and clothing and apparel would work. When I have to deal with data shape conversion which is what would happen here, we would have changed the data shape of the original product to this new product. I always write a converter the first time. And then the second time I have to do it on a project, not only do I write a converter, I also create a versioned definition. So I'm going to ask Copilot to do the same, which is what it suggested when this file was previously a JavaScript file and not a JSON file. Can you create a converter function to move from the original format to this form? And here's that convert data function. As always, double-check Copilot's work. Copilot is great at code. My initial thought when I have to fix or remap a data structure is to write a function to do that. But Copilot can often handle fixes iteratively through discussion in the source file itself. Obviously, pay attention and don't lose a product like I did. This might be faster than writing a function in some cases.

Contents