Symfony Live London 2013

Refactorizando aplicaciones legacy

Mathias Verraes  · 

Presentación

Vídeo

Transcripción

Extracto de la transcripción automática del vídeo realizada por YouTube.

my name is Matthias for us I am an independent consultant I'm helping teams build enterprise applications and sort of specialized in the really difficult projects with complex business domains and lots of legacy codes will have to integrate with stuff

on it but we have very little time it's a it's a live demo I'm going to show you some codes and try to add this to it and refactor it until it's you know readable and better quality so please keep your questions till the end of the presentation

there should be some minutes left and if not then just grab me afterwards so happy to explain more if you if you have any questions so can everybody read this in the back yeah okay so what we have here it's a class called group service we don't know

what it does but we need to get it under control it's just very short but let's imagine that this is a 6,000 line class with huge methods and that the original developers have left and nobody knows what it's supposed to do so to get started we

just tried to get a sense of what is happening here it's a group service that should give us some hint it takes a group repository and the pupil repository as constructor arguments and then it has one public method called add which takes an ID and the

pupil ID so that gives us a hint as well it's you know group service at pupil ID it's something for school having pupils to a group very simple use case but you know as I said it's we have very little time so we cannot do this with real really

big methods so what else can we find out here it's always good to look at your posture is because here's something interesting happens we are persisting the group so probably this is where what this method is about and the line above it we think they'll

we're adding a pupil to that groups or initial assumption is correct so this seems to be the core reason to exist for this for this ad method and we can see some other interesting things we have two exceptions going on here the pupil already in group exception

and too many pupils exception and if we look at the code we can sort of see that there are some you know business rules happening here we are counting pupils we are looping over pupils and comparing them for some reason so let's just go back to the top

and start trying to put our insights so whenever we have some new insight in the code instead of just remembering it or adding a comment let's try to actually make the goat reflect that new insight for example we have a pupil repository here and something

called riposte or if it's actually a group repository that's sort of annoying because if you have you know as I said 5,000 lines and you won't know at the end what repository this is so let's just rename it and we let we don't do that manually

we have ideas for that sort of stuff so we just call this group repository you can shade see that it changes right here as well and we do the same thing with the property here rename group repository and you can see that it changes right here and right here

and everywhere it's used so just you know get good dueling for this sort of stuff if your IDE cannot do this sort of reef actors then you know you're probably stealing from your employer because you waste too much time on doing that sir stuff so what

else can we see the same sort of problem here ID and pupil ID maybe that grew historically when that original developer wrote code we don't know but we'd like it to be a little bit more explicit so let's just rename that we are as we can see it

right here that idea is being used to find a group in a group repository so that's you know a group ID and something else like here for example this this method name is just at maybe we talk to the to the domain expert to the customer and he's always

talking about here when we enlist pupils in a group so the language that the customer is using is sort of different from the language as we are using and it's going to make communication a lot easier easier if we make this very explicit and this is sort

of database language add stuff delete stuff update stuff whereas in the business it's you know unless the pupil or whatever terms that that the customer is using try to use those as inspiration for your code so we can rename this this will automatically

change every place that this method is being called in our code base so we don't need to worry about that we let the tools do the job for us so that's enlist pupil in group and so the IDE will usually also complain if it's for some reason impossible

to do the refactor so but the important thing try not to focus on the tools i'm using here you can figure that out on your own but try to focus on why i'm doing this sort of stuff so let's let's try to add a test because if we're going

to do more seriously factors it's going to be very hard without test because you don't know if you've broken anything somebody once told me that legacy code is good that you are afraid to change and without tests i I'm afraid to change this

code so let's generate the class for that called group service test you can do this in would be H piece paper or any other testing tool but I like to use phpunit in presentations to piss off Marcelo now that I'm sorry used to doing this for for legacy

code i usually use pc unit but for new coat i think PHP spec is a superior so we just talk to her business expert we renamed our methods what is the point of this we want to end this the people in a group so lets you know goal or test that it should enlist

pupil in group and if you saw the art of stock you already know why this is using underscores if you missed a stop go ask him afterwards and so when we know that we have maybe i'll add a setup method first so if you don't know HP unit this is a this

method will be called before every test that we run in this in this class so we need the system under test it's sort of a convention or you could just call this group service the variable there we go and we know that we need a group repository because

[ ... ]

Nota: se han omitido las otras 3.089 palabras de la transcripción completa para cumplir con las normas de «uso razonable» de YouTube.