U
U
Uncle Bogdan2021-11-04 23:13:34
code review
Uncle Bogdan, 2021-11-04 23:13:34

Is it normal to split everything into so many methods?

There is this code:

The code
using UnityEngine;

public class CharactersSelector : MonoBehaviour
{
    [SerializeField] private Transform _leftCharacterParent;
    [SerializeField] private Transform _rightCharacterParent;

    [SerializeField] private UICharacter _startUICharacter;

    private void Awake()
    {
        SelectCharacter(_leftCharacterParent, _startUICharacter);
        SelectCharacter(_rightCharacterParent, _startUICharacter);
    }

    public void SelectFirstCharacter(UICharacter UICharacter) => SelectCharacter(_leftCharacterParent, UICharacter);
    public void SelectSecondCharacter(UICharacter UICharacter) => SelectCharacter(_rightCharacterParent, UICharacter);

    private void SelectCharacter(Transform parent, UICharacter UICharacter)
    {
        ClearParentChilds(parent);

        InstantiateCharacterOnParent(parent, UICharacter.gameObject);
    }

    private void ClearParentChilds(Transform parent)
    {
        foreach(Transform child in parent)
        {
            Destroy(child.gameObject);
        }
    }

    private void InstantiateCharacterOnParent(Transform parent, GameObject UICharacterGameObject) => Instantiate(UICharacterGameObject, parent);
}

Is it bad that there are a large number of methods? Or is it better the other way around? One tells me that it is necessary that there are few methods, and the other that there are many.

I understand that a lot is better, but I have methods inside of which there is 1 line of code. Is it possible to write without a method?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Bannikov, 2021-11-04
@motkot

looks good except for unused methods.
Better that than piling up code.

One tells me that it is necessary that there are few methods, and the other that there are many.

And why then these same people were not asked why they say so?

N
Nekit Medvedev, 2021-11-08
@NIKROTOS

I usually divide into classes and divide them into layers, I paint the bottom layer into objects to the maximum, the layers above, group the objects (they use the bottom ones as libraries), as a result, having created the bottom layer once, you can not be distracted by it and use the methods of the top-level classes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question