rspec/rules/S1862/vbnet/rule.adoc
2023-06-28 13:52:34 +02:00

63 lines
1.6 KiB
Plaintext

== Why is this an issue?
A chain of https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/if-then-else-statement[If/ElseIf] statements is evaluated from top to bottom. At most, only one branch will be executed: the first statement with a condition that evaluates to `True`.
Therefore, duplicating a condition leads to unreachable code inside the duplicated condition block. Usually, this is due to a copy/paste error.
The result of such duplication can lead to unreachable code or even to unexpected behavior.
== How to fix it
=== Code examples
==== Noncompliant code example
[source,vbnet,diff-id=1,diff-type=noncompliant]
----
If param = 1 Then
OpenWindow()
ElseIf param = 2 Then
CloseWindow()
ElseIf param = 1 Then ' Noncompliant: condition has already been checked
MoveWindowToTheBackground() ' unreachable code
End If
----
==== Compliant solution
[source,vbnet,diff-id=1,diff-type=compliant]
----
If param = 1 Then
OpenWindow()
ElseIf param = 2 Then
CloseWindow()
ElseIf param = 3 Then
MoveWindowToTheBackground()
End If
----
== Resources
=== Documentation
* https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/statements/if-then-else-statement[If...Then...Else Statement]
ifdef::env-github,rspecator-view,env-vscode[]
'''
== Implementation Specification
(visible only on this page)
=== Message
This branch duplicates the one on line n.
include::../highlighting.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]
endif::env-github,rspecator-view,env-vscode[]