Skip to content

Commit ac0cf7d

Browse files
authored
refactor: Create guard clauses (#117)
1 parent 3e8a2b1 commit ac0cf7d

File tree

4 files changed

+50
-21
lines changed

4 files changed

+50
-21
lines changed

src/Common/ThrowHelper.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
namespace YeSql.Net;
2+
3+
/// <summary>
4+
/// Helper methods to efficiently throw exceptions.
5+
/// </summary>
6+
internal class ThrowHelper
7+
{
8+
/// <summary>
9+
/// Throws an <see cref="ArgumentNullException"/> if <c>argument</c> is <c>null</c>.
10+
/// </summary>
11+
/// <param name="argument">
12+
/// The reference type argument to validate as non-null.
13+
/// </param>
14+
/// <param name="paramName">
15+
/// The name of the parameter with which argument corresponds.
16+
/// </param>
17+
/// <exception cref="ArgumentNullException"></exception>
18+
public static void ThrowIfNull(object argument, string paramName)
19+
{
20+
if (argument is null)
21+
throw new ArgumentNullException(paramName);
22+
}
23+
24+
/// <summary>
25+
/// Throws an exception if the <paramref name="argument"/> contains elements with a null value,
26+
/// an empty string, or consists only of white-space characters.
27+
/// </summary>
28+
/// <param name="argument">
29+
/// The collection argument to validate.
30+
/// </param>
31+
/// <param name="paramName">
32+
/// The name of the parameter with which argument corresponds.
33+
/// </param>
34+
/// <exception cref="ArgumentException"></exception>
35+
public static void ThrowIfContainsNullOrWhiteSpace(IEnumerable<string> argument, string paramName)
36+
{
37+
if (argument.ContainsNullOrWhiteSpace())
38+
{
39+
var message = string.Format(ExceptionMessages.CollectionHasNullValueOrOnlyWhitespace, paramName);
40+
throw new ArgumentException(message);
41+
}
42+
}
43+
}

src/Loader/YeSqlLoader.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,11 @@ public ISqlCollection LoadFromDefaultDirectory()
6060
/// </exception>
6161
public ISqlCollection LoadFromFiles(params string[] sqlFiles)
6262
{
63-
if (sqlFiles is null)
64-
throw new ArgumentNullException(nameof(sqlFiles));
65-
63+
ThrowHelper.ThrowIfNull(sqlFiles, nameof(sqlFiles));
6664
if (sqlFiles.IsEmpty())
6765
return _parser.SqlStatements;
68-
69-
if (sqlFiles.ContainsNullOrWhiteSpace())
70-
throw new ArgumentException(string.Format(ExceptionMessages.CollectionHasNullValueOrOnlyWhitespace, nameof(sqlFiles)));
7166

67+
ThrowHelper.ThrowIfContainsNullOrWhiteSpace(sqlFiles, nameof(sqlFiles));
7268
foreach (var fileName in sqlFiles)
7369
{
7470
Result<SqlFile> result = LoadFromFile(fileName);
@@ -103,15 +99,11 @@ public ISqlCollection LoadFromFiles(params string[] sqlFiles)
10399
/// </exception>
104100
public ISqlCollection LoadFromDirectories(params string[] directories)
105101
{
106-
if (directories is null)
107-
throw new ArgumentNullException(nameof(directories));
108-
102+
ThrowHelper.ThrowIfNull(directories, nameof(directories));
109103
if (directories.IsEmpty())
110104
return _parser.SqlStatements;
111105

112-
if(directories.ContainsNullOrWhiteSpace())
113-
throw new ArgumentException(string.Format(ExceptionMessages.CollectionHasNullValueOrOnlyWhitespace, nameof(directories)));
114-
106+
ThrowHelper.ThrowIfContainsNullOrWhiteSpace(directories, nameof(directories));
115107
foreach (var directory in directories)
116108
{
117109
Result<IEnumerable<SqlFile>> result = LoadFromDirectory(directory);

src/Parser/YeSqlParser.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ public ISqlCollection ParseAndThrow(string source)
8585
/// </exception>
8686
public ISqlCollection Parse(string source, out YeSqlValidationResult validationResult)
8787
{
88-
if(source is null)
89-
throw new ArgumentNullException(nameof(source));
90-
88+
ThrowHelper.ThrowIfNull(source, nameof(source));
9189
validationResult = ValidationResult;
9290
if(string.IsNullOrWhiteSpace(source))
9391
{

src/Reader/YeSqlDictionary.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,7 @@ public string this[string tagName]
1818
{
1919
get
2020
{
21-
if(tagName is null)
22-
throw new ArgumentNullException(nameof(tagName));
23-
21+
ThrowHelper.ThrowIfNull(tagName, nameof(tagName));
2422
if(_sqlStatements.TryGetValue(tagName, out var sqlStatement))
2523
return sqlStatement;
2624

@@ -35,9 +33,7 @@ public string this[string tagName]
3533
/// <inheritdoc />
3634
public bool TryGetStatement(string tagName, out string sqlStatement)
3735
{
38-
if (tagName is null)
39-
throw new ArgumentNullException(nameof(tagName));
40-
36+
ThrowHelper.ThrowIfNull(tagName, nameof (tagName));
4137
return _sqlStatements.TryGetValue(tagName, out sqlStatement);
4238
}
4339

0 commit comments

Comments
 (0)