GameDev : clean our code with refactoring!
--
We need now to clean a little bit our code.
Nothing crazy, but better make it now.
The two if/else statement are DIFFERENT. One check X and one check Y, ok?
So we have to make this
First IF/ELSE, SPACE, and another IF/ELSE.
Than, we don’t write number inside code.
Remember? Cash EVERYTHING when is possible. And this is one of that moment, so we need a variables.
And use them inside code to replace numbers
Good for now, but we have another problem : too much things inside Update method.
Every we make so far is around Movement right?
So, why not take EVERYTHING and create a MOVEMENT method?
Is really easy to do and after this incredible little thing, the code is really much more readable!
We can do it in two ways :
Create a method called Movement() and COPY / PASTE everything inside.
It is ok, absolutely nothing wrong to do it.
OR, we can use one of the best SHORTCUT in Visual Studio : Alt+Enter!
If we select everything inside Update and click Alt+Enter
Click enter on Extract Method and Visual Studio creates a new Method for us! We have only have to make a name for it and press enter again!
And now we have a good Movement method!
Everything about Move is inside this method, and the Update method is now incredible clean!
One method for all our work! Amazing!