Why do GuardClauses return a value and not the GuardClause
or void
?
#244
-
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. |
Beta Was this translation helpful? Give feedback.
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. |
Beta Was this translation helpful? Give feedback.
-
Aha, I see. Thanks. |
Beta Was this translation helpful? Give feedback.
-
@amal-stack has the answer. It's purely for pragmatic reasons to make constructor assignment simpler. |
Beta Was this translation helpful? Give feedback.
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.