HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

A one change boolean value

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
onechangevalueboolean

Problem

I have the case of repeated very similar logic that is used to check if a boolean value was changed from the initial value and prevent it from changing back, or to summarize multiple boolean return values like:

bool SomeMethod()
{
    bool endResult = true;
    foreach(var obj in objs)
    {
        bool result = obj.Method();
        if(!result)
            endResult = false;
    }
    return endResult;
}


I reworked the logic into a class where a boolean can only be changed from the initial value, so that the boolean logic is all in one place. The IsChanged property is for LINQ queries.

public class OneUseBool
{
    private readonly bool _initValue;

    public OneUseBool(bool init)
    {
        _initValue = init;
        _currentValue = init;
        _isChanged = false;
    }

    private bool _currentValue;
    public bool CurrentValue
    {
        get { return _currentValue; }
        set
        {
            if (_initValue != value)
            {
                _currentValue = value;
                IsChanged = true;
            }
        }
    }

    private bool _isChanged;
    public bool IsChanged
    {
        get { return _isChanged; }
        private set
        {
            if(!_isChanged)
                _isChanged = value;
        }
    }
}


So the original code gets changed to:

bool SomeMethod()
{
    OneUseBool endResult = new OneUseBool(true);
    foreach(var obj in objs)
    {
        endResult.CurrentValue = obj.Method();
    }
    return endResult.CurrentValue;
}


Is this a good way to do this sort of logic with bools, or am I just overcomplicating a simple problem?

Solution

If you can only change a bool once, and the entire purpose of a loop is to set the bool, then you should just exit the loop after the bool gets changed. There's no point in looping more than you need to, and you also don't have to worry about the bool getting switched back. So you can write your example like this:

bool SomeMethod()
{
    foreach(var obj in objs)
    {
        if(!obj.Method())
        {
            return false;
        }
    }
    return true;
}


You can use Enumerable.All() for this logic:

bool SomeMethod()
{
    return objs.All(x => x.Method());
}


An alternative to if(!x) y = x; in your situation is y = y && x;

Code Snippets

bool SomeMethod()
{
    foreach(var obj in objs)
    {
        if(!obj.Method())
        {
            return false;
        }
    }
    return true;
}
bool SomeMethod()
{
    return objs.All(x => x.Method());
}

Context

StackExchange Code Review Q#128308, answer score: 7

Revisions (0)

No revisions yet.