Member Design Guidelines

Edit this page

Allow properties to be set in any order (AV1100)

Properties should be stateless with respect to other properties, i.e. there should not be a difference between first setting property DataSource and then DataMember or vice-versa.

Use a method instead of a property (AV1105)

  • If the work is more expensive than setting a field value.
  • If it represents a conversion such as the Object.ToString method.
  • If it returns a different result each time it is called, even if the arguments didn’t change. For example, the NewGuid method returns a different value each time it is called.
  • If the operation causes a side effect such as changing some internal state not directly related to the property (which violates the Command Query Separation principle).

Exception: Populating an internal cache or implementing lazy-loading is a good exception.

Don’t use mutually exclusive properties (AV1110)

Having properties that cannot be used at the same time typically signals a type that represents two conflicting concepts. Even though those concepts may share some of their behavior and states, they obviously have different rules that do not cooperate.

This violation is often seen in domain models and introduces all kinds of conditional logic related to those conflicting rules, causing a ripple effect that significantly increases the maintenance burden.

A property, method or local function should do only one thing (AV1115)

Similarly to rule AV1000, a method body should have a single responsibility.

Don’t expose stateful objects through static members (AV1125)

A stateful object is an object that contains many properties and lots of behavior behind it. If you expose such an object through a static property or method of some other object, it will be very difficult to refactor or unit test a class that relies on such a stateful object. In general, introducing a construct like that is a great example of violating many of the guidelines of this chapter.

A classic example of this is the HttpContext.Current property, part of ASP.NET. Many see the HttpContext class as a source of a lot of ugly code.

Return interfaces to unchangeable collections (AV1130)

You generally don’t want callers to be able to change an internal collection, so don’t return arrays, lists or other collection classes directly. Instead, return an IEnumerable<T>, IAsyncEnumerable<T>, IQueryable<T>, IReadOnlyCollection<T>, IReadOnlyList<T>, IReadOnlySet<T> or IReadOnlyDictionary<TKey, TValue>.

Exception: Immutable collections such as ImmutableArray<T>, ImmutableList<T> and ImmutableDictionary<TKey, TValue> prevent modifications from the outside and are thus allowed.

Properties, arguments and return values representing strings, collections or tasks should never be null (AV1135)

Returning null can be unexpected by the caller. Always return an empty collection or an empty string instead of a null reference. When your member returns Task or Task<T>, return Task.CompletedTask or Task.FromResult(). This also prevents cluttering your code base with additional checks for null, or even worse, string.IsNullOrEmpty().

Define parameters as specific as possible (AV1137)

If your method or local function needs a specific piece of data, define parameters as specific as that and don’t take a container object instead. For instance, consider a method that needs a connection string that is exposed through a central IConfiguration interface. Rather than taking a dependency on the entire configuration, just define a parameter for the connection string. This not only prevents unnecessary coupling, it also improves maintainability in the long run.

Note: An easy trick to remember this guideline is the Don’t ship the truck if you only need a package.

Consider using domain-specific value types rather than primitives (AV1140)

Instead of using strings, integers and decimals for representing domain-specific types such as an ISBN number, an email address or amount of money, consider creating dedicated value objects that wrap both the data and the validation rules that apply to it. By doing this, you prevent ending up having multiple implementations of the same business rules, which both improves maintainability and prevents bugs.