rspec/rules/S1199/csharp/rule.adoc
Angelo Buono c24d13b88a
Modify S1199: Migrate to LayC - Nested code blocks should not be used (#3233)
Co-authored-by: Marco Borgeaud <89914223+marco-antognini-sonarsource@users.noreply.github.com>
2023-10-11 06:59:57 +00:00

69 lines
1.2 KiB
Plaintext

== Why is this an issue?
include::../description.adoc[]
=== Exceptions
The usage of a code block after a `case` is allowed.
== How to fix it
The nested code blocks should be extracted into separate methods.
=== Code examples
==== Noncompliant code example
[source,csharp,diff-id=1,diff-type=noncompliant]
----
public void Evaluate()
{
/* ... */
{ // Noncompliant - nested code block '{' ... '}'
int a = stack.pop();
int b = stack.pop();
int result = a + b;
stack.push(result);
}
/* ... */
}
----
==== Compliant solution
[source,csharp,diff-id=1,diff-type=compliant]
----
public void Evaluate()
{
/* ... */
StackAdd();
/* ... */
}
private void StackAdd()
{
int a = stack.pop();
int b = stack.pop();
int result = a + b;
stack.push(result);
}
----
== Resources
=== Documentation
* Wikipedia - https://en.wikipedia.org/wiki/Single-responsibility_principle[Single Responsibility Principle]
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::../message.adoc[]
'''
== Comments And Links
(visible only on this page)
include::../comments-and-links.adoc[]