Skip to content

Commit b069cb8

Browse files
committed
refactor null checks using ArgumentException and ArgumentNullException
Simplify null and whitespace checks by using ArgumentException.ThrowIfNullOrWhiteSpace and ArgumentNullException.ThrowIfNull. This reduces repetitive code and enhances readability across various methods.
1 parent eaf5d75 commit b069cb8

File tree

9 files changed

+28
-50
lines changed

9 files changed

+28
-50
lines changed

src/GitVersion.Core.Tests/Helpers/TestConsole.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using GitVersion.Extensions;
12
using GitVersion.Helpers;
23
using GitVersion.Logging;
34

@@ -12,7 +13,7 @@ public class TestConsole(params string[] responses) : IConsole
1213

1314
public void WriteLine() => this.log.Info(PathHelper.NewLine);
1415

15-
public void Write(string? msg) => this.log.Info(msg ?? throw new ArgumentNullException(nameof(msg)));
16+
public void Write(string? msg) => this.log.Info(msg.NotNull());
1617

1718
public string ReadLine() => this.responses.Dequeue();
1819

src/GitVersion.Core/Core/FileSystem.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,14 @@ public void WriteAllText(string? file, string fileContents)
2424

2525
public void WriteAllText(string? file, string fileContents, Encoding encoding)
2626
{
27-
if (string.IsNullOrEmpty(file))
28-
throw new ArgumentNullException(nameof(file));
27+
ArgumentException.ThrowIfNullOrWhiteSpace(file);
2928

3029
File.WriteAllText(file, fileContents, encoding);
3130
}
3231

3332
public IEnumerable<string> DirectoryEnumerateFiles(string? directory, string searchPattern, SearchOption searchOption)
3433
{
35-
if (string.IsNullOrEmpty(directory))
36-
throw new ArgumentNullException(nameof(directory));
34+
ArgumentException.ThrowIfNullOrWhiteSpace(directory);
3735

3836
return Directory.EnumerateFiles(directory, searchPattern, searchOption);
3937
}

src/GitVersion.Core/Extensions/EnumerableExtensions.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ public static class EnumerableExtensions
44
{
55
public static T? OnlyOrDefault<T>(this IEnumerable<T> source)
66
{
7-
switch (source)
7+
ArgumentNullException.ThrowIfNull(source);
8+
9+
if (source is IList<T> { Count: 1 } list)
810
{
9-
case null:
10-
throw new ArgumentNullException(nameof(source));
11-
case IList<T> { Count: 1 } list:
12-
return list[0];
11+
return list[0];
1312
}
1413

1514
using var e = source.GetEnumerator();
@@ -21,10 +20,7 @@ public static class EnumerableExtensions
2120

2221
public static T SingleOfType<T>(this IEnumerable source)
2322
{
24-
if (source == null)
25-
{
26-
throw new ArgumentNullException(nameof(source));
27-
}
23+
ArgumentNullException.ThrowIfNull(source);
2824

2925
return source.OfType<T>().Single();
3026
}

src/GitVersion.Core/Helpers/PathHelper.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ public static string GetFullPath(string? path)
3535

3636
public static string Combine(string? path1, string? path2)
3737
{
38-
if (path1 == null || path2 == null)
39-
throw new ArgumentNullException((path1 == null) ? nameof(path1) : nameof(path2));
38+
ArgumentException.ThrowIfNullOrWhiteSpace(path1);
39+
ArgumentException.ThrowIfNullOrWhiteSpace(path2);
4040

4141
return Path.Combine(path1, path2);
4242
}
@@ -50,16 +50,19 @@ public static string Combine(string? path1)
5050

5151
public static string Combine(string? path1, string? path2, string? path3)
5252
{
53-
if (path1 == null || path2 == null || path3 == null)
54-
throw new ArgumentNullException((path1 == null) ? nameof(path1) : (path2 == null) ? nameof(path2) : nameof(path3));
53+
ArgumentException.ThrowIfNullOrWhiteSpace(path1);
54+
ArgumentException.ThrowIfNullOrWhiteSpace(path2);
55+
ArgumentException.ThrowIfNullOrWhiteSpace(path3);
5556

5657
return Path.Combine(path1, path2, path3);
5758
}
5859

5960
public static string Combine(string? path1, string? path2, string? path3, string? path4)
6061
{
61-
if (path1 == null || path2 == null || path3 == null || path4 == null)
62-
throw new ArgumentNullException((path1 == null) ? nameof(path1) : (path2 == null) ? nameof(path2) : (path3 == null) ? nameof(path3) : nameof(path4));
62+
ArgumentException.ThrowIfNullOrWhiteSpace(path1);
63+
ArgumentException.ThrowIfNullOrWhiteSpace(path2);
64+
ArgumentException.ThrowIfNullOrWhiteSpace(path3);
65+
ArgumentException.ThrowIfNullOrWhiteSpace(path4);
6366

6467
return Path.Combine(path1, path2, path3, path4);
6568
}

src/GitVersion.Core/Helpers/StringFormatWith.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,8 @@ internal static class StringFormatWithExtension
3030
/// </example>
3131
public static string FormatWith<T>(this string template, T? source, IEnvironment environment)
3232
{
33-
if (template is null)
34-
{
35-
throw new ArgumentNullException(nameof(template));
36-
}
37-
38-
if (source is null)
39-
{
40-
throw new ArgumentNullException(nameof(source));
41-
}
33+
ArgumentNullException.ThrowIfNull(template);
34+
ArgumentNullException.ThrowIfNull(source);
4235

4336
foreach (Match match in RegexPatterns.Common.ExpandTokensRegex.Matches(template).Cast<Match>())
4437
{

src/GitVersion.Core/Logging/LogExtensions.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,7 @@ private static void Write(this ILog log, LogLevel level, LogAction? logAction)
9696

9797
private static IDisposable WithVerbosity(this ILog log, Verbosity verbosity)
9898
{
99-
if (log == null)
100-
{
101-
throw new ArgumentNullException(nameof(log));
102-
}
99+
ArgumentNullException.ThrowIfNull(log);
103100
var lastVerbosity = log.Verbosity;
104101
log.Verbosity = verbosity;
105102
return Disposable.Create(() => log.Verbosity = lastVerbosity);

src/GitVersion.Output/TemplateManager.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ internal class TemplateManager(TemplateType templateType)
1515

1616
public string? GetTemplateFor(string fileExtension)
1717
{
18-
if (fileExtension == null)
19-
{
20-
throw new ArgumentNullException(nameof(fileExtension));
21-
}
18+
ArgumentNullException.ThrowIfNull(fileExtension);
2219

2320
string? result = null;
2421

@@ -32,10 +29,7 @@ internal class TemplateManager(TemplateType templateType)
3229

3330
public string? GetAddFormatFor(string fileExtension)
3431
{
35-
if (fileExtension == null)
36-
{
37-
throw new ArgumentNullException(nameof(fileExtension));
38-
}
32+
ArgumentNullException.ThrowIfNull(fileExtension);
3933

4034
string? result = null;
4135

@@ -49,10 +43,7 @@ internal class TemplateManager(TemplateType templateType)
4943

5044
public bool IsSupported(string fileExtension)
5145
{
52-
if (fileExtension == null)
53-
{
54-
throw new ArgumentNullException(nameof(fileExtension));
55-
}
46+
ArgumentNullException.ThrowIfNull(fileExtension);
5647

5748
return this.templates.ContainsKey(fileExtension);
5849
}

src/GitVersion.Testing/Fixtures/RepositoryFixtureBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using GitVersion.Helpers;
22
using GitVersion.Testing.Internal;
33
using LibGit2Sharp;
4+
using Shouldly;
45

56
namespace GitVersion.Testing;
67

@@ -17,7 +18,7 @@ protected RepositoryFixtureBase(Func<string, Repository> repositoryBuilder)
1718
protected RepositoryFixtureBase(Repository repository)
1819
{
1920
SequenceDiagram = new();
20-
Repository = repository ?? throw new ArgumentNullException(nameof(repository));
21+
Repository = repository.ShouldNotBeNull();
2122
Repository.Config.Set("user.name", "Test");
2223
Repository.Config.Set("user.email", "[email protected]");
2324
}

src/GitVersion.Testing/Helpers/ProcessHelper.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,8 @@ public static class ProcessHelper
7676
// http://csharptest.net/532/using-processstart-to-capture-console-output/
7777
public static int Run(Action<string> output, Action<string> errorOutput, TextReader? input, string exe, string args, string workingDirectory, params KeyValuePair<string, string?>[] environmentalVariables)
7878
{
79-
if (string.IsNullOrEmpty(exe))
80-
throw new ArgumentNullException(nameof(exe));
81-
if (output == null)
82-
throw new ArgumentNullException(nameof(output));
79+
ArgumentException.ThrowIfNullOrWhiteSpace(exe);
80+
ArgumentNullException.ThrowIfNull(output);
8381

8482
var psi = new ProcessStartInfo
8583
{

0 commit comments

Comments
 (0)