Maintainability Guidelines

Methods should not exceed 30 statements (HTA1500)

A method that requires more than 30 statements is simply doing too much or has too many responsibilities. It also requires the human mind to analyze the exact statements to understand what the code is doing. Break it down into multiple small and focused methods with self-explaining names, but make sure the high-level algorithm is still clear.

Make all members private and types internal sealed by default (HTA1501)

To make a more conscious decision on which members to make available to other classes, first restrict the scope as much as possible. Then carefully decide what to expose as a public member or type.

Avoid conditions with double negatives (HTA1502)

Although a property like customer.HasNoOrders makes sense, avoid using it in a negative condition like this:

bool hasOrders = !customer.HasNoOrders;

Double negatives are more difficult to grasp than simple expressions, and people tend to read over the double negative easily.

Name assemblies after their contained namespace (HTA1505)

All DLLs should be named according to the pattern Company.Component.dll where Company refers to your company’s name and Component contains one or more dot-separated clauses. For example AvivaSolutions.Web.Controls.dll.

As an example, consider a group of classes organized under the namespace AvivaSolutions.Web.Binding exposed by a certain assembly. According to this guideline, that assembly should be called AvivaSolutions.Web.Binding.dll.

Exception: If you decide to combine classes from multiple unrelated namespaces into one assembly, consider suffixing the assembly name with Core, but do not use that suffix in the namespaces. For instance, AvivaSolutions.Consulting.Core.dll.

Name a source file to the type it contains (HTA1506)

Use Pascal casing to name the file and don’t use underscores. Don’t include (the number of) generic type parameters in the file name.

Limit the contents of a source code file to one type (HTA1507)

For interfaces, enums, classes, etc. that are public, place each one in a separate source file.

Exception: Nested types should be part of the same file.

Exception: Types that only differ by their number of generic type parameters should be part of the same file.

Name a source file to the logical function of the partial type (HTA1508)

When using partial types and allocating a part per file, name each file after the logical part that part plays. For example:

// In MyClass.cs
public partial class MyClass
{...}

// In MyClass.Designer.cs
public partial class MyClass
{...}

Use using statements instead of fully qualified type names (HTA1510)

Limit usage of fully qualified type names to prevent name clashing. For example, don’t do this:

var list = new System.Collections.Generic.List<string>();

Instead, do this:

using System.Collections.Generic;

var list = new List<string>();

If you do need to prevent name clashing, use a using directive to assign an alias:

using Label = System.Web.UI.WebControls.Label;

Don’t use “magic” numbers (HTA1515)

Don’t use literal values, either numeric or strings, in your code, other than to define symbolic constants. For example:

public class Whatever
{
	public static readonly Color PapayaWhip = new Color(0xFFEFD5);
	public const int MaxNumberOfWheels = 18;
	public const byte ReadCreateOverwriteMask = 0b0010_1100;
}

Strings intended for logging or tracing are exempt from this rule. Literals are allowed when their meaning is clear from the context, and not subject to future changes, For example:

mean = (a + b) / 2; // okay
WaitMilliseconds(waitTimeInSeconds * 1000); // clear enough

If the value of one constant depends on the value of another, attempt to make this explicit in the code.

public class SomeSpecialContainer
{
	public const int MaxItems = 32;
	public const int HighWaterMark = 3 * MaxItems / 4; // at 75%
}

Note: An enumeration can often be used for certain types of symbolic constants.

Only use var when the type is evident (HTA1520)

Use var for anonymous types (typically resulting from a LINQ query), or if the type is evident. Never use var for built-in types.

// Projection into anonymous type.
var largeOrders =
	from order in dbContext.Orders
	where order.Items.Count > 10 && order.TotalAmount > 1000
	select new { order.Id, order.TotalAmount };

// Built-in types.
bool isValid = true;
string phoneNumber = "(unavailable)";
uint pageSize = Math.Max(itemCount, MaxPageSize);

// Types are evident.
var customer = new Customer();
var invoice = Invoice.Create(customer.Id);
var user = sessionCache.Resolve<User>("john.doe@mail.com");
var subscribers = new List<Subscriber>();
var summary = shoppingBasket.ToOrderSummary();

