== Why is this an issue?

include::../description.adoc[]

However, nested code blocks are acceptable when they encapsulate all statements within a `switch` (a `case xxx:` or a `default:`)
to prevent variable declarations from interfering with other `cases`.

== How to fix it

The nested code blocks should be extracted into separate methods.

=== Code examples

==== Noncompliant code example

[source,cpp,diff-id=1,diff-type=noncompliant]
----
void f(Cache &c, int data) {
  int value;
  { // Noncompliant
    std::scoped_lock l(c.getMutex());
    if (c.hasKey(data)) {
      value = c.get(data);
    } else {
      value = compute(data);
      c.set(data, value);
    }
  } // Releases the mutex

  switch(value) {
    case 1: 
    { // Noncompliant, some statements are outside of the block
       int result = compute(value);
       save(result);
    }
    log();
    break;
    case 2:
    // ...
  }
}
----

==== Compliant solution

[source,cpp,diff-id=1,diff-type=compliant]
----
int getValue(Cache &c, int data) {
  std::scoped_lock l(c.getMutex());
  if (c.hasKey(data)) {
    return c.get(data);
  } else {
    value = compute(data);
    c.set(data, value);
    return value;
  }
}

void f(Cache &c, int data) {
  int value = getValue(c, data);

  switch(value) {
    case 1: 
    { // Compliant, limits the scope of "result"
       int result = compute(value);
       save(result);
       log();
    }
    break;
    case 2:
    // ...
  }
}
----

== 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[]

endif::env-github,rspecator-view[]
