GameDev : clean our code with refactoring!

Matteo Lo Piccolo
2 min readMay 7, 2021

--

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

Two different IF/ELSE

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.

Create variables

And use them inside code to replace numbers

Nothing change in the flow, but is really much better

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

It opens a menu!

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!

A really clean Update method!

One method for all our work! Amazing!

--

--

Matteo Lo Piccolo

Always in love with programming, even if late (I'm already 39 years old) I decided to follow my dream! We will see how far my passion will take me!