// All other cases.
IQueryable<Order> recentOrders = ApplyFilter(order => order.CreatedAt > DateTime.Now.AddDays(-30));
LoggerMessage message = Compose(context);
ReadOnlySpan<char> key = ExtractKeyFromPair("email=john.doe@mail.com");
IDictionary<Category, Product> productsPerCategory =
	shoppingBasket.Products.ToDictionary(product => product.Category);

Declare and initialize variables as late as possible (HTA1521)

Avoid the C and Visual Basic styles where all variables have to be defined at the beginning of a block, but rather define and initialize each variable at the point where it is needed.

Assign each variable in a separate statement (HTA1522)

Don’t use confusing constructs like the one below:

var result = someField = GetSomeMethod();

Exception: Multiple assignments per statement are allowed by using out variables, is-patterns or deconstruction into tuples. Examples:

bool success = int.TryParse(text, out int result);

if ((items[0] is string text) || (items[1] is Action action))
{
}

(string name, string value) = SplitNameValuePair(text);

Favor object and collection initializers over separate statements (HTA1523)

Instead of:

var startInfo = new ProcessStartInfo("myapp.exe");
startInfo.StandardOutput = Console.Output;
startInfo.UseShellExecute = true;

var countries = new List();
countries.Add("Netherlands");
countries.Add("United States");

var countryLookupTable = new Dictionary<string, string>();
countryLookupTable.Add("NL", "Netherlands");
countryLookupTable.Add("US", "United States");

Use Object and Collection Initializers:

var startInfo = new ProcessStartInfo("myapp.exe")
{
	StandardOutput = Console.Output,
	UseShellExecute = true
};

var countries = new List { "Netherlands", "United States" };

var countryLookupTable = new Dictionary<string, string>
{
	["NL"] = "Netherlands",
	["US"] = "United States"
};

Don’t make explicit comparisons to true or false (HTA1525)

It is usually bad style to compare a bool-type expression to true or false. For example:

while (condition == false) // wrong; bad style
while (condition != true) // also wrong
while (((condition == true) == true) == true) // where do you stop?
while (condition) // OK

Comparing against null and constants in if statements (HTA1526)

Avoid placing constants or the null keyword as the left hand side argument of an if statement. For readability, place nulls and constants on the right hand side of the statement. For example:

Don’t do this: if (null == myParameter || MyConstant == myParameter)

Instead, do this: if (myParameter == null || myParameter == MyConstant)

Don’t change a loop variable inside a for loop (HTA1530)

Updating the loop variable within the loop body is generally considered confusing, even more so if the loop variable is modified in more than one place.

for (int index = 0; index < 10; ++index)
{
	if (someCondition)
	{
		index = 11; // Wrong! Use 'break' or 'continue' instead.
	}
}

Avoid nested loops (HTA1532)

A method that nests loops is more difficult to understand than one with only a single loop. In fact, in most cases nested loops can be replaced with a much simpler LINQ query that uses the from keyword twice or more to join the data.

Some cases where you do need to use nested loops, are 2D arrays.

Always add a block after the keywords if, else, do, while, for, foreach and case (HTA1535)

Please note that this also avoids possible confusion in statements of the form:

if (isActive) if (isVisible) Foo(); else Bar(); // which 'if' goes with the 'else'?

// The right way:
if (isActive)
{
	if (isVisible)
	{
		Foo();
	}
	else
	{
		Bar();
	}
}

Always add a default block after the last case in a switch statement (HTA1536)

Add a descriptive comment if the default block is supposed to be empty. Moreover, if that block is not supposed to be reached throw an InvalidOperationException to detect future changes that may fall through the existing cases. This ensures better code, because all paths the code can travel have been thought about.

void Foo(string answer)
{
	switch (answer)
	{
		case "no":
		{
		  Console.WriteLine("You answered with No");
		  break;
		}

		case "yes":
		{
		  Console.WriteLine("You answered with Yes");
		  break;
		}

		default:
		{
		  // Not supposed to end up here.
		  throw new InvalidOperationException("Unexpected answer " + answer);
		}
	}
}

Finish every if-else-if statement with an else clause (HTA1537)

For example:

void Foo(string answer)
{
	if (answer == "no")
	{
		Console.WriteLine("You answered with No");
	}
	else if (answer == "yes")
	{
		Console.WriteLine("You answered with Yes");
	}
	else
	{
		// What should happen when this point is reached? Ignored? If not,
		// throw an InvalidOperationException.
	}
}

Be reluctant with multiple return statements (HTA1540)

