While it is acceptable for setters to throw exceptions, getters are simply expected to return a value. Throwing any exception is considered an antipattern and is not a recommended practice. It is recommended that you refactor your code accordingly.
NullReferenceException
CS-R1009Explicit trapping of NullReferenceException
is usually considered a bad practice. It usually means that your application has come across a null
reference in an unexpected manner which you're trying to suppress by explicitly trapping through a catch
block rather than finding the root cause. Since this was unexpected, it is probably not safe for your application to continue with the execution.
Most of the built-in types in the language have aliases defined for them. Some such examples are:
[Type] [Alias] 1. System.String [String] -> string 2. System.Boolean [Boolean] -> bool 3. System.Byte [Byte] -> byte 4. System.Int32 [Int32] -> int
and so on. Therefore, it is suggested that you use these aliases when and where possible.
==
operator to perform value equality checks CS-R1001Operators such as ==
and !=
are expected to perform reference comparisons rather than compare values. If you intend to compare two different objects, consider implementing a relevant method in your class or look into interfaces such as IComparable<T>
and IEquatable<T>
.
Pure
attribute should return a value CS-R1002A method with the Pure
attribute suggests that it does not have any side effects, i.e., does not mutate the object's state. Therefore, having such a method have a return type void
does not make any sense. Therefore, it is suggested that you refactor your code accordingly, i.e., drop the said attribute or modify your method's logic.
DoesNotReturn
attribute should not return a value CS-R1003A method with the DoesNotReturn
attribute suggests that the method does not return anything. Therefore, having return
statements inside such methods does not make any sense. Therefore, it is suggested that you refactor your code accordingly, i.e., drop the said attribute or modify your method's logic.
.TryParse
over .Parse
when converting types CS-R1004Methods such as int.Parse
and double.Parse
throw Exceptions such as ArgumentNullException
, ArgumentException
, FormatException
or OverflowException
depending on the input. Incorrectly handling these Exceptions can cause issues during the runtime. It is therefore suggested that you use the safer alternative .TryParse
.
void
CS-R1005An async
method with return type void
does not provide any reliable way to know if the intended task has been completed or not. It is a fire-and-forget method and provides no reliable way to handle any Exceptions should things go wrong. It is therefore suggested that your method have a return type Task
.
Task/Task<T>
should not return null
CS-R1006A non-async method with a return type of either Task
or Task<T>
should never return null
. Doing so may result in a NullReferenceException
at runtime. Therefore, it is suggested that you either modify your method's logic or use Task.FromResult<T>(null)
.
Guid.Empty
to create an empty GUID CS-R1007new SomeClass()
is the syntax to instantiate a class in C#. However, new Guid()
does not generate a new GUID
. It instead returns an empty GUID
. If you intend to use an empty GUID
, consider using Guid.Empty
as it is more straightforward to comprehend. If you wish to generate a new usable GUID
, consider using Guid.NewGuid()
.
The exception caught is generic and defeats the purpose of exception handling. Each type of exception provides an insight into what exactly went wrong and provides scenario-specific ways for graceful recovery. While it is easy to recover from some exceptions, a small subset of them make the recovery very difficult, usually because the conditions are not suitable for the program to continue executing. It is therefore suggested that you switch to a better approach of exception handling and recovery.
I
CS-R1010The general consensus is that the names of interfaces begin with I
. Doing so makes it clearer to the person reading the code that the type being dealt with is an interface thereby giving more context about the code-snippet.
ToString
method should never return null
CS-R1011The ToString
method should return a string that best represents your class/object. Returning null
from such a method does not make any sense and is therefore recommended that you refactor this method to return a more suitable and appropriate representation.
void
CS-R1013Accessor-like methods are methods that usually return something and begin with keywords such as get
. Having such methods have return type void
is an antipattern and can affect the code readability. It is therefore recommended that you refactor your code accordingly.
string.IsNullOrEmpty
or string.IsNullOrWhiteSpace
to check for empty strings CS-R1014Comparing a string against an empty string literal is valid and is the preferred way in languages such as Go and Python. In C# however, it is recommended that you use the convenience methods such as string.IsNullOrWhiteSpace
or string.IsNullOrEmpty
as they offer slightly better performance when compared to other traditional/naive implementations.
The exception thrown is generic and defeats the purpose of exception handling. Each type of exception provides insight into what exactly went wrong and provides scenario-specific ways to recover gracefully. While it is easy to recover from some exceptions, a small subset of them make the recovery very difficult, usually because the conditions are not suitable for the program to continue executing. Be more specific about the exception types you throw.
Obsolete
attribute allows you to mark certain entities as obsolete, thereby discouraging users from using them. However, to convey the full context, it is recommended that you specify the obsolete-message/description. Failing to do so produces a blank line/empty string during the build process and can cause confusion.
default
label without a comment is redundant CS-R1022The default
case in a switch
is executed when none of the provided cases match. An empty default
case without a user comment is redundant. Either add a user comment to indicate to the reader and the analyzer that all the possible cases have been covered, or, consider throwing a NotImplementedException
to indicate that some cases are yet to be handled.
public
class per namespace CS-R1035Having more than 1 public class per namespace increases the complexity and affects the overall code readability and navigation. It is therefore recommended that you have a single public
class per namespace
.
var
is redundant when combined with out
and a discard pattern CS-R1067The out
keyword is used to indicate that the parameter is initialized by the method to which it is being passed. But, when a discard pattern (_
) is used with out
, it indicates that the variable is unused and is only a placeholder.
Therefore, in such cases, the keyword var
becomes redundant and can be dropped.