'Setting bool to false VS checking if it's already false before setting it

I know that this may be over-optimizationbut still I'd like to know what's better.

So let's add a bit of context. I'm working on a game on Unity which uses C# to write the game scripts and there are methods made by Unity that run at every frame of the game. For the game I'm working on I'm using triggers in my code. They are simple bool values that start as false. When something specific happens in the game the specific trigger will be set to true, will activate an event instantaneously and be set to false again. This all happens in a single frame.

So it means that at the end of every frame i have to reset my triggers to false. This is where my doubt is. Should I just set all the triggers to false indifferently to their current value or should I add an IF condition to check if they aren't already false?



Solution 1:[1]

Generally speaking, I suggest you check a boolean before setting it. I don't have any data on which is more performant, but it has a functional advantage. If you're working with a field, checking and setting a boolean value has no side effects. However, if you're working with a property, the getter and setter can have some side effects that can lead to unwanted behaviour if you set them every frame. It's not always immediately clear if you're working with fields or properties if you're just using that boolean.

I would advise you not to use flag booleans inside the Update method. The Update method should only do things that have to be done whenever the object is active. I assume you have some code that sets the flag if something happens and in your Update method you do something until another condition is met and you reset that flag.

// This is my flag variable.
public bool isCounting;
public int maximum;
private int _currentCount;

private void Update()
{
    if (IsConditionMet()) {
        // Should I check if isCounting is already false before setting?
        isCounting = false;
    }

    if (isCounting) {
        DoWork();
    }
    else
    {
        return;
    }
}

private bool IsConditionMet()
{
    return _currentCount >= maximum;
}

private void DoWork()
{
    _currentCount++;
}

When some other code sets the flag isCounting to true, _currentCount will be increased until it hits the maximum and stops counting. There is exactly one frame when the counting starts and one frame when the counting stops. During all other frames, the Update method checks the flag variable and does nothing else.

Instead you could be working with coroutines and calling methods:

public void StartCounting()
{
    StartCoroutine(Count());
}

private IEnumerable Count()
{
    while (!IsConditionMet())
    {
        DoWork();
        yield new WaitForSeconds(1);
    }
}

This code exposes a public method called StartCounting. To start counting, you call StartCounting instead of setting the public field isCounting. The method then starts a coroutine that executes outside of the Update code. The coroutine Count checks if the condition is met and continues to do work if not. When it's done working, the coroutine waits for a second and tries again until _currentCount reaches maximum. This way, you don't have to worry about checking and setting a flag value and instead call the StartCounting method once and let it work until it finishes.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 G_hi3