From 39c8c7defbf814ca3235b25fee71b281674947bf Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Sat, 1 Apr 2023 14:37:37 +0100 Subject: [PATCH] style: cluster throw helpers together to reduce branch repetition --- X10D/src/Collections/DictionaryExtensions.cs | 98 +++++++------------- X10D/src/Core/RandomExtensions.cs | 12 +-- X10D/src/IO/ListOfByteExtensions.cs | 6 +- X10D/src/Reflection/MemberInfoExtensions.cs | 18 ++-- X10D/src/Reflection/TypeExtensions.cs | 12 +-- X10D/src/Text/StringExtensions.cs | 18 ++-- 6 files changed, 55 insertions(+), 109 deletions(-) diff --git a/X10D/src/Collections/DictionaryExtensions.cs b/X10D/src/Collections/DictionaryExtensions.cs index f2594a0..bb13e05 100644 --- a/X10D/src/Collections/DictionaryExtensions.cs +++ b/X10D/src/Collections/DictionaryExtensions.cs @@ -1,4 +1,4 @@ -using System.Diagnostics.Contracts; +using System.Diagnostics.Contracts; using System.Runtime.InteropServices; using System.Web; @@ -37,15 +37,13 @@ public static class DictionaryExtensions { #if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(dictionary); + ArgumentNullException.ThrowIfNull(updateValueFactory); #else if (dictionary is null) { throw new ArgumentNullException(nameof(dictionary)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(updateValueFactory); -#else + if (updateValueFactory is null) { throw new ArgumentNullException(nameof(updateValueFactory)); @@ -108,15 +106,13 @@ public static class DictionaryExtensions { #if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(dictionary); + ArgumentNullException.ThrowIfNull(updateValueFactory); #else if (dictionary is null) { throw new ArgumentNullException(nameof(dictionary)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(updateValueFactory); -#else + if (updateValueFactory is null) { throw new ArgumentNullException(nameof(updateValueFactory)); @@ -167,23 +163,19 @@ public static class DictionaryExtensions { #if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(dictionary); + ArgumentNullException.ThrowIfNull(addValueFactory); + ArgumentNullException.ThrowIfNull(updateValueFactory); #else if (dictionary is null) { throw new ArgumentNullException(nameof(dictionary)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(addValueFactory); -#else + if (addValueFactory is null) { throw new ArgumentNullException(nameof(addValueFactory)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(updateValueFactory); -#else + if (updateValueFactory is null) { throw new ArgumentNullException(nameof(updateValueFactory)); @@ -250,23 +242,19 @@ public static class DictionaryExtensions { #if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(dictionary); + ArgumentNullException.ThrowIfNull(addValueFactory); + ArgumentNullException.ThrowIfNull(updateValueFactory); #else if (dictionary is null) { throw new ArgumentNullException(nameof(dictionary)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(addValueFactory); -#else + if (addValueFactory is null) { throw new ArgumentNullException(nameof(addValueFactory)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(updateValueFactory); -#else + if (updateValueFactory is null) { throw new ArgumentNullException(nameof(updateValueFactory)); @@ -325,23 +313,19 @@ public static class DictionaryExtensions { #if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(dictionary); + ArgumentNullException.ThrowIfNull(addValueFactory); + ArgumentNullException.ThrowIfNull(updateValueFactory); #else if (dictionary is null) { throw new ArgumentNullException(nameof(dictionary)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(addValueFactory); -#else + if (addValueFactory is null) { throw new ArgumentNullException(nameof(addValueFactory)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(updateValueFactory); -#else + if (updateValueFactory is null) { throw new ArgumentNullException(nameof(updateValueFactory)); @@ -414,23 +398,19 @@ public static class DictionaryExtensions { #if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(dictionary); + ArgumentNullException.ThrowIfNull(addValueFactory); + ArgumentNullException.ThrowIfNull(updateValueFactory); #else if (dictionary is null) { throw new ArgumentNullException(nameof(dictionary)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(addValueFactory); -#else + if (addValueFactory is null) { throw new ArgumentNullException(nameof(addValueFactory)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(updateValueFactory); -#else + if (updateValueFactory is null) { throw new ArgumentNullException(nameof(updateValueFactory)); @@ -514,15 +494,13 @@ public static class DictionaryExtensions { #if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(source); + ArgumentNullException.ThrowIfNull(selector); #else if (source is null) { throw new ArgumentNullException(nameof(source)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(selector); -#else + if (selector is null) { throw new ArgumentNullException(nameof(selector)); @@ -575,23 +553,19 @@ public static class DictionaryExtensions { #if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(source); + ArgumentNullException.ThrowIfNull(keySelector); + ArgumentNullException.ThrowIfNull(valueSelector); #else if (source is null) { throw new ArgumentNullException(nameof(source)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(keySelector); -#else + if (keySelector is null) { throw new ArgumentNullException(nameof(keySelector)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(valueSelector); -#else + if (valueSelector is null) { throw new ArgumentNullException(nameof(valueSelector)); @@ -669,15 +643,13 @@ public static class DictionaryExtensions { #if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(source); + ArgumentNullException.ThrowIfNull(selector); #else if (source is null) { throw new ArgumentNullException(nameof(source)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(selector); -#else + if (selector is null) { throw new ArgumentNullException(nameof(selector)); @@ -722,23 +694,19 @@ public static class DictionaryExtensions { #if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(source); + ArgumentNullException.ThrowIfNull(keySelector); + ArgumentNullException.ThrowIfNull(valueSelector); #else if (source is null) { throw new ArgumentNullException(nameof(source)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(keySelector); -#else + if (keySelector is null) { throw new ArgumentNullException(nameof(keySelector)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(valueSelector); -#else + if (valueSelector is null) { throw new ArgumentNullException(nameof(valueSelector)); diff --git a/X10D/src/Core/RandomExtensions.cs b/X10D/src/Core/RandomExtensions.cs index c4cc672..32acc3b 100644 --- a/X10D/src/Core/RandomExtensions.cs +++ b/X10D/src/Core/RandomExtensions.cs @@ -157,15 +157,13 @@ public static class RandomExtensions { #if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(random); + ArgumentNullException.ThrowIfNull(source); #else if (random is null) { throw new ArgumentNullException(nameof(random)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(source); -#else + if (source is null) { throw new ArgumentNullException(nameof(source)); @@ -534,15 +532,13 @@ public static class RandomExtensions { #if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(random); + ArgumentNullException.ThrowIfNull(source); #else if (random is null) { throw new ArgumentNullException(nameof(random)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(source); -#else + if (source is null) { throw new ArgumentNullException(nameof(source)); diff --git a/X10D/src/IO/ListOfByteExtensions.cs b/X10D/src/IO/ListOfByteExtensions.cs index d48e8e5..3289fef 100644 --- a/X10D/src/IO/ListOfByteExtensions.cs +++ b/X10D/src/IO/ListOfByteExtensions.cs @@ -210,15 +210,13 @@ public static class ListOfByteExtensions { #if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(source); + ArgumentNullException.ThrowIfNull(encoding); #else if (source is null) { throw new ArgumentNullException(nameof(source)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(encoding); -#else + if (encoding is null) { throw new ArgumentNullException(nameof(encoding)); diff --git a/X10D/src/Reflection/MemberInfoExtensions.cs b/X10D/src/Reflection/MemberInfoExtensions.cs index b54f9b4..24039e0 100644 --- a/X10D/src/Reflection/MemberInfoExtensions.cs +++ b/X10D/src/Reflection/MemberInfoExtensions.cs @@ -60,15 +60,13 @@ public static class MemberInfoExtensions { #if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(member); + ArgumentNullException.ThrowIfNull(attribute); #else if (member is null) { throw new ArgumentNullException(nameof(member)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(attribute); -#else + if (attribute is null) { throw new ArgumentNullException(nameof(attribute)); @@ -105,15 +103,13 @@ public static class MemberInfoExtensions { #if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(member); + ArgumentNullException.ThrowIfNull(selector); #else if (member is null) { throw new ArgumentNullException(nameof(member)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(selector); -#else + if (selector is null) { throw new ArgumentNullException(nameof(selector)); @@ -145,15 +141,13 @@ public static class MemberInfoExtensions { #if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(member); + ArgumentNullException.ThrowIfNull(selector); #else if (member is null) { throw new ArgumentNullException(nameof(member)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(selector); -#else + if (selector is null) { throw new ArgumentNullException(nameof(selector)); diff --git a/X10D/src/Reflection/TypeExtensions.cs b/X10D/src/Reflection/TypeExtensions.cs index 9d53c39..6f56d62 100644 --- a/X10D/src/Reflection/TypeExtensions.cs +++ b/X10D/src/Reflection/TypeExtensions.cs @@ -57,15 +57,13 @@ public static class TypeExtensions { #if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(value); + ArgumentNullException.ThrowIfNull(interfaceType); #else if (value is null) { throw new ArgumentNullException(nameof(value)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(interfaceType); -#else + if (interfaceType is null) { throw new ArgumentNullException(nameof(interfaceType)); @@ -144,15 +142,13 @@ public static class TypeExtensions { #if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(value); + ArgumentNullException.ThrowIfNull(type); #else if (value is null) { throw new ArgumentNullException(nameof(value)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(type); -#else + if (type is null) { throw new ArgumentNullException(nameof(type)); diff --git a/X10D/src/Text/StringExtensions.cs b/X10D/src/Text/StringExtensions.cs index 43aed4e..28d7693 100644 --- a/X10D/src/Text/StringExtensions.cs +++ b/X10D/src/Text/StringExtensions.cs @@ -136,23 +136,19 @@ public static class StringExtensions { #if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(value); + ArgumentNullException.ThrowIfNull(sourceEncoding); + ArgumentNullException.ThrowIfNull(destinationEncoding); #else if (value is null) { throw new ArgumentNullException(nameof(value)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(sourceEncoding); -#else + if (sourceEncoding is null) { throw new ArgumentNullException(nameof(sourceEncoding)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(destinationEncoding); -#else + if (destinationEncoding is null) { throw new ArgumentNullException(nameof(destinationEncoding)); @@ -478,15 +474,13 @@ public static class StringExtensions { #if NET6_0_OR_GREATER ArgumentNullException.ThrowIfNull(value); + ArgumentNullException.ThrowIfNull(encoding); #else if (value is null) { throw new ArgumentNullException(nameof(value)); } -#endif -#if NET6_0_OR_GREATER - ArgumentNullException.ThrowIfNull(encoding); -#else + if (encoding is null) { throw new ArgumentNullException(nameof(encoding));