Refactoring

In this article, we will talk about Refactoring.


Refactoring is a code enhancement technique, its goal is to improve the code structure, without modifying the behaviors, thus improving your organization, and making maintenance easier.

For this article I will use my code named Enemy_2, and also the Enemy_1' code that I created in Article about DRY. If you do not readed, the link is this mauroubaldo.com/using-principle-dry/.

Now here is our class Enemy_1:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy_1 : MonoBehaviour
{

    float _Speed_X = 5;
    int _HP = 100;

    bool _Right_Movementation;
    bool _Left_Movementation;

    Rigidbody2D _Rdb;

    Vector2 _Right_Vector;
    Vector2 _Left_Vector;

    Health_System_Enemy _Health_System_Enemy_Script;

    void Start()
    {

        _Right_Movementation = true;

        _Rdb = GetComponent<Rigidbody2D>();
        _Health_System_Enemy_Script = GetComponent<Health_System_Enemy>();

        _Right_Vector = new Vector2(_Speed_X, _Rdb.velocity.y);
        _Left_Vector = new Vector2(-_Speed_X, _Rdb.velocity.y);

    }

    void Update()
    {

        if (_Right_Movementation == true)
            _Rdb.velocity = _Right_Vector;
        if (_Left_Movementation == true)
            _Rdb.velocity = _Left_Vector;

    }

    private void OnTriggerEnter2D(Collider2D collision)
    {

        if (collision.gameObject.CompareTag("Left_Limit"))
        {
            _Right_Movementation = true;
            _Left_Movementation = false;
        }

        if (collision.gameObject.CompareTag("Right_Limit"))
        {
            _Right_Movementation = false;
            _Left_Movementation = true;
        }

        if (collision.gameObject.CompareTag("Player"))
        {
            _Health_System_Enemy_Script.Reduce_HP(15);
        }

    }

}

