C#

C#

Made by DeepSource
Getters must not throw exceptions CS-R1038
Anti-pattern
Major

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.

Explicit trap of NullReferenceException CS-R1009
Anti-pattern
Major

Explicit 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.

Use aliases when using built-in types CS-R1000
Anti-pattern
Minor

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.

Do not overload the == operator to perform value equality checks CS-R1001
Anti-pattern
Major

Operators 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>.

Methods with Pure attribute should return a value CS-R1002
Anti-pattern
Major

A 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.

Methods with DoesNotReturn attribute should not return a value CS-R1003
Anti-pattern
Major
Autofix

A 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.

Consider using .TryParse over .Parse when converting types CS-R1004
Anti-pattern
Major

Methods 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.

Async methods should not have a return type of void CS-R1005
Anti-pattern
Critical

An 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.

Non-Async methods with return type Task/Task<T> should not return null CS-R1006
Anti-pattern
Critical

A 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).

Use Guid.Empty to create an empty GUID CS-R1007
Anti-pattern
Critical
Autofix

new 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().

Exception caught is generic CS-R1008
Anti-pattern
Major

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.

Names of interfaces must begin with I CS-R1010
Anti-pattern
Minor

The 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-R1011
Anti-pattern
Critical

The 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.

Accessor like method has return type void CS-R1013
Anti-pattern
Major

Accessor-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.

Use string.IsNullOrEmpty or string.IsNullOrWhiteSpace to check for empty strings CS-R1014
Anti-pattern
Major
Autofix

Comparing 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.

Exception thrown is generic CS-R1015
Anti-pattern
Major

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 should specify the message CS-R1016
Anti-pattern
Major

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.

Empty default label without a comment is redundant CS-R1022
Anti-pattern
Major

The 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.

Consider having 1 public class per namespace CS-R1035
Anti-pattern
Major

Having 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-R1067
Anti-pattern
Major
Autofix

The 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.