Skip to content

Reason for returning 'input' from the Guard clauses? #158

Answered by ardalis
jshergal asked this question in Q&A
Discussion options

You must be logged in to vote

In constructors most of the time you're doing some check and then assigning the input to a field. Guards get used in constructors a lot - it's a primary use case. So going from

public void SomeClass(IDependency a, IDependency b, IDependency c)
{
  Guard.Against.Null(a, nameof(a);
  Guard.Against.Null(a, nameof(b);
  Guard.Against.Null(a, nameof(c);

  _a = a;
 _b = b;
 _c = c;
}

to this:

public void SomeClass(IDependency a, IDependency b, IDependency c)
{
  _a = Guard.Against.Null(a, nameof(a);
  _b = Guard.Against.Null(a, nameof(b);
  _c = Guard.Against.Null(a, nameof(c);
}

is a big win.

I can see how it could cause some annoyance in your methods, though. Using a discard is probably yo…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@jshergal
Comment options

Answer selected by jshergal
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants