Parameter Names

Mark
2 min readMar 27, 2021

Dear Microsoft, I’m sick of writing nameof(…) every second fucking parameter in validations. Can you fix this shit already?

So you’re all familiar with ArgumentException I. Should. Fucking. Hope. It’s what you throw (or the base class of what you throw) when some diversity hire passes an invalid value to your method. For example…

public class SomeClass
{
public void Foo(string bar)
{
if (bar is null)
throw new ArgumentNullException(nameof(bar));
if (bar == string.Empty)
throw new ArgumentException("Argument cannot be empty.", nameof(bar));
...
}
}

Because none of us like 80 % of our method bodies to be validation and the inbred retards at Microsoft never saw the value in Spec#, we end up only cluttering 40 % of the method bodies and write functions like…

public static class Assert
{
public void HasValue(string parameter, string parameterName)
{
if (parameter is null)
throw new ArgumentNullException(parameterName);
if (parameter == string.Empty)
throw new ArgumentException("Argument cannot be empty.", parameterName);
}
}
public class SomeClass
{
public void Foo(string bar)
{
Assert.HasValue(bar, nameof(bar));
...
}
}

But here-in lies my gripe: why do I need to tell HasValue parameterName=“bar” when the compiler is perfectly capable of working it out on its own? Obviously you guys employ some people who aren’t slow, because we have the ability to do this with the name of the calling method, file, and line…

public class Logger : ILogger
{
public void LogError(
string exceptionType,
[CallerMemberName] string memberName = null,
[CallerFilePath] string filePath = null,
[CallerLineNumber] int lineNumber = null)
{
Console.WriteLine($"Exception {exceptionType} was caught in {memberName} (File: {filePath}, Line: {lineNumber}).");
}
}
public class SomeService : ISomeService
{
private readonly ILogger logger;

...
public void DoStuff()
{
try
{
...
}
catch (Exception ex)
{
logger.LogError(typeof(ex));
...
}
}
}

… so why the fuck not let us do that with parameters (and for that matter, why did class name get skipped??)…

public static class Assert
{
public void HasValue(string parameter, [ParameterName(parameter)] string parameterName = null)
{
if (parameter is null)
throw new ArgumentNullException(parameterName);
if (parameter == string.Empty)
throw new ArgumentException("Argument cannot be empty.", parameterName);
}
}
public class SomeClass
{
public void Foo(string bar)
{
Assert.HasValue(bar);
...
}
}

While you’re at it, can you revisit Spec# now that you’ve killed Code Contracts? DBC is no less valid just because you keep bungling its implementation.

--

--