I Asked GPT-4 To Refactor My Legacy Codebase

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everybody I'm Nick and in this video I'm going to use charge GPT using the brand new gpt4 model to try and get it to refactor a legacy code base generating code from scratch based on a few prompts might be easy for an AI model but what if I give it a lot of code and I ask you to refactor it using best practices that's why it's going to get a really interesting because many people have that use case you have a legacy code base that might have bugs or you might want to refactor it to make it more testable and you just don't know where to start from or how to approach it I think that's where tools like this can be very helpful alongside the developer and we're going to see if it's actually capable of doing so if you lack of content and you want to see more make sure you subscribe during this notification Bell and for more training check out nickjrsas.com now one of the biggest benefits of the gpt4 model compared to the previous ones is that it can actually handle up to 25 000 words worth of context compared to the previous three so we can actually feed more data and can keep track of all that data to generate a result so for things like refactoring this can be really really useful now what is this going to be refactoring well have a relatively small code base over here which contains a legacy project that actually one of the biggest companies here in London gives out to developers interviewing for them to refactor so this is actually an interview question and the code base is a.net and c-sharp code base it has this program.cs as a harness project where effectively tells you don't introduce any breaking changes here which makes it harder to actually refactor because you can't do things like change this Constructor for example or this method over here and then you get the rest of the code base and the main focal point usually in the interview is actually this user service which is a yes it is a lot and then you have things like this client repository which you know doesn't Implement any interface so it's very hard to unit test things like the user service so you have a hard dependency here for example sample then you have this static invocation you have a lot of things that it really make it hard for you to actually see what's going on and introduce changes or also test another thing is that we actually also have some generated code so this user Credit Service is WCF generated code it's not supposed to be changed in any way which also makes it harder to refactor a code base so we're going to use all that and we're going to fade it into charge gbt now here's how we're gonna do this and we're going to use the latest gpt4 model to see how it does so here's what I'm going to ask it hey there my boss asked me to refactor our Legacy project can you do that for me they asked me to use solid principles keys yagni and dry as well as clean code here's the code and the code will just be raw text so I just extracted everything without using statements just the namespace in the methods and everything is here just faded all the data and then in the end I'm going to give it some instructions so the I user Credit Service and the channel interfaces as well as the user Credit Service client are generated code so do not change them keep in mind that anything within the legacy app.consumer is someone else's code and must not be changed please do not introduce any breaking changes in the user service class and it's add user method thanks and that's all I'm gonna give it let's see how it does straight away to refactor this project I'm going to use solid case exactly dry and clean code and it explains what each thing is let's see some code so first it's going to make it Sims an iclanned repository interface which is what you would actually do if you were to refactor this you want to be able to mock this so you can actually test this so you would need an interface then it just omits the existing code to be more efficient I I'm happy with that then it also makes a user repository interface which is very interesting and what it does with that repository is it actually adds the existing implementation of the user data axis the reason why it does that and I'm actually going to ask it as well is because you cannot mock a static method so what it's doing is it's making a wrapper to make a repository for that add user method which is actually what you would do in the interview you should be doing that now should you be calling it a repository I would argue maybe a wrapper would be better but it is not wrong it's actually very good then it makes a validator and it extracts the existing validation logic now I would like to actually see the code here so just saying existing this existing that is not good enough for me it also made a pretty big mistake here by changing the Constructor if you change the Constructor and your consumer is using that Constructor then you just introduce the Breaking Chains so this would actually be a thing that the interviewer would raise and say okay why do you do this you just broke your consumer however one of the things it didn't fix and I'm happy it didn't is it did not change times that the argument name of her name which you'd probably have as first name but if you were to change this you would actually introduce a Breaking Chains because someone can be using named arguments now if I scroll the way down it just finished in one go I will actually ask it some questions because I'm not happy with a few of the things it did here so overall this is actually not bad this is way better than what we had before in fact I would say in isolation this method was refactor really well those are all things you'd have to do past the interview and I'm very surprised because when the previous model of charge gbt was launched I tried this around 20 times I never got it to come anywhere close this is the first time I'm recording this and the model just launched a couple of days ago so this is incredibly impressive now yes you do not have the Constructor here so let's actually challenge it on that hey by removing the default Constructor of user service you broke the consumer can you fix it so I'm not telling it backwards compatibility or anything I'm just saying hey you messed up fix it and you would see that as a user because the project wouldn't compile so I apologize I'll let the default Constructor I cannot tell you how many times even senior developers failed to actually do this because for some reason your mind doesn't go to the point where you can say Hey you can have a default Constructor you can add that to maintain backwards compatibility and then also have the one that is injectable that will allow for you to actually test this this is seriously impressive and to that point this is what we normally would expect from senior developers taking that interview now the interview is supposed to last two hours so developer would have to do all that in two hours this did it in what less than six minutes with me talking to the camera as well this is very impressive I want to go back and just take a look at other things as well just to make sure I didn't miss anything for example I would like to know what is in this user validator so show me the implementation of the user validator because what am I supposed to do with this you don't give me the code so it just goes straight away and it does it so I'm guessing it omits for brevity or maybe for rate limiting purpose because you only have 100 messages you can get it is paid back in four hours so it literally just remove the validation logic into this class however there's a bit of a problem here you see this daytime dot now this is actually a hard dependency on the system's clock because when the time comes to write unit test for something like this well you can't really do that because the age of the user is an offset from The Machine's age so you don't want to be dealing with that like this so what I'm going to do is actually try and get it to refactor this but I don't want to give it the answer what now I'm going to you throw with a curveball and I really don't know how this will try to interpret it so hey I tried writing tests for the validate user method or what that user and after five years the test suddenly started failing why is that and this would happen because we're getting an offset to calculate the date time so if the test is passing today five years down the road depending on what value you used because it's based on the user's age it might start failing so let's see what happens I'd be very interested if oh my God I can't believe this so it's expect that it's due to the hard-coded age validation exactly 21 years of age okay how are you gonna fix that though oh my God I got Shivers there is on this issue refactor the use of our day to accept the reference wow are you gonna inject the daytime damn so that is one way to do it well almost so this is still bad this is still wrong and the reason why it's wrong is because it actually passes down the daytime itself as daytime dot now uh it almost got it so by passing it down like this depending on the lifetime with validator which in this case it is transient so nothing will actually break for what it is trying to fix it for in fact everything will work fine but if you had the validator as a Singleton and you maintained it for the lifetime of the application then if your app runs for days this would still be pinned to that previous day so This Is Not Great and what I'm going to try and get to do is fix it so let's say okay but what if I want to register user validator as a Singleton will it understand the mistake if you want to register the same time you can use different approach to handle your friend's date instead of injecting the reference date through the Constructor you can introduce an iclock yes that's exactly what you would actually do and I cannot tell you how many people get this wrong there's interfaces like I system clock that the dot net team themselves use in fact they have seven of those I don't know why we should get one built-in in.net but the I clock or the I system clock allows you to inject it and then you can mock it by passing down whichever date time you want so looking at this this is the last thing that someone would have to do to really pass the senior developer interview it actually refactored everything perfectly yes I did have to hold its hand a bit but arguably the interviewer would actually ask you those questions so I'm just asking it the questions that interviewer would ask and subsequently the developer would so this is incredibly impressive oh my God it even generates a test clock to allow you to mock it this is more of a fake not a mock but I am speechless I cannot tell you how hard this is for some developers to actually nail down okay one last thing though because some of you might not be familiar with dotnet and C sharp and you might not believe that this is actually called that compiles so what I'm going to do is actually just copy all that code and paste it in my ID to see does it actually build so I copy the refactor code from charge gbt to here and as you can see I haven't changed anything it just works it it all compiles it all makes sense you have the clock generated you have the injection happening on the Constructor nothing broke on the consumer which is one of the biggest issues you might have with this project and then this was left untouched this was less untouched the repository was made and I added you know the missing code that wasn't there but I could ask it to generate it and really that is it it did it so I think that this will be an excellent tool alongside developers to actually refactor the existing code bases and choose how to do this because some limitations might be in place and it recognizes them and you can use them I'm extremely impressed with this I never go to the old model to come anywhere near this thing so I'm I'm just speechless like I highly recommend you try to think outside of the box and see how you can use this as a tool to make you a better developer because beyond the skepticism and the fear-mongering there's something here anyway that's all I have for you for us but definitely you very much for watching special Flex my patreon so making this video as possible if you want to support muscular find the link in the description down below leave a like if you like this video subscribe like listening the Bell as well and I'll see you in the next video keep coding
Info
Channel: Nick Chapsas
Views: 336,922
Rating: undefined out of 5
Keywords: Elfocrash, coding, dot net, core, C#, how to code, tutorial, development, software engineering, microsoft mvp, .net core, nick chapsas, chapsas, dotnet, .net, chatgpt, openai, copilot, chatgpt developer, chatgpt code, coding ai, coding chat gpt, chatpgt coding, open ai chatgpt, openai chatgpt, gpt 4, gpt 4 demo, gpt 4 coding, gpt 4 code, gpt 4 openai, gpt 4 chatgpt, chat gpt 4, chatgpt 4, chat gpt refactoring, gpt 4 refactoring, I Asked ChatGPT 4 To Refactor My Legacy Codebase
Id: y_NHMGZMb14
Channel Id: undefined
Length: 12min 38sec (758 seconds)
Published: Thu Mar 16 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.