GithubHelp home page GithubHelp logo

boolean guard question about guard HOT 3 CLOSED

safakgur avatar safakgur commented on May 20, 2024
boolean guard question

from guard.

Comments (3)

safakgur avatar safakgur commented on May 20, 2024

Hi, I'm glad you liked the library.

Guard.Argument overloads expect the value and name of a single argument, and failed preconditions throw exceptions for that specific argument. The reason why () => myStuff || myOtherStuff doesn't work is that it does not represent a single argument.

var a = 1;
var b = 2;

// The following lambda expression is a MemberExpression that represents the variable "a".
// Guard.Argument compiles that expression to get the value, 1.
Guard.Argument(() => a);

// The following lambda expression is a BinaryExpression that adds "a" and "b".
// It doesn't represent a single parameter which Guard can get the value and name of.
Guard.Argument(() => a + b);

If your method accept multiple arguments, you need to have a Guard.Argument(...) call for each. This allows the ParamName and Message properties of the exceptions thrown to be unique per argument.

If you're sure that you want to threat a combination of arguments as a single argument, you can use the Guard.Argument overload that accepts the argument value and name as separate parameters.

void Foo(bool a, bool b)
{
    // Here we specify a combination of values, and a custom name
    // to be included in exception messages.
    Guard.Argument(a || b, "a or b").True();
}

Update: However, I would recommend this approach instead:

void Foo(bool a, bool b)
{
    // Here, both a and b can be false if the other one is not.
    // Since "a" is the first argument, we'll blame "b" if it's not consistent with "a".
    if (!a)
        Guard.Argument(() => b).True("b can only false when a is true.");
}

I hope this helps, please let me know if you have any more questions.

from guard.

eiredrake avatar eiredrake commented on May 20, 2024

ah so the 'e' in question that it's complaining about is the name of the argument? Yep that helps. The check was to make sure a particular JSON fragment had one of two options in it or not. A sanity check basically. I appreciate the help!

from guard.

safakgur avatar safakgur commented on May 20, 2024

Kind of.

e is the lambda expression you specify. It's body is a MemberExpression when you write it like () => foo. A member expression has a name we can use (foo). We can also compile the expression to get its value, so we can have both the name and value of the argument - it's just for convenience over the overload that accepts the name and value separately.

But when you write e like () => foo || bar, it's body becomes a BinaryExpression. Now it's the logical OR result of two different member expressions. We can still compile it and get a value but we now don't have a name, because the expression doesn't point to a single named argument.

Is the JSON fragment a Newtonsoft JObject that you accept as a parameter? Maybe you can use member guards to validate it. But I guess writing a "JSON to model" parser function and using Guard.Wrap with it would make more sense. Though you'd have to throw exceptions manually for invalid JSON values in that function.

I hope this clarifies things.

from guard.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.