One entry, one exit is a sound principle and keeps control flow readable. However, if the method body is very small and complies with guideline HTA1500 then multiple return statements may actually improve readability over some central boolean flag that is updated at various points.

Don’t use an if-else construct instead of a simple (conditional) assignment (HTA1545)

Express your intentions directly. For example, rather than:

bool isPositive;

if (value > 0)
{
	isPositive = true;
}
else
{
	isPositive = false;
}

write:

bool isPositive = value > 0;

Or instead of:

string classification;

if (value > 0)
{
	classification = "positive";
}
else
{
	classification = "negative";
}

return classification;

write:

return value > 0 ? "positive" : "negative";

Or instead of:

int result;

if (offset == null)
{
	result = -1;
}
else
{
	result = offset.Value;
}

return result;

write:

return offset ?? -1;

Or instead of:

private DateTime? firstJobStartedAt;

public void RunJob()
{
	if (firstJobStartedAt == null)
	{
		firstJobStartedAt = DateTime.UtcNow;
	}
}

write:

private DateTime? firstJobStartedAt;

public void RunJob()
{
	firstJobStartedAt ??= DateTime.UtcNow;
}

Or instead of:

if (employee.Manager != null)
{
	return employee.Manager.Name;
}
else
{
	return null;
}

write:

return employee.Manager?.Name;

Prefer interpolated strings over concatenation or string.Format. (HTA1546)

Since .NET 6, interpolated strings are optimized at compile-time, which inlines constants and reduces memory allocations due to boxing and string copying.

// GOOD
string result = $"Welcome, {firstName} {lastName}!";

// BAD
string result = string.Format("Welcome, {0} {1}!", firstName, lastName);

// BAD
string result = "Welcome, " + firstName + " " + lastName + "!";

// BAD
string result = string.Concat("Welcome, ", firstName, " ", lastName, "!");

Encapsulate complex expressions in a property, method or local function (HTA1547)

Consider the following example:

if (member.HidesBaseClassMember && member.NodeType != NodeType.InstanceInitializer)
{
	// do something
}

In order to understand what this expression is about, you need to analyze its exact details and all of its possible outcomes. Obviously, you can add an explanatory comment on top of it, but it is much better to replace this complex expression with a clearly named method:

if (NonConstructorMemberUsesNewKeyword(member))
{
	// do something
}

private bool NonConstructorMemberUsesNewKeyword(Member member)
{
	return member.HidesBaseClassMember &&
		member.NodeType != NodeType.InstanceInitializer;
}

You still need to understand the expression if you are modifying it, but the calling code is now much easier to grasp.

Call the more overloaded method from other overloads (HTA1551)

This guideline only applies to overloads that are intended to provide optional arguments. Consider, for example, the following code snippet:

public class MyString
{
	private string someText;

	public int IndexOf(string phrase)
	{
		return IndexOf(phrase, 0);
	}

	public int IndexOf(string phrase, int startIndex)
	{
		return IndexOf(phrase, startIndex, someText.Length - startIndex);
	}

	public virtual int IndexOf(string phrase, int startIndex, int count)
	{
		return someText.IndexOf(phrase, startIndex, count);
	}
}

The class MyString provides three overloads for the IndexOf method, but two of them simply call the one with one more parameter. Notice that the same rule applies to class constructors; implement the most complete overload and call that one from the other overloads using the this() operator. Also notice that the parameters with the same name should appear in the same position in all overloads.

Important: If you also want to allow derived classes to override these methods, define the most complete overload as a non-private virtual method that is called by all overloads.

Only use optional parameters to replace overloads (HTA1553)

The only valid reason for using C# 4.0’s optional parameters is to replace the example from rule HTA1551 with a single method like:

public virtual int IndexOf(string phrase, int startIndex = 0, int count = -1)
{
	int length = count == -1 ? someText.Length - startIndex : count;
	return someText.IndexOf(phrase, startIndex, length);
}

Since strings, collections and tasks should never be null according to rule HTA1135, if you have an optional parameter of these types with default value null then you must use overloaded methods instead.

Strings, unlike other reference types, can have non-null default values. So an optional string parameter may be used to replace overloads with the condition of having a non-null default value.

Regardless of optional parameters’ types, following caveats always apply:

1) The default values of the optional parameters are stored at the caller side. As such, changing the default argument without recompiling the calling code will not apply the new default value. Unless your method is private or internal, optional parameters must always default to their type-default (e.g. 0 for numerical types, false for boolean, etc.).

