diff --git a/rules/S3257/comments-and-links.adoc b/rules/S3257/comments-and-links.adoc deleted file mode 100644 index 61f52518f3..0000000000 --- a/rules/S3257/comments-and-links.adoc +++ /dev/null @@ -1,18 +0,0 @@ -=== on 9 Jul 2015, 13:54:52 Ann Campbell wrote: -\[~tamas.vajk] please add a ``++Nullable++`` example to the code samples - -=== on 20 Jul 2015, 11:32:42 Tamas Vajk wrote: -\[~ann.campbell.2] Modified the samples a bit. - -=== on 20 Jul 2015, 14:15:56 Ann Campbell wrote: -thanks [~tamas.vajk] - -=== on 9 Dec 2015, 18:39:35 Ann Campbell wrote: -\[~tamas.vajk] because of the char limit in the R# mapping field, I truncated/expanded to map this rule to "RedundantExplicit.*" - - -Given the scope of this RSpec, I think it's an okay mapping, we just may come across other cases that we'll have to add in the future. But we would have done that anyway. - -=== on 10 Dec 2015, 09:02:12 Tamas Vajk wrote: -\[~ann.campbell.2] LGTM - diff --git a/rules/S3257/csharp/rule.adoc b/rules/S3257/csharp/rule.adoc index 45254d5a3a..40f0536f9c 100644 --- a/rules/S3257/csharp/rule.adoc +++ b/rules/S3257/csharp/rule.adoc @@ -1,9 +1,10 @@ == Why is this an issue? -Unnecessarily verbose declarations and initializations make it harder to read the code, and should be simplified. +In C#, the type of a variable can often be inferred by the compiler. The use of the [var keyword](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/implicitly-typed-local-variables) allows you to avoid repeating the type name in a variable declaration and object instantiation because the declared type can often be inferred by the compiler. +Additionally, initializations providing the default value can also be omitted, helping to make the code more concise and readable. -Specifically the following should be omitted when they can be inferred: +Unnecessarily verbose declarations and initializations should be simplified. Specifically, the following should be omitted when they can be inferred: * array element type * array size @@ -13,10 +14,14 @@ Specifically the following should be omitted when they can be inferred: * type of lambda expression parameters * parameter declarations of anonymous methods when the parameters are not used. +== How to fix it -=== Noncompliant code example +Remove any unneeded code. C# provides many features designed to help you write more concise code. -[source,csharp] +=== Code examples +==== Noncompliant code example + +[source,csharp,diff-id=1,diff-type=noncompliant] ---- var l = new List() {}; // Noncompliant, {} can be removed var o = new object() {}; // Noncompliant, {} can be removed @@ -25,7 +30,7 @@ var ints = new int[] {1, 2, 3}; // Noncompliant, int can be omitted ints = new int[3] {1, 2, 3}; // Noncompliant, the size specification can be removed int? i = new int?(5); // Noncompliant new int? could be omitted, it can be inferred from the declaration, and there's implicit conversion from T to T? -var j = new int?(5); +var j = new int?(5); Func f1 = (int i) => 1; //Noncompliant, can be simplified @@ -40,10 +45,9 @@ class Class } ---- +==== Compliant solution -=== Compliant solution - -[source,csharp] +[source,csharp,diff-id=1,diff-type=compliant] ---- var l = new List(); var o = new object(); @@ -67,21 +71,7 @@ class Class } ---- +== Resources +=== Documentation -ifdef::env-github,rspecator-view[] - -''' -== Implementation Specification -(visible only on this page) - -include::../message.adoc[] - -include::../highlighting.adoc[] - -''' -== Comments And Links -(visible only on this page) - -include::../comments-and-links.adoc[] - -endif::env-github,rspecator-view[] +* Microsoft Learn - https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/declarations[Declaration statements] \ No newline at end of file diff --git a/rules/S3257/highlighting.adoc b/rules/S3257/highlighting.adoc deleted file mode 100644 index e10847773b..0000000000 --- a/rules/S3257/highlighting.adoc +++ /dev/null @@ -1,4 +0,0 @@ -=== Highlighting - -The bit to be removed/replaced - diff --git a/rules/S3257/javascript/rule.adoc b/rules/S3257/javascript/rule.adoc index b7d34bd0d9..e6db255af7 100644 --- a/rules/S3257/javascript/rule.adoc +++ b/rules/S3257/javascript/rule.adoc @@ -1,10 +1,18 @@ == Why is this an issue? -Unnecessarily verbose declarations and initializations make it harder to read the code, and should be simplified. Specifically, primitive (``++number++``, ``++string++``, ``++boolean++`` and others) types should be omitted from variable and parameter declaration when they can be easily inferred from the initialized or defaulted value. +TypeScript supports type inference, a mechanism that automatically infers the type of a variable based on its initial value. This means that if you initialize a variable with a particular value, TypeScript will assume that this variable should always hold that type of value. -=== Noncompliant code example +Unnecessarily verbose declarations and initializations make it harder to read the code and should be simplified. Therefore, type annotations should be omitted from variable and parameter declarations when they can be easily inferred from the initialized or defaulted value. -[source,javascript] +== How to fix it + +Omit explicit type annotations in declarations whenever the type can be inferred from the context. + +=== Code examples + +==== Noncompliant code example + +[source,javascript,diff-id=1,diff-type=noncompliant] ---- const n: number = 1; // Noncompliant, "number" can be omitted @@ -15,9 +23,9 @@ class Bar { } ---- -=== Compliant solution +==== Compliant solution -[source,javascript] +[source,javascript,diff-id=1,diff-type=compliant] ---- const n = 1; @@ -28,20 +36,7 @@ class Bar { } ---- -ifdef::env-github,rspecator-view[] +== Resources +=== Documentation -''' -== Implementation Specification -(visible only on this page) - -include::../message.adoc[] - -include::../highlighting.adoc[] - -''' -== Comments And Links -(visible only on this page) - -include::../comments-and-links.adoc[] - -endif::env-github,rspecator-view[] +* TypeScript - https://www.typescriptlang.org/docs/handbook/type-inference.html[Type Inference] diff --git a/rules/S3257/message.adoc b/rules/S3257/message.adoc deleted file mode 100644 index 4b338614d5..0000000000 --- a/rules/S3257/message.adoc +++ /dev/null @@ -1,4 +0,0 @@ -=== Message - -Remove XXX; it is redundant. -