Why do GuardClauses return a value and not the GuardClause or void?
#244
Answered
by
amal-stack
Marvin-Brouwer
asked this question in
Q&A
|
Hi I was just wondering why all the GuardClauses return the original value they were validating? If I compare these approaches, the latter looks more readable to me. |
Answered by
amal-stack
Jan 14, 2023
Replies: 3 comments
|
The purpose of the return value is to simplify value assignment in constructors. class Person
{
public string Name { get; }
public string Age { get; }
public Person(string name, int age)
{
// return values used for assignment to Name and Age
Name = Guard.Against.NullOrWhiteSpace(name);
Age = Guard.Against.NegativeOrZero(age);
}
}Without the return value, the constructor would be of four lines of code instead of two. |
0 replies
Answer selected by
Marvin-Brouwer
|
Aha, I see. Thanks. |
0 replies
|
@amal-stack has the answer. It's purely for pragmatic reasons to make constructor assignment simpler. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment


The purpose of the return value is to simplify value assignment in constructors.
Without the return value, the constructor would be of four lines of code instead of two.
This has already been answered in #158.