Returning null instead of an actual array, collection or map forces callers of the method to explicitly test for nullity, making them
more complex and less readable.
Moreover, in many cases, null is used as a synonym for empty.
public Result[] GetResults()
{
return null; // Noncompliant
}
public IEnumerable<Result> GetResults()
{
return null; // Noncompliant
}
public IEnumerable<Result> GetResults() => null; // Noncompliant
public IEnumerable<Result> Results
{
get
{
return null; // Noncompliant
}
}
public IEnumerable<Result> Results => null; // Noncompliant
public Result[] GetResults()
{
return new Result[0];
}
public IEnumerable<Result> GetResults()
{
return Enumerable.Empty<Result>();
}
public IEnumerable<Result> GetResults() => Enumerable.Empty<Result>();
public IEnumerable<Result> Results
{
get
{
return Enumerable.Empty<Result>();
}
}
public IEnumerable<Result> Results => Enumerable.Empty<Result>();
Although string is a collection, the rule won’t report on it.