0

I was looking at a piece of error handling code that looks like this:

if (condition) {
    thereIsAProblem = true;
    problemDescription = "x";
}

if (!thereIsAProblem && condition2) {
    thereIsAProblem = true;
    problemDescription = "y";
}

And I got to wondering whether there is a way to define a local variable called thereIsNotAProblem that is dynamically based on the value of thereIsAProblem. In other words:

var thereIsAProblem = false;
var thereIsNotAProblem = *** some expression... ***

Console.WriteLine(thereIsNotAProblem);   // true
thereIsAProblem = true;
Console.WriteLine(thereIsNotAProblem);   // false

if (thereIsNotAProblem && condition3) {
  ..
}

Is there some expression that can be entered on the line above that would assign the value of thereIsNotAProblem to be a dynamic formula based on !thereIsAProblem, and still allow thereIsNotAProblem to be supplied anywhere a bool value is required?

3 Answers 3

2

Not quite... but you could make it a delegate instead:

Func<bool> thereIsNotAProblem = () => { /* some expression */ };

Console.WriteLine(thereIsNotAProblem());   // true
thereIsAProblem = true;
Console.WriteLine(thereIsNotAProblem());   // false

Note how now each time was want to evaluate thereIsNotAProblem we invoke the delegate... which evaluates the expression to get a bool value.

Sign up to request clarification or add additional context in comments.

1 Comment

I suspected that was the only solution. Thanks!
1

Whilst you can do this by declaring a lambda,

var thereIsAProblem = false;
Func<bool> thereIsNotAProblem = () => !thereIsAProblem;

I'd argue you shouldn't. thereIsAProblem and thereIsNotAProblem look very similar and so one could easily be misread for the other. the use of ! to negate a variable with a positive name is well understood and easy to read and should lead to less bugs.

I'd further argue that a better solution is the "fail fast" approach of returning as soon as there is a problem, avoiding the need to test for an earlier problem in the first place:

if (condition) 
{
    problemDescription = "x";
    return;
}

if (condition2) 
{
    problemDescription = "y";
    return;
}
...

Comments

0

You could use do something like the following (also see on .NET Fiddle https://dotnetfiddle.net/E9X6XJ )

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        Console.WriteLine("YourQuestion() returns " + YourQuestion());
        Console.WriteLine("AnswerOne() returns " + AnswerOne());
        Console.WriteLine("AnswerTwo() returns " + AnswerTwo());
    }

    private static bool condition1()
    {
        return false;
    }

    private static bool condition2()
    {
        return true;
    }

    private static bool condition3()
    {
        return true;
    }


    public static string YourQuestion() 
    {
        var thereIsAProblem = false;
        var problemDescription = "";        

        if (condition1()) {
            thereIsAProblem = true;
            problemDescription = "x";
        }

        if (!thereIsAProblem && condition2()) {
            thereIsAProblem = true;
            problemDescription = "y";
        }

        return problemDescription;
    }

    public static string AnswerOne() 
    {
        return checkCondition1() ??
               checkCondition2() ??
               checkCondition3();
    }   

    private static string checkCondition1()
    {       
        return condition1() ? "x" : null;       
    }

    private static string checkCondition2()
    {       
        return condition2() ? "y" : null;
    }

    private static string checkCondition3()
    {       
        return condition3() ? "z" : null;           
    }


    public static string AnswerTwo()
    {       
        var conditionChecks = new Dictionary<string,Func<bool>>();
        conditionChecks.Add("x",condition1);
        conditionChecks.Add("y",condition2);
        conditionChecks.Add("z",condition3);
        foreach (var check in conditionChecks)
        {           
            if (check.Value())
            {
                return check.Key;
            }
        }
        return null;
    }

}

1 Comment

These show different design options that may lead you down a better path to the one you are currently following. See Design Patterns.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.