Skip to content

Commit 79bd020

Browse files
sungam3rzidad
authored andcommitted
refactor preconditions:
- eliminate string.Format() allocations - fix xml comments - add generic constraints - add nameof
1 parent 0d2fd2e commit 79bd020

File tree

1 file changed

+42
-31
lines changed

1 file changed

+42
-31
lines changed

Source/EasyNetQ/Preconditions.cs

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Reflection;
54

65
namespace EasyNetQ
76
{
@@ -26,9 +25,14 @@ internal static class Preconditions
2625
/// <exception cref="ArgumentException">
2726
/// Thrown if <paramref name="name"/> is blank.
2827
/// </exception>
29-
public static void CheckNotNull<T>(T value, string name)
28+
public static void CheckNotNull<T>(T value, string name) where T : class
3029
{
31-
CheckNotNull(value, name, string.Format("{0} must not be null", name));
30+
if (value == null)
31+
{
32+
CheckNotBlank(name, nameof(name), "name must not be blank");
33+
34+
throw new ArgumentNullException(name, string.Format("{0} must not be null", name));
35+
}
3236
}
3337

3438
/// <summary>
@@ -52,12 +56,12 @@ public static void CheckNotNull<T>(T value, string name)
5256
/// Thrown if <paramref name="name"/> or <paramref name="message"/> are
5357
/// blank.
5458
/// </exception>
55-
public static void CheckNotNull<T>(T value, string name, string message)
59+
public static void CheckNotNull<T>(T value, string name, string message) where T : class
5660
{
5761
if (value == null)
5862
{
59-
CheckNotBlank(name, "name", "name must not be blank");
60-
CheckNotBlank(message, "message", "message must not be blank");
63+
CheckNotBlank(name, nameof(name), "name must not be blank");
64+
CheckNotBlank(message, nameof(message), "message must not be blank");
6165

6266
throw new ArgumentNullException(name, message);
6367
}
@@ -85,12 +89,12 @@ public static void CheckNotBlank(string value, string name, string message)
8589
{
8690
if (string.IsNullOrWhiteSpace(name))
8791
{
88-
throw new ArgumentException("name must not be blank", "name");
92+
throw new ArgumentException("name must not be blank", nameof(name));
8993
}
9094

9195
if (string.IsNullOrWhiteSpace(message))
9296
{
93-
throw new ArgumentException("message must not be blank", "message");
97+
throw new ArgumentException("message must not be blank", nameof(message));
9498
}
9599

96100
if (string.IsNullOrWhiteSpace(value))
@@ -115,7 +119,15 @@ public static void CheckNotBlank(string value, string name, string message)
115119
/// </exception>
116120
public static void CheckNotBlank(string value, string name)
117121
{
118-
CheckNotBlank(value, name, string.Format("{0} must not be blank", name));
122+
if (string.IsNullOrWhiteSpace(name))
123+
{
124+
throw new ArgumentException("name must not be blank", nameof(name));
125+
}
126+
127+
if (string.IsNullOrWhiteSpace(value))
128+
{
129+
throw new ArgumentException(string.Format("{0} must not be blank", name), name);
130+
}
119131
}
120132

121133
/// <summary>
@@ -134,73 +146,72 @@ public static void CheckNotBlank(string value, string name)
134146
/// is empty, must not be blank.
135147
/// </param>
136148
/// <exception cref="ArgumentException">
137-
/// Thrown if <paramref name="collection"/> is empty, or if
138-
/// <paramref name="value"/> or <paramref name="name"/> are blank.
149+
/// Thrown if <paramref name="collection"/> is empty, or <c>null</c>.
139150
/// </exception>
140151
public static void CheckAny<T>(IEnumerable<T> collection, string name, string message)
141152
{
142153
if (collection == null || !collection.Any())
143154
{
144-
CheckNotBlank(name, "name", "name must not be blank");
145-
CheckNotBlank(message, "message", "message must not be blank");
155+
CheckNotBlank(name, nameof(name), "name must not be blank");
156+
CheckNotBlank(message, nameof(message), "message must not be blank");
146157

147158
throw new ArgumentException(message, name);
148159
}
149160
}
150161

151162
/// <summary>
152-
/// Ensures that <paramref name="value"/> is true.
163+
/// Ensures that <paramref name="value"/> is <c>true</c>.
153164
/// </summary>
154165
/// <param name="value">
155-
/// The value to check, must be true.
166+
/// The value to check, must be <c>true</c>.
156167
/// </param>
157168
/// <param name="name">
158169
/// The name of the parameter the value is taken from, must not be
159170
/// blank.
160171
/// </param>
161172
/// <param name="message">
162-
/// The message to provide to the exception if <paramref name="collection"/>
163-
/// is false, must not be blank.
173+
/// The message to provide to the exception if <paramref name="value"/>
174+
/// is <c>false</c>, must not be blank.
164175
/// </param>
165176
/// <exception cref="ArgumentException">
166-
/// Thrown if <paramref name="value"/> is false, or if <paramref name="name"/>
177+
/// Thrown if <paramref name="value"/> is <c>false</c>, or if <paramref name="name"/>
167178
/// or <paramref name="message"/> are blank.
168179
/// </exception>
169180
public static void CheckTrue(bool value, string name, string message)
170181
{
171182
if (!value)
172183
{
173-
CheckNotBlank(name, "name", "name must not be blank");
174-
CheckNotBlank(message, "message", "message must not be blank");
184+
CheckNotBlank(name, nameof(name), "name must not be blank");
185+
CheckNotBlank(message, nameof(message), "message must not be blank");
175186

176187
throw new ArgumentException(message, name);
177188
}
178189
}
179190

180191
/// <summary>
181-
/// Ensures that <paramref name="value"/> is false.
192+
/// Ensures that <paramref name="value"/> is <c>false</c>.
182193
/// </summary>
183194
/// <param name="value">
184-
/// The value to check, must be false.
195+
/// The value to check, must be <c>false</c>.
185196
/// </param>
186197
/// <param name="name">
187198
/// The name of the parameter the value is taken from, must not be
188199
/// blank.
189200
/// </param>
190201
/// <param name="message">
191-
/// The message to provide to the exception if <paramref name="collection"/>
192-
/// is true, must not be blank.
202+
/// The message to provide to the exception if <paramref name="value"/>
203+
/// is <c>true</c>, must not be blank.
193204
/// </param>
194205
/// <exception cref="ArgumentException">
195-
/// Thrown if <paramref name="value"/> is true, or if <paramref name="name"/>
206+
/// Thrown if <paramref name="value"/> is <c>true</c>, or if <paramref name="name"/>
196207
/// or <paramref name="message"/> are blank.
197208
/// </exception>
198209
public static void CheckFalse(bool value, string name, string message)
199210
{
200211
if (value)
201212
{
202-
CheckNotBlank(name, "name", "name must not be blank");
203-
CheckNotBlank(message, "message", "message must not be blank");
213+
CheckNotBlank(name, nameof(name), "name must not be blank");
214+
CheckNotBlank(message, nameof(message), "message must not be blank");
204215

205216
throw new ArgumentException(message, name);
206217
}
@@ -220,8 +231,8 @@ public static void CheckTypeMatches(Type expectedType, object value, string name
220231
bool assignable = expectedType.IsAssignableFrom(value.GetType());
221232
if (!assignable)
222233
{
223-
CheckNotBlank(name, "name", "name must not be blank");
224-
CheckNotBlank(message, "message", "message must not be blank");
234+
CheckNotBlank(name, nameof(name), "name must not be blank");
235+
CheckNotBlank(message, nameof(message), "message must not be blank");
225236

226237
throw new ArgumentException(message, name);
227238
}
@@ -234,11 +245,11 @@ public static void CheckLess(TimeSpan value, TimeSpan maxValue, string name)
234245
throw new ArgumentOutOfRangeException(name, string.Format("Arguments {0} must be less than maxValue", name));
235246
}
236247

237-
public static void CheckNull<T>(T value, string name)
248+
public static void CheckNull<T>(T value, string name) where T : class
238249
{
239250
if (value == null)
240251
return;
241252
throw new ArgumentException(string.Format("{0} must not be null", name), name);
242253
}
243254
}
244-
}
255+
}

0 commit comments

Comments
 (0)