[C# Design Patterns] Failing fast

catgirl

Administrator
Staff member
Administrator
May 28, 2020
155
28
22
United States
envyforums.net
When designing a C# application you will inevitably have to accept some sort of user input for the application to be useful. In this post I talk about a design pattern known as failing fast.

What is failing fast?

As data is moved across different methods throughout your application you will want to validate the input (make sure it follows specific formatting rules) and throw an ArgumentException as quickly as possible. This reduces difficult to trace bugs by knowing which input causes the error right away, after avoiding it by informing the user their input was not accepted by the method called.

How to check user-input?

When writing a method that accepts user-input directly I recommend implementing the following types of validations at a minimum to reduce bugs.

  • A length check: If the amount of data too long or two short?
  • A regular expression check: Do you know what the input will look like? Whenever possible use a regular expression to check if the user’s input is valid and throw an exception if the input does not match the required format.
  • A character-set check: Is the user inputting invalid unicode characters or using characters from another alphabetic (e.g. crylic instead of latin characters), if they are not using the expected characters consider throwing an exception.
  • A data-type check: Don’t default to string input. Whenever possible use an integer, boolean, float, etc. I often see new developers using string data for every single data input type. The different data-types are here to help you, take advantage of them.
Take a look as the following code example for how you might implement one of these checks in your application.

C#:
void PrintAString(string name)
{
  if (name.Length > 100)
  {
    throw new ArgumentException("Invalid length. String cannot be above 100 characters!");
  }
}
How to throw an exception?

Once input has failed one of your checks, you will need to throw an exception. To do this include the following code throw new ArgumentException("Invalid length. String cannot be above 100 characters!") changing the message to reflect the error in question.

Notice that we are throwing a new object from the ArgumentException class, exceptions (from a class that inherits behavior of the Exception class) are just objects and are thrown in lieu of the expected return value of the method. When running your code within a debugger you can view this object for details about what failed and why it failed. That’s incredibly useful when handled properly. For example we wouldn’t want a web app to fail because of invalid user input, so throw the exception and have the web application display a helpful error message (logging the issue where relevant).

Conclusion

By failing fast you are aware of the limitations in your application, you have more safety checks in place to reduce bugs and security issues in your programs, and will have an easier time debugging your application when an issue occurs.