And here is our Enemy_2:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy_2 : MonoBehaviour
{

    float _Speed_X = 5;
    float _Speed_Y = 5;
    float _Time;

    int _HP = 100;

    bool _Right_Movementation;
    bool _Left_Movementation;
    bool _Up_Movementation;
    bool _Down_Movementation;

    Rigidbody2D _Rdb;

    Vector2 _Right_Up_Diagonal;
    Vector2 _Right_Down_Diagonal;

    Vector2 _Left_Up_Diagonal;
    Vector2 _Left_Down_Diagonal;

    Health_System_Enemy _Health_System_Enemy_Script;

    void Start()
    {

        _Left_Movementation = true;
        _Up_Movementation = true;

        _Rdb = GetComponent<Rigidbody2D>();
        _Health_System_Enemy_Script = GetComponent<Health_System_Enemy>();

        _Right_Up_Diagonal = new Vector2(_Speed_X, _Speed_Y);
        _Right_Down_Diagonal = new Vector2(_Speed_X, -_Speed_Y);

        _Left_Up_Diagonal = new Vector2(-_Speed_X, _Speed_Y);
        _Left_Down_Diagonal = new Vector2(-_Speed_X, -_Speed_Y);

    }

    void Update()
    {

        if (_Right_Movementation == true && _Up_Movementation == true)
            _Rdb.velocity = _Right_Up_Diagonal;

        if (_Right_Movementation == true && _Down_Movementation == true)
            _Rdb.velocity = _Right_Down_Diagonal;

        if (_Left_Movementation == true && _Up_Movementation == true)
            _Rdb.velocity = _Left_Up_Diagonal;

        if (_Left_Movementation == true && _Down_Movementation == true)
            _Rdb.velocity = _Left_Down_Diagonal;

        _Time += Time.deltaTime;

        if(_Time > 5)
        {
            _Time = 0;

            if(_Up_Movementation == true)
            {
                Change_For_Down_Movementation();
            }

            if (_Down_Movementation == true)
            {
                Change_For_Up_Movementation();
            }

            if(_Left_Movementation == true)
            {
                Change_For_Right_Movementation();
            }

            if (_Right_Movementation == true)
            {
                Change_For_Left_Movementation();
            }

        }

    }

    void Change_For_Up_Movementation()
    {
        _Up_Movementation = true;
        _Down_Movementation = false;
    }

    void Change_For_Down_Movementation()
    {
        _Up_Movementation = false;
        _Down_Movementation = true;
    }

    void Change_For_Left_Movementation()
    {
        _Left_Movementation = true;
        _Right_Movementation = false;
    }

    void Change_For_Right_Movementation()
    {
        _Left_Movementation = false;
        _Right_Movementation = true;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {

        if (collision.gameObject.CompareTag("Left_Limit"))
        {
            Change_For_Right_Movementation();
        }

        if (collision.gameObject.CompareTag("Right_Limit"))
        {
            Change_For_Left_Movementation();
        }

        if (collision.gameObject.CompareTag("Up_Limit"))
        {
            Change_For_Down_Movementation();
        }

        if (collision.gameObject.CompareTag("Down_Limit"))
        {
            Change_For_Up_Movementation();
        }

        if (collision.gameObject.CompareTag("Player"))
        {
            _Health_System_Enemy_Script.Reduce_HP(15);
        }

    }

}

For this case, I will create a class for each behavior, and in the final, I will create a class for to make managing behaviors of Enemies.

Now that We saw our classes, Let’s go.

Firstly, I will start with the Enemy class 1. So, our Enemy 1 has the following behaviors: Walk to the right side and to the left side, and He also causes damage to the Player, as I created the Damage class, I will create just the walking class, here is our new class:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Walk_Enemy : MonoBehaviour
{

    float _Speed_X = 5;

    Rigidbody2D _Rdb;

    Vector2 _Right_Vector;
    Vector2 _Left_Vector;

    void Start()
    {
        _Right_Vector = new Vector2(_Speed_X, _Rdb.velocity.y);
        _Left_Vector = new Vector2(-_Speed_X, _Rdb.velocity.y);

        _Rdb = GetComponent<Rigidbody2D>();
    }

    public void Right_Movementation()
    {
        _Rdb.velocity = _Right_Vector;
    }

    public void Left_Movementation()
    {
        _Rdb.velocity = _Left_Vector;
    }

}

As you can see, this class has a unique responsibility, which is to make Enemy_1 walk, our damage class was created in the DRY article, but I will show the class here as well.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Health_System_Enemy : MonoBehaviour
{

    int _HP = 100;

    //This method is used in Enemy_1 Class, and also Enemy_2 Class

    public void Reduce_HP(int Damage)
    {

        _HP -= Damage;
        if (_HP <= 0)
            print("Die");

    }

}

Now I will modify our class Enemy_1 for she to only manage the behaviors, but before I need to clear Enemy_1 class, because the class has many code without utility, she is dirty and with unnecessary code. I will show our Enemy_1 class before and after the changes.

Enemy_1 Before the changes:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy_1 : MonoBehaviour
{

    float _Speed_X = 5;
    int _HP = 100;

    bool _Right_Movementation;
    bool _Left_Movementation;

    Rigidbody2D _Rdb;

    Vector2 _Right_Vector;
    Vector2 _Left_Vector;

    Health_System_Enemy _Health_System_Enemy_Script;

    void Start()
    {

        _Right_Movementation = true;

        _Rdb = GetComponent<Rigidbody2D>();
        _Health_System_Enemy_Script = GetComponent<Health_System_Enemy>();

        _Right_Vector = new Vector2(_Speed_X, _Rdb.velocity.y);
        _Left_Vector = new Vector2(-_Speed_X, _Rdb.velocity.y);

    }

    void Update()
    {

        if (_Right_Movementation == true)
            _Rdb.velocity = _Right_Vector;
        if (_Left_Movementation == true)
            _Rdb.velocity = _Left_Vector;

    }

    private void OnTriggerEnter2D(Collider2D collision)
    {

        if (collision.gameObject.CompareTag("Left_Limit"))
        {
            _Right_Movementation = true;
            _Left_Movementation = false;
        }

        if (collision.gameObject.CompareTag("Right_Limit"))
        {
            _Right_Movementation = false;
            _Left_Movementation = true;
        }

        if (collision.gameObject.CompareTag("Player"))
        {
            _Health_System_Enemy_Script.Reduce_HP(15);
        }

    }

}

After the changes:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy_1 : MonoBehaviour
{

    bool _Right_Movementation;
    bool _Left_Movementation;

    Walk_Enemy _Walk_Left_And_Right_Side_Script;
    Health_System_Enemy _Health_System_Enemy_Script;

    void Start()
    {

        _Health_System_Enemy_Script = GetComponent<Health_System_Enemy>();
        _Walk_Left_And_Right_Side_Script = GetComponent<Walk_Enemy>();

    }

    void Update()
    {

        if (_Right_Movementation == true)
            _Walk_Left_And_Right_Side_Script.Right_Movementation();

        if (_Left_Movementation == true)
            _Walk_Left_And_Right_Side_Script.Left_Movementation();

    }

    private void OnTriggerEnter2D(Collider2D collision)
    {

        if (collision.gameObject.CompareTag("Left_Limit"))
        {
            _Right_Movementation = true;
            _Left_Movementation = false;
        }

        if (collision.gameObject.CompareTag("Right_Limit"))
        {
            _Right_Movementation = false;
            _Left_Movementation = true;
        }

        if (collision.gameObject.CompareTag("Player"))
        {
            _Health_System_Enemy_Script.Reduce_HP(15);
        }

    }

}

Repair that I removed all the unnecessary code, and I kept only motion control movement variables, because the Enemy_1 class only will to manage the behaviors, she will control movement and to apply damage. 

As this is a very simple class, all the modifies look small, but in the Enemy_2 class, the changes will be more expressive.

I modified this class because for to show better the changes.

So Let’s go to Enemy_2 Class:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy_2 : MonoBehaviour
{

    float _Speed_X = 5;
    float _Speed_Y = 5;
    float _Time;

    int _HP = 100;
    int _Bullet_Counter;

    bool _Follow_Autorization;
    bool _Fire_Autorization;

    bool _Right_Movementation;
    bool _Left_Movementation;
    bool _Up_Movementation;
    bool _Down_Movementation;

    Rigidbody2D _Rdb;

    Vector2 _Right_Up_Diagonal;
    Vector2 _Right_Down_Diagonal;

    Vector2 _Left_Up_Diagonal;
    Vector2 _Left_Down_Diagonal;

    public GameObject _Bullet;
    public Transform _Cannon;

    Health_System_Enemy _Health_System_Enemy_Script;

    void Start()
    {

        _Left_Movementation = true;
        _Up_Movementation = true;

        _Rdb = GetComponent<Rigidbody2D>();
        _Health_System_Enemy_Script = GetComponent<Health_System_Enemy>();

        _Right_Up_Diagonal = new Vector2(_Speed_X, _Speed_Y);
        _Right_Down_Diagonal = new Vector2(_Speed_X, -_Speed_Y);

        _Left_Up_Diagonal = new Vector2(-_Speed_X, _Speed_Y);
        _Left_Down_Diagonal = new Vector2(-_Speed_X, -_Speed_Y);

    }

    void Update()
    {

        if (_Right_Movementation == true && _Up_Movementation == true)
            _Rdb.velocity = _Right_Up_Diagonal;

        if (_Right_Movementation == true && _Down_Movementation == true)
            _Rdb.velocity = _Right_Down_Diagonal;

        if (_Left_Movementation == true && _Up_Movementation == true)
            _Rdb.velocity = _Left_Up_Diagonal;

        if (_Left_Movementation == true && _Down_Movementation == true)
            _Rdb.velocity = _Left_Down_Diagonal;

        _Time += Time.deltaTime;

        if(_Time > 5)
        {
            _Time = 0;

            if(_Up_Movementation == true)
            {
                Change_For_Down_Movementation();
            }

            if (_Down_Movementation == true)
            {
                Change_For_Up_Movementation();
            }

            if(_Left_Movementation == true)
            {
                Change_For_Right_Movementation();
            }

            if (_Right_Movementation == true)
            {
                Change_For_Left_Movementation();
            }

        }

    }
    
    void Add_Value_In_Bullet_Counter()
    {
        _Bullet_Counter++;
    }
    void Shoot()
    {
        //Create bullet
        Instantiate(_Bullet, _Cannon.transform.position, Quaternion.identity);
        Add_Value_In_Bullet_Counter();
    }

    void Follow()
    {
        //To make the enemy follow player
    }

    void Change_For_Up_Movementation()
    {
        _Up_Movementation = true;
        _Down_Movementation = false;
    }

    void Change_For_Down_Movementation()
    {
        _Up_Movementation = false;
        _Down_Movementation = true;
    }

    void Change_For_Left_Movementation()
    {
        _Left_Movementation = true;
        _Right_Movementation = false;
    }

    void Change_For_Right_Movementation()
    {
        _Left_Movementation = false;
        _Right_Movementation = true;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {

        if (collision.gameObject.CompareTag("Left_Limit"))
        {
            Change_For_Right_Movementation();
        }

        if (collision.gameObject.CompareTag("Right_Limit"))
        {
            Change_For_Left_Movementation();
        }

        if (collision.gameObject.CompareTag("Up_Limit"))
        {
            Change_For_Down_Movementation();
        }

        if (collision.gameObject.CompareTag("Down_Limit"))
        {
            Change_For_Up_Movementation();
        }

        if (collision.gameObject.CompareTag("Player"))
        {
            _Health_System_Enemy_Script.Reduce_HP(15);
        }

    }

}

So, We can to see that, exist many useless code, as methods: Add_Value_In_Bullet_Counter, Shoot, Follow, and as variables: _Follow_Autorization, _Fire_Autorization, _Bullet_Counter, _HP, _Bullet, _Cannon.

Well, Let’s go, firstly I will start removing useless code, now this is our class:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy_2 : MonoBehaviour
{

    float _Speed_X = 5;
    float _Speed_Y = 5;
    float _Time;

    bool _Right_Movementation;
    bool _Left_Movementation;
    bool _Up_Movementation;
    bool _Down_Movementation;

    Rigidbody2D _Rdb;

    Vector2 _Right_Up_Diagonal;
    Vector2 _Right_Down_Diagonal;

    Vector2 _Left_Up_Diagonal;
    Vector2 _Left_Down_Diagonal;

    Health_System_Enemy _Health_System_Enemy_Script;

    void Start()
    {

        _Left_Movementation = true;
        _Up_Movementation = true;

        _Rdb = GetComponent<Rigidbody2D>();
        _Health_System_Enemy_Script = GetComponent<Health_System_Enemy>();

        _Right_Up_Diagonal = new Vector2(_Speed_X, _Speed_Y);
        _Right_Down_Diagonal = new Vector2(_Speed_X, -_Speed_Y);

        _Left_Up_Diagonal = new Vector2(-_Speed_X, _Speed_Y);
        _Left_Down_Diagonal = new Vector2(-_Speed_X, -_Speed_Y);

    }

    void Update()
    {

        if (_Right_Movementation == true && _Up_Movementation == true)
            _Rdb.velocity = _Right_Up_Diagonal;

        if (_Right_Movementation == true && _Down_Movementation == true)
            _Rdb.velocity = _Right_Down_Diagonal;

        if (_Left_Movementation == true && _Up_Movementation == true)
            _Rdb.velocity = _Left_Up_Diagonal;

        if (_Left_Movementation == true && _Down_Movementation == true)
            _Rdb.velocity = _Left_Down_Diagonal;

        _Time += Time.deltaTime;

        if(_Time > 5)
        {
            _Time = 0;

            if(_Up_Movementation == true)
            {
                Change_For_Down_Movementation();
            }

            if (_Down_Movementation == true)
            {
                Change_For_Up_Movementation();
            }

            if(_Left_Movementation == true)
            {
                Change_For_Right_Movementation();
            }

            if (_Right_Movementation == true)
            {
                Change_For_Left_Movementation();
            }

        }

    }

    void Change_For_Up_Movementation()
    {
        _Up_Movementation = true;
        _Down_Movementation = false;
    }

    void Change_For_Down_Movementation()
    {
        _Up_Movementation = false;
        _Down_Movementation = true;
    }

    void Change_For_Left_Movementation()
    {
        _Left_Movementation = true;
        _Right_Movementation = false;
    }

    void Change_For_Right_Movementation()
    {
        _Left_Movementation = false;
        _Right_Movementation = true;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {

        if (collision.gameObject.CompareTag("Left_Limit"))
        {
            Change_For_Right_Movementation();
        }

        if (collision.gameObject.CompareTag("Right_Limit"))
        {
            Change_For_Left_Movementation();
        }

        if (collision.gameObject.CompareTag("Up_Limit"))
        {
            Change_For_Down_Movementation();
        }

        if (collision.gameObject.CompareTag("Down_Limit"))
        {
            Change_For_Up_Movementation();
        }

        if (collision.gameObject.CompareTag("Player"))
        {
            _Health_System_Enemy_Script.Reduce_HP(15);
        }

    }

}

But, You can see that the class is very big, Update method has 3 different functionalities, and this is not good, so now I will change this. For this case, the correct way is the same as Enemy_1, our class Enemy_2 only will to manage behaviors, for the sake of good practice’s question, I will add our movement in our Walk_Enemy_Script.

Now here is our Walk_Enemy_Script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Walk_Enemy : MonoBehaviour
{

    float _Speed_X = 5;
    float _Speed_Y = 5;

    Rigidbody2D _Rdb;

    Vector2 _Right_Vector;
    Vector2 _Left_Vector;

    Vector2 _Right_Up_Diagonal;
    Vector2 _Right_Down_Diagonal;

    Vector2 _Left_Up_Diagonal;
    Vector2 _Left_Down_Diagonal;

    void Start()
    {
        _Right_Vector = new Vector2(_Speed_X, _Rdb.velocity.y);
        _Left_Vector = new Vector2(-_Speed_X, _Rdb.velocity.y);

        _Right_Up_Diagonal = new Vector2(_Speed_X, _Speed_Y);
        _Right_Down_Diagonal = new Vector2(_Speed_X, -_Speed_Y);

        _Left_Up_Diagonal = new Vector2(-_Speed_X, _Speed_Y);
        _Left_Down_Diagonal = new Vector2(-_Speed_X, -_Speed_Y);

        _Rdb = GetComponent<Rigidbody2D>();
    }

    public void Right_Movementation()
    {
        _Rdb.velocity = _Right_Vector;
    }

    public void Left_Movementation()
    {
        _Rdb.velocity = _Left_Vector;
    }

    public void Right_Up_Diagonal()
    {
        _Rdb.velocity = _Right_Up_Diagonal;
    }

    public void Right_Down_Diagonal()
    {
        _Rdb.velocity = _Right_Down_Diagonal;
    }

    public void Left_Up_Diagonal()
    {
        _Rdb.velocity = _Left_Up_Diagonal;
    }

    public void Left_Down_Diagonal()
    {
        _Rdb.velocity = _Left_Down_Diagonal;
    }

}

Now, I will to remove the walk behavior in the Enemy_2, so for this. 

This is our class:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy_2 : MonoBehaviour
{

    float _Time;

    bool _Right_Movementation;
    bool _Left_Movementation;
    bool _Up_Movementation;
    bool _Down_Movementation;

    Health_System_Enemy _Health_System_Enemy_Script;

    void Start()
    {

        _Left_Movementation = true;
        _Up_Movementation = true;

        _Health_System_Enemy_Script = GetComponent<Health_System_Enemy>();

    }

    void Update()
    {

        if (_Right_Movementation == true && _Up_Movementation == true)

        if (_Right_Movementation == true && _Down_Movementation == true)

        if (_Left_Movementation == true && _Up_Movementation == true)

        if (_Left_Movementation == true && _Down_Movementation == true)

        _Time += Time.deltaTime;

        if(_Time > 5)
        {
            _Time = 0;

            if(_Up_Movementation == true)
            {
                Change_For_Down_Movementation();
            }

            if (_Down_Movementation == true)
            {
                Change_For_Up_Movementation();
            }

            if(_Left_Movementation == true)
            {
                Change_For_Right_Movementation();
            }

            if (_Right_Movementation == true)
            {
                Change_For_Left_Movementation();
            }

        }

    }

    void Change_For_Up_Movementation()
    {
        _Up_Movementation = true;
        _Down_Movementation = false;
    }

    void Change_For_Down_Movementation()
    {
        _Up_Movementation = false;
        _Down_Movementation = true;
    }

    void Change_For_Left_Movementation()
    {
        _Left_Movementation = true;
        _Right_Movementation = false;
    }

    void Change_For_Right_Movementation()
    {
        _Left_Movementation = false;
        _Right_Movementation = true;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {

        if (collision.gameObject.CompareTag("Left_Limit"))
        {
            Change_For_Right_Movementation();
        }

        if (collision.gameObject.CompareTag("Right_Limit"))
        {
            Change_For_Left_Movementation();
        }

        if (collision.gameObject.CompareTag("Up_Limit"))
        {
            Change_For_Down_Movementation();
        }

        if (collision.gameObject.CompareTag("Down_Limit"))
        {
            Change_For_Up_Movementation();
        }

        if (collision.gameObject.CompareTag("Player"))
        {
            _Health_System_Enemy_Script.Reduce_HP(15);
        }

    }

}

Now I will to insert the walk movimentation using our class Walk_Enemy.

So here is our class:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy_2 : MonoBehaviour
{

    float _Time;

    bool _Right_Movementation;
    bool _Left_Movementation;
    bool _Up_Movementation;
    bool _Down_Movementation;

    Health_System_Enemy _Health_System_Enemy_Script;
    Walk_Enemy _Walk_Enemy_Script;

    void Start()
    {

        _Left_Movementation = true;
        _Up_Movementation = true;

        _Health_System_Enemy_Script = GetComponent<Health_System_Enemy>();
        _Walk_Enemy_Script = GetComponent<Walk_Enemy>();

    }

    void Update()
    {

        if (_Right_Movementation == true && _Up_Movementation == true)
            _Walk_Enemy_Script.Right_Up_Diagonal();

        if (_Right_Movementation == true && _Down_Movementation == true)
            _Walk_Enemy_Script.Right_Down_Diagonal();

        if (_Left_Movementation == true && _Up_Movementation == true)
            _Walk_Enemy_Script.Left_Up_Diagonal();

        if (_Left_Movementation == true && _Down_Movementation == true)
            _Walk_Enemy_Script.Left_Down_Diagonal();

        _Time += Time.deltaTime;

        if(_Time > 5)
        {
            _Time = 0;

            if(_Up_Movementation == true)
            {
                Change_For_Down_Movementation();
            }

            if (_Down_Movementation == true)
            {
                Change_For_Up_Movementation();
            }

            if(_Left_Movementation == true)
            {
                Change_For_Right_Movementation();
            }

            if (_Right_Movementation == true)
            {
                Change_For_Left_Movementation();
            }

        }

    }

    void Change_For_Up_Movementation()
    {
        _Up_Movementation = true;
        _Down_Movementation = false;
    }

    void Change_For_Down_Movementation()
    {
        _Up_Movementation = false;
        _Down_Movementation = true;
    }

    void Change_For_Left_Movementation()
    {
        _Left_Movementation = true;
        _Right_Movementation = false;
    }

    void Change_For_Right_Movementation()
    {
        _Left_Movementation = false;
        _Right_Movementation = true;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {

        if (collision.gameObject.CompareTag("Left_Limit"))
        {
            Change_For_Right_Movementation();
        }

        if (collision.gameObject.CompareTag("Right_Limit"))
        {
            Change_For_Left_Movementation();
        }

        if (collision.gameObject.CompareTag("Up_Limit"))
        {
            Change_For_Down_Movementation();
        }

        if (collision.gameObject.CompareTag("Down_Limit"))
        {
            Change_For_Up_Movementation();
        }

        if (collision.gameObject.CompareTag("Player"))
        {
            _Health_System_Enemy_Script.Reduce_HP(15);
        }

    }

}

Looks better, but still not is good, because in Update method has many things happening, now I will change this.

Firstly is necessary to understand what this method is making.

Our variable _Time is receiving increment, after this, a verification is made, _Time receives zero, and the behavior is changed.

So I will to create a method for making the increment, and another for changing the behavior.

This is our class after to make the changes:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy_2 : MonoBehaviour
{

    float _Time;

    bool _Right_Movementation;
    bool _Left_Movementation;
    bool _Up_Movementation;
    bool _Down_Movementation;

    Health_System_Enemy _Health_System_Enemy_Script;
    Walk_Enemy _Walk_Enemy_Script;

    void Start()
    {

        _Left_Movementation = true;
        _Up_Movementation = true;

        _Health_System_Enemy_Script = GetComponent<Health_System_Enemy>();
        _Walk_Enemy_Script = GetComponent<Walk_Enemy>();

    }

    void Update()
    {

        if (_Right_Movementation == true && _Up_Movementation == true)
            _Walk_Enemy_Script.Right_Up_Diagonal();

        if (_Right_Movementation == true && _Down_Movementation == true)
            _Walk_Enemy_Script.Right_Down_Diagonal();

        if (_Left_Movementation == true && _Up_Movementation == true)
            _Walk_Enemy_Script.Left_Up_Diagonal();

        if (_Left_Movementation == true && _Down_Movementation == true)
            _Walk_Enemy_Script.Left_Down_Diagonal();

        Manage_Timer();

    }
    
    void Manage_Timer()
    {
        _Time += Time.deltaTime;

        if (_Time > 5)
        {
            _Time = 0;
            Change_Behavior();
        }

    }

    void Change_Behavior()
    {

        if (_Up_Movementation == true)
        {
            Change_For_Down_Movementation();
        }

        if (_Down_Movementation == true)
        {
            Change_For_Up_Movementation();
        }

        if (_Left_Movementation == true)
        {
            Change_For_Right_Movementation();
        }

        if (_Right_Movementation == true)
        {
            Change_For_Left_Movementation();
        }

    }

    void Change_For_Up_Movementation()
    {
        _Up_Movementation = true;
        _Down_Movementation = false;
    }

    void Change_For_Down_Movementation()
    {
        _Up_Movementation = false;
        _Down_Movementation = true;
    }

    void Change_For_Left_Movementation()
    {
        _Left_Movementation = true;
        _Right_Movementation = false;
    }

    void Change_For_Right_Movementation()
    {
        _Left_Movementation = false;
        _Right_Movementation = true;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {

        if (collision.gameObject.CompareTag("Left_Limit"))
        {
            Change_For_Right_Movementation();
        }

        if (collision.gameObject.CompareTag("Right_Limit"))
        {
            Change_For_Left_Movementation();
        }

        if (collision.gameObject.CompareTag("Up_Limit"))
        {
            Change_For_Down_Movementation();
        }

        if (collision.gameObject.CompareTag("Down_Limit"))
        {
            Change_For_Up_Movementation();
        }

        if (collision.gameObject.CompareTag("Player"))
        {
            _Health_System_Enemy_Script.Reduce_HP(15);
        }

    }

}

Now, all changes have been made, but for a legibility question, I will remove the { } in conditionals that have only one instruction.

So here is our new Enemy_2 class with all changes finished:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy_2 : MonoBehaviour
{

    float _Time;

    bool _Right_Movementation;
    bool _Left_Movementation;
    bool _Up_Movementation;
    bool _Down_Movementation;

    Health_System_Enemy _Health_System_Enemy_Script;
    Walk_Enemy _Walk_Enemy_Script;

    void Start()
    {

        _Left_Movementation = true;
        _Up_Movementation = true;

        _Health_System_Enemy_Script = GetComponent<Health_System_Enemy>();
        _Walk_Enemy_Script = GetComponent<Walk_Enemy>();

    }

    void Update()
    {

        if (_Right_Movementation == true && _Up_Movementation == true)
            _Walk_Enemy_Script.Right_Up_Diagonal();

        if (_Right_Movementation == true && _Down_Movementation == true)
            _Walk_Enemy_Script.Right_Down_Diagonal();

        if (_Left_Movementation == true && _Up_Movementation == true)
            _Walk_Enemy_Script.Left_Up_Diagonal();

        if (_Left_Movementation == true && _Down_Movementation == true)
            _Walk_Enemy_Script.Left_Down_Diagonal();

        Manage_Timer();

    }
    
    void Manage_Timer()
    {
        _Time += Time.deltaTime;

        if (_Time > 5)
        {
            _Time = 0;
            Change_Behavior();
        }

    }

    void Change_Behavior()
    {

        if (_Up_Movementation == true)
            Change_For_Down_Movementation();
        

        if (_Down_Movementation == true)
             Change_For_Up_Movementation();
        

        if (_Left_Movementation == true)
            Change_For_Right_Movementation();
        

        if (_Right_Movementation == true)
            Change_For_Left_Movementation();

    }

    void Change_For_Up_Movementation()
    {
        _Up_Movementation = true;
        _Down_Movementation = false;
    }

    void Change_For_Down_Movementation()
    {
        _Up_Movementation = false;
        _Down_Movementation = true;
    }

    void Change_For_Left_Movementation()
    {
        _Left_Movementation = true;
        _Right_Movementation = false;
    }

    void Change_For_Right_Movementation()
    {
        _Left_Movementation = false;
        _Right_Movementation = true;
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {

        if (collision.gameObject.CompareTag("Left_Limit"))
            Change_For_Right_Movementation();

        if (collision.gameObject.CompareTag("Right_Limit"))
            Change_For_Left_Movementation();

        if (collision.gameObject.CompareTag("Up_Limit"))
            Change_For_Down_Movementation();

        if (collision.gameObject.CompareTag("Down_Limit"))
            Change_For_Up_Movementation();

        if (collision.gameObject.CompareTag("Player"))
            _Health_System_Enemy_Script.Reduce_HP(15);

    }

}

Very well, in this article, I talked about refactoring, a very important programming’s concept.

Follow-me in Instagram for stay knowing when I create new posts @mauro_developer

You can also leave your feedback, and your suggestion, I will read everything and answer your message

This project is uploaded in my github: github.com/MauroJrDeveloper/Refactoring-Article

Thank you for reading.

Code Documenting Part 2

In this article, I will to say about Comments in code.


My last article was about some good practices for writing your code. Now we let’s go to study comments in the code. We also to see the solution for problem that was presented in the last article.

So Let’s go.


This article is a complement of the previous article.

I would like make clear that good code do not need many comments, because your variable’s and method’s names are auto explicative.


Good, firstly, exist two forms of use comments in code, they are:
First type, you need write two / and after this, you write your code, this comment stay only a line, for example:

//Your comment


Second type, you need write /* for open the comment, and */ for close the comment, all text that written inside the block, will stay as comment.

 /*
Your comment
*/


Now we let’s go to solve our problem that last article.
We have this method with this parameter:

public void Reduce_Player_Life(int Damage_For_Reduce)

How to document this?
You only need write a comment informing where it is used, for example:

//This method is called in class named Enemy, on method, OnCollisionEnter2D
public void Reduce_Player_Life(int Damage_For_Reduce)

Follow-me in Instagram for stay knowing when I create new posts @mauro_developer
You can also leave your feedback, and your suggestion, I will to read all, and I will to answer for you.

Code Documenting Part 1

In this article, I will to say about Code Documenting.

So let’s go to start

Is very important that our code be good documented, because this create a facility for maintenance.


Now a question that probably is present in your brain is, how do to this?

Is exactly, that I will to say in this article, how to do a code with a good documenting, this is a Part 1, in next part I will to say about comments in code, and how to solve a problem that will to be presented in this article.

For to start, let’s to say about names.

Your classes, variables and methods, need have an auto-descriptive name. I will give examples separately:

Classes

Incorrect

public class Dmg: MonoBehaviour

What is DMG?

For you DMG can has a sense, but for others DMG can do not has sense, DMG is an acronym, the unique way for to discover is opening the class and read. This it costs time that sometimes you may not have.

Correct

public class User_Damage_System : MonoBehaviour

Now I will to say about correct example, here we have User_Damage_System, so with base in this, I know that this is a class responsible for damage in user.

Variables

Incorrect

public int value;

Again, I have the same question, what is value? What your function? What he receipt? You only to know when open the class and read the code.

Correct

public int _Player_Life;

Here We have a correct name Player_Life, we know that the reason for your existence is for receipt the Player’s life value, and we know also, that for increase or decrease the player life, we only need to change this value present in Player_Life.

Now I will to present our problem that will be solved in the next article.

Method

Incorrect

public void R_L_P(int value)

So, I already made the same question many times, and here is completely unnecessary to repeat the question. R_L_P, do not says nothing, and value do not says nothing, the unique way for to know that this method make, is reading the code.

Correct

public void Reduce_Player_Life(int Damage_For_Reduce)

This name is completely good, because, on method name you can to know, that method will to make, and the parameter says the reason for your own existence.

For last, here is our problem, Damage_For_Reduce is a parameter, and Reducle_Player_Life is a public method, so, here will my question, how to documenting this? Come from Damage_For_Reduce? How to documenting this?

This question will to be answered in next Article, Documenting Code Part 2, where I will make about Comments.

Follow-me in Instagram for stay knowing when I create new posts @mauro_developer

You can also leave your feedback, and your suggestion, I will to read all, and I will to answer for you.

Thank you for to read.