-
Notifications
You must be signed in to change notification settings - Fork 156
Guard APIs docs #302
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
michael-hawker
merged 10 commits into
MicrosoftDocs:master
from
Sergio0694:feature/guard-apis
Jun 5, 2020
Merged
Guard APIs docs #302
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
993a829
Added Guard APIs docs
Sergio0694 3e9f73f
Added details for APIs used in the sample
Sergio0694 78e2da3
Added .NET Standard mention
Sergio0694 7384d3e
Fixed a copy-paste fail
Sergio0694 aac780b
Grouped APIs by category
Sergio0694 3a1849e
Fixed <T> display in markdown
Sergio0694 4c11f26
Removed word repetition
Sergio0694 61452b3
Added more info on range/interval APIs
Sergio0694 32754b2
Updated Guard sample with new naming changes
Sergio0694 a919c11
Update docs/developer-tools/Guard.md
michael-hawker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| --- | ||
| title: Guard | ||
| author: Sergio0694 | ||
| description: Helper methods to verify conditions when running code | ||
| keywords: windows 10, uwp, windows community toolkit, uwp community toolkit, uwp toolkit, debug, net core, net standard | ||
| dev_langs: | ||
| - csharp | ||
| --- | ||
|
|
||
| # Guard | ||
|
|
||
| The [Guard](https://docs.microsoft.com/dotnet/api/microsoft.toolkit.diagnostics.guard) can be used to validate method arguments in a streamlined manner, which is also faster, less verbose, more expressive and less error prone than manually writing checks and throwing exceptions. | ||
|
|
||
| ## How it works | ||
|
|
||
| These `Guard` APIs are built with three core principles in mind: | ||
|
|
||
| - Being **fast**. To achieve that, all the APIs have been designed to produce as little code as possible in the caller, and each single Guard API will (almost always) be inlined. Furthermore, specialized methods are generated with T4 templates to achieve the most efficient assembly code possible when working with common types (eg. primitive numeric types). | ||
| - Being **helpful**. Each `Guard` API produces a detailed exception message that clearly specifies what went wrong, along with additional info (eg. current variable values), when applicable. | ||
| - Being **intuitive**. To achieve this, all the `Guard` APIs have expressive and self-explanatory names that make it immediately clear what each API is supposed to do. This shifts the burden of writing checks away from the developers, letting them express conditions using natural language. | ||
|
|
||
| ## Syntax | ||
|
|
||
| Here is a sample method, with some checks being done with explicit checks and manual throw statements: | ||
|
|
||
| ```csharp | ||
| public static void SampleMethod(int[] array, int index, Span<int> span, string text) | ||
| { | ||
| if (array is null) | ||
| { | ||
| throw new ArgumentNullException(nameof(array), "The array must not be null"); | ||
| } | ||
|
|
||
| if (array.Length >= 10) | ||
| { | ||
| throw new ArgumentException($"The array must have less than 10 items, had a size of {array.Length}", nameof(array)); | ||
| } | ||
|
|
||
| if (index < 0 || index >= array.Length) | ||
| { | ||
| throw new ArgumentOutOfRangeException(nameof(index), $"The index must be in the [0, {array.Length}) range, was {index}"); | ||
| } | ||
|
|
||
| if (span.Length < array.Length) | ||
| { | ||
| throw new ArgumentException($"The target span is shorter than the input array, had a length of {span.Length}", nameof(span)); | ||
| } | ||
|
|
||
| if (string.IsNullOrEmpty(text)) | ||
| { | ||
| throw new ArgumentException("The input text can't be null or empty", nameof(text)); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| And here is the same method, but using the new `Guard.APIs` to validate the input arguments: | ||
|
|
||
| ```csharp | ||
| public static void SampleMethod(int[] array, int index, Span<int> span, string text) | ||
| { | ||
| Guard.IsNotNull(array, nameof(array)); | ||
| Guard.HasSizeAtLeast(array, 10, nameof(array)); | ||
| Guard.IsInRangeFor(index, array, nameof(index)); | ||
| Guard.HasSizeLessThanOrEqualTo(array, span, nameof(span)); | ||
| Guard.IsNotNullOrEmpty(text, nameof(text)); | ||
Sergio0694 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| ``` | ||
|
|
||
| The `Guard` APIs will perform the required checks in the fastest way possible, and will throw the appropriate exception with a well formated message if they fail. | ||
|
|
||
| ## Methods | ||
|
|
||
| There are dozens of different APIs and overloads in the `Guard` class, here are a few of them: | ||
|
|
||
| | Methods | Return Type | Description | | ||
| | -- | -- | -- | | ||
| | IsNotNull<T>(T?, string) | void | Asserts that the input value is not null | | ||
| | IsOfType<T>(object, string) | void | Asserts that the input value is of a specific type | | ||
| | IsReferenceEqualTo<T>(T, T, string) | void | Asserts that the input value must be the same instance as the target value | | ||
| | IsTrue(bool, string) | void | Asserts that the input value must be true | | ||
| | IsEqualTo<T>(T, T, string) | void | Asserts that the input value must be equal to a specified value | | ||
| | IsLessThan<T>(T, T, string) | void | Asserts that the input value must be less than a specified value | | ||
| | IsLessThanOrEqualTo<T>(T, T, string) | void | Asserts that the input value must be less than or equal to a specified value | | ||
| | IsInRange<T>(T, T, T, string) | void | Asserts that the input value must be in a given range | | ||
michael-hawker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| | IsInRangeFor<T>(int, T[], string) | void | Asserts that the input index is valid for a given array | | ||
| | IsNotEmpty<T>(T[], string) | void | Asserts that the input array must not be empty | | ||
| | HasSizeEqualTo<T>(T[], int, string) | void | Asserts that the input array must have a size of a specified value | | ||
| | IsCompleted(Task, string) | void | Asserts that the input task is in a completed state | | ||
| | IsNotCanceled(Task, string) | void | Asserts that the input task is not canceled | | ||
Sergio0694 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Sample Code | ||
|
|
||
| You can find more examples in our [unit tests](https://github.com/Microsoft/WindowsCommunityToolkit//blob/master/UnitTests/Diagnostics/Test_Guard.cs) | ||
|
|
||
| ## Requirements | ||
|
|
||
| | Device family | Universal, 10.0.16299.0 or higher | | ||
michael-hawker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| | --- | --- | | ||
| | Namespace | Microsoft.Toolkit.Diagnostics | | ||
| | NuGet package | [Microsoft.Toolkit](https://www.nuget.org/packages/Microsoft.Toolkit/) | | ||
|
|
||
| ## API | ||
|
|
||
| * [Guard source code](https://github.com/Microsoft/WindowsCommunityToolkit//blob/master/Microsoft.Toolkit/Diagnostics) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.