Skip to content

Commit

Permalink
refactor: Create guard clauses (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrDave1999 authored Jul 6, 2024
1 parent 3e8a2b1 commit ac0cf7d
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 21 deletions.
43 changes: 43 additions & 0 deletions src/Common/ThrowHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
namespace YeSql.Net;

/// <summary>
/// Helper methods to efficiently throw exceptions.
/// </summary>
internal class ThrowHelper
{
/// <summary>
/// Throws an <see cref="ArgumentNullException"/> if <c>argument</c> is <c>null</c>.
/// </summary>
/// <param name="argument">
/// The reference type argument to validate as non-null.
/// </param>
/// <param name="paramName">
/// The name of the parameter with which argument corresponds.
/// </param>
/// <exception cref="ArgumentNullException"></exception>
public static void ThrowIfNull(object argument, string paramName)
{
if (argument is null)
throw new ArgumentNullException(paramName);
}

/// <summary>
/// Throws an exception if the <paramref name="argument"/> contains elements with a null value,
/// an empty string, or consists only of white-space characters.
/// </summary>
/// <param name="argument">
/// The collection argument to validate.
/// </param>
/// <param name="paramName">
/// The name of the parameter with which argument corresponds.
/// </param>
/// <exception cref="ArgumentException"></exception>
public static void ThrowIfContainsNullOrWhiteSpace(IEnumerable<string> argument, string paramName)
{
if (argument.ContainsNullOrWhiteSpace())
{
var message = string.Format(ExceptionMessages.CollectionHasNullValueOrOnlyWhitespace, paramName);
throw new ArgumentException(message);
}
}
}
16 changes: 4 additions & 12 deletions src/Loader/YeSqlLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,11 @@ public ISqlCollection LoadFromDefaultDirectory()
/// </exception>
public ISqlCollection LoadFromFiles(params string[] sqlFiles)
{
if (sqlFiles is null)
throw new ArgumentNullException(nameof(sqlFiles));

ThrowHelper.ThrowIfNull(sqlFiles, nameof(sqlFiles));
if (sqlFiles.IsEmpty())
return _parser.SqlStatements;

if (sqlFiles.ContainsNullOrWhiteSpace())
throw new ArgumentException(string.Format(ExceptionMessages.CollectionHasNullValueOrOnlyWhitespace, nameof(sqlFiles)));

ThrowHelper.ThrowIfContainsNullOrWhiteSpace(sqlFiles, nameof(sqlFiles));
foreach (var fileName in sqlFiles)
{
Result<SqlFile> result = LoadFromFile(fileName);
Expand Down Expand Up @@ -103,15 +99,11 @@ public ISqlCollection LoadFromFiles(params string[] sqlFiles)
/// </exception>
public ISqlCollection LoadFromDirectories(params string[] directories)
{
if (directories is null)
throw new ArgumentNullException(nameof(directories));

ThrowHelper.ThrowIfNull(directories, nameof(directories));
if (directories.IsEmpty())
return _parser.SqlStatements;

if(directories.ContainsNullOrWhiteSpace())
throw new ArgumentException(string.Format(ExceptionMessages.CollectionHasNullValueOrOnlyWhitespace, nameof(directories)));

ThrowHelper.ThrowIfContainsNullOrWhiteSpace(directories, nameof(directories));
foreach (var directory in directories)
{
Result<IEnumerable<SqlFile>> result = LoadFromDirectory(directory);
Expand Down
4 changes: 1 addition & 3 deletions src/Parser/YeSqlParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ public ISqlCollection ParseAndThrow(string source)
/// </exception>
public ISqlCollection Parse(string source, out YeSqlValidationResult validationResult)
{
if(source is null)
throw new ArgumentNullException(nameof(source));

ThrowHelper.ThrowIfNull(source, nameof(source));
validationResult = ValidationResult;
if(string.IsNullOrWhiteSpace(source))
{
Expand Down
8 changes: 2 additions & 6 deletions src/Reader/YeSqlDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ public string this[string tagName]
{
get
{
if(tagName is null)
throw new ArgumentNullException(nameof(tagName));

ThrowHelper.ThrowIfNull(tagName, nameof(tagName));
if(_sqlStatements.TryGetValue(tagName, out var sqlStatement))
return sqlStatement;

Expand All @@ -35,9 +33,7 @@ public string this[string tagName]
/// <inheritdoc />
public bool TryGetStatement(string tagName, out string sqlStatement)
{
if (tagName is null)
throw new ArgumentNullException(nameof(tagName));

ThrowHelper.ThrowIfNull(tagName, nameof (tagName));
return _sqlStatements.TryGetValue(tagName, out sqlStatement);
}

Expand Down

0 comments on commit ac0cf7d

Please sign in to comment.