U
U
Uncle Bogdan2021-09-11 21:28:34
code review
Uncle Bogdan, 2021-09-11 21:28:34

How to make normal code out of this method?

How to make normal, beautiful, readable and generally the best code out of this?

PS Added C# to the tag, since special knowledge of Unity is not needed here

public List<Cell> CalculateVariants(List<Vector2> movevariants, List<Vector2> attackvariants)
    {
        var variants = new List<Cell>();

        foreach (Vector2 offset in movevariants)
        {
            if (GridRepository.Cells.Find(cell => cell.Position == cell.Position + offset && cell.Unit == null) != null)
            {
                variants.Add(GridRepository.Cells.Find(cell => cell.Position == cell.Position + offset && cell.Unit == null));
            }
        }

        foreach (Vector2 offset in attackvariants)
        {
            if (GridRepository.Cells.Find(cell => cell.Position == cell.Position + offset && cell.Unit != null) != null)
            {
                variants.Add(GridRepository.Cells.Find(cell => cell.Position == cell.Position + offset && cell.Unit == null));
            }
        }

        return variants;
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2021-09-11
@motkot

Get rid of duplicates in the form of Find, at least. Cache as much as possible.

D
Developer, 2021-09-11
@samodum

use LINQ

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question