Pattern: Missing use of nameof
Issue: -
nameof
improves code maintainability in cases where the parameter may be renamed in the future, but the string literal is mistakenly not renamed. By using nameof
, the symbol will be renamed when the parameter is renamed through a refactoring operation.
Example of incorrect code:
public Book(string title)
{
// Violates rule CA1507
Title = title ?? throw new ArgumentNullException("title", "All books must have a title.");
}
Example of correct code:
public Book(string title)
{
// Resolves rule CA1507 violation
Title = title ?? throw new ArgumentNullException(nameof(title), "All books must have a title.");
}