C#

C#

Made by DeepSource
Do not prefix enum members with enum's name CS-R1132
Anti-pattern
Major

Enum values are always accessed using the enum's name. Therefore, it does not make any sense to prefix the enum's values with the enum's name. Doing so is repetitive and adds noise to the code.

Empty record achieves nothing and is likely a bad design pattern CS-R1134
Anti-pattern
Critical

Records are majorly used in serialization and deserialization. Either they take in some parameters or define members that participate in this process. However, a record that neither declares any parameters nor any members achieves nothing and is likely a bad design pattern. It is recommended that you rethink if a record is really the right structure for your use case.

Fields initialized only in constructors can be made readonly CS-R1137
Anti-pattern
Major

The readonly modifier can be applied to fields that are not initialized anywhere in a class and if initialized, it is either initialized inline or in the constructor. This modifier prevents you from rewriting/overwriting its values and may even allow the runtime to perform additional optimizations. Consider using this modifier when and where possible.

Using the logical not operator to invert binary expressions can affect readability CS-R1108
Anti-pattern
Major

Using the logical not operator ! to invert the result of a binary expression's result can affect readability as it requires that the reader first comprehend the binary expression and then mentally invert the result. This can interrupt the natural flow of reading the code, thereby affecting readability. It is recommended that you refactor this expression.

ICloneable does not define a spec for Clone() and hence should not be implemented CS-R1104
Anti-pattern
Critical

ICloneable allows you to define methods that help in cloning the instances of your class. However, the specification does not define whether this clone operation is a shallow clone or a deep clone. If it is a deep clone, it may end up recursively referencing other objects in the object graph. Moreover, if a class implements ICloneable, there may be a need for all its subtypes to implement it too. It is therefore recommended that you define your own method that aids in cloning.

Implementing IComparable<T> may be particularly useful CS-R1106
Anti-pattern
Major

The specified class has a method whose signature resembles IComparable<T>::CompareTo(T? other) but does not implement IComparable<T>. If your method indeed performs a comparison between 2 objects of the same type, it may be particularly useful to implement the IComparable<T> interface which is defined exactly for purposes like these.

Missing implementation of System.Exception CS-R1109
Anti-pattern
Major

Inheriting System.Exception class allows you to define your own custom exceptions. This is particularly useful for scenarios where you believe that the exceptions supplied in the standard library are not suitable for your use cases. The norm is that such user-defined exception's name end with the word Exception such as EmployeeListNotFoundException. However, in this case, such a class was found to not inherit System.Exception. Either consider inheriting it, or, renaming your class to avoid confusion.

Drop record's body if it does not define any members CS-R1130
Anti-pattern
Major
Autofix

Record is an entity that is primarily (but not limited to) used for supporting immutable data models and is commonly used in serialization and deserialization. Consider dropping the record's body if it takes in one or more parameters and does not define any members.

Avoid multiple assignments in a single expression CS-R1143
Anti-pattern
Major

While assignments are simple to read and comprehend, it is easy to negatively affect the readability by adding more assignments to the same expression. While it may save you a few lines of code, it may introduce bugs into your application and additionally cause confusion to the readers. Instead, consider breaking down the complex assignment and placing each assignment separately.

0 should be assigned to None in enum-s attributed with Flags attribute CS-R1144
Anti-pattern
Major

The Flags attribute specifies that the enumeration can be treated as a bit field; that is, a set of flags. It is idiomatic that 0 be assigned to a member named None in such cases. Consider renaming the concerned member appropriately.

Child and parent classes cannot share the same name CS-R1103
Anti-pattern
Major

Using the same name for both the child class and the parent class can cause confusion. It is recommended that the names be as unique as possible

Consider replacing an if statement with just the condition if all it does is return a bool value CS-R1126
Anti-pattern
Major
Autofix

If all your ifstatement does is return a bool value in both the then and else blocks, consider replacing the entire if statement with just the condition.

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.

Numeric literal has incorrect grouping CS-R1131
Anti-pattern
Minor

Large numeric literals are separated using the underscore character to make it easier to read. In such cases, the numeric literal is grouped in a group of size of 3. However, in this case, the literal is unevenly grouped. This issue is raised whenever the grouping is uneven or the individual group size is not 3.

Deconstruct tuple expression to avoid repetitiveness CS-R1133
Anti-pattern
Minor

C# allows you to declare tuples using parenthesis. However, if you wish to declare multiple variables, consider deconstruction/unpacking the variables by using a single var keyword. In short, you can deconstruct a tuple that's been declared as (var x, var y, var z) as var (x, y, z).

Function with cyclomatic complexity higher than threshold CS-R1140
Anti-pattern
Minor

A function with high cyclomatic complexity can be hard to understand and maintain. Cyclomatic complexity is a software metric that measures the number of independent paths through a function. A higher cyclomatic complexity indicates that the function has more decision points and is more complex.

CancellationToken parameter should always be placed last in the parameter list CS-R1141
Anti-pattern
Minor

Asynchronous operations or long-running tasks that are cancelable can take in a CancellationToken which helps in terminating these operations. It is common for asynchronous operations to pass around this token as they invoke other such operations, thereby aiding in terminating the call chain. Microsoft guidelines state that this CancellationToken be always placed at last in the parameter list unless there are optional parameters or parameters with ref or out modifiers. While the token itself is not usually relevant to the core functionality of a majority of these methods, it is considered a good API design practice to have such parameters be the last parameter in the list.

Consider using the equality operators when evaluating bool? CS-R1118
Anti-pattern
Major
Autofix

While the null coalescing operator may come in handy when evaluating the bool? types, it is recommended that you stick to the traditional equality operators and explicit boolean values — an approach that is more readable and easier to comprehend.

Consider using not null instead of an empty recursive pattern when checking for nullness CS-R1120
Anti-pattern
Major
Autofix

One way to check for nullness using pattern matching is to use the empty recursive pattern syntax {}. However, consider using not null instead as it is more readable and easier to comprehend as it accurately conveys what it achieves.

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.