2) If optional parameters cause the method to follow and/or exit from alternative paths, overloaded methods are probably a better fit for your case.

Do not use optional parameters in interface methods or their concrete implementations (HTA1554)

When an interface method defines an optional parameter, its default value is discarded during overload resolution unless you call the concrete class through the interface reference.

When a concrete implementation of an interface method sets a default argument for a parameter, the default value is discarded during overload resolution if you call the concrete class through the interface reference.

See the series on optional argument corner cases by Eric Lippert (part one, two, three, four) for more details.

Avoid using named arguments (HTA1555)

C# 4.0’s named arguments have been introduced to make it easier to call COM components that are known for offering many optional parameters. If you need named arguments to improve the readability of the call to a method, that method is probably doing too much and should be refactored.

Exception: The only exception where named arguments improve readability is when calling a method of some code base you don’t control that has a bool parameter, like this:

object[] myAttributes = type.GetCustomAttributes(typeof(MyAttribute), inherit: false);

Don’t declare signatures with too many* parameters (HTA1561)

To keep constructors, methods, delegates and local functions small and focused, do not use too many parameters. Do not use tuple parameters. Do not return tuples with more than two elements.

Methods declared with too many parameters can be a sign that the method is doing too much. Similarly, a constructor which accepts a large number of injected services may be an indication that the class has too much responsibility.

Additionally, methods which accept a large number of primitive types can be an indication that it is time to introduce an object to encapsulate these values.

The ethos of this guideline is not to put an arbitrary hard-limit on the number of arguments, but instead to encourage better design to keep a code-base understandable and maintainable as it grows.

*“too many” is subjective. As a community of practice, we have currently agreed upon more than 5.

Exception: A parameter that is a collection of tuples is allowed.

Exception: Classes that use dependency injection may reasonably have larger number of constructor parameters but it is still a good idea to look for ways to minimize your class dependencies.

Don’t use ref or out parameters (HTA1562)

They make code less understandable and might cause people to introduce bugs. Instead, return compound objects or tuples.

Exception: Calling and declaring members that implement the TryParse pattern is allowed. For example:

bool success = int.TryParse(text, out int number);

Avoid signatures that take a bool parameter (HTA1564)

Consider the following method signature:

public Customer CreateCustomer(bool platinumLevel)
{
}

On first sight this signature seems perfectly fine, but when calling this method you will lose this purpose completely:

Customer customer = CreateCustomer(true);

Often, a method taking such a bool is doing more than one thing and needs to be refactored into two or more methods. An alternative solution is to replace the bool with an enumeration.

Don’t use parameters as temporary variables (HTA1568)

Never use a parameter as a convenient variable for storing temporary state. Even though the type of your temporary variable may be the same, the name usually does not reflect the purpose of the temporary variable.

Prefer is patterns over as operations (HTA1570)

If you use ‘as’ to safely upcast an interface reference to a certain type, always verify that the operation does not return null. Failure to do so may cause a NullReferenceException at a later stage if the object did not implement that interface. Pattern matching syntax prevents this and improves readability. For example, instead of:

var remoteUser = user as RemoteUser;
if (remoteUser != null)
{
}

write:

if (user is RemoteUser remoteUser)
{
}

Don’t comment out code (HTA1575)

Never check in code that is commented out. Instead, use a work item tracking system to keep track of some work to be done. Nobody knows what to do when they encounter a block of commented-out code. Was it temporarily disabled for testing purposes? Was it copied as an example? Should I delete it?

Don’t leave unused/non-functional code in code base (HTA1576)

Never leave any non-functional code behind in the codebase. This can cause confusion and maintanability issues. If you delete the one call you have in your code to a specific method, then that method should also be removed. Don’t leave it behind for a ‘may be’ scenario.’

Write code that is easy to debug (HTA1580)

Because debugger breakpoints cannot be set inside expressions, avoid overuse of nested method calls. For example, a line like:

string result = ConvertToXml(ApplyTransforms(ExecuteQuery(GetConfigurationSettings(source))));

requires extra steps to inspect intermediate method return values. On the other hand, were this expression broken into intermediate variables, setting a breakpoint on one of them would be sufficient.

Note This does not apply to chaining method calls, which is a common pattern in fluent APIs.