Method convergeColumnField
has a Cognitive Complexity of 102 (exceeds 50 allowed). Consider refactoring. Open
private void convergeColumnField(@Nonnull Message.Builder target,
@Nonnull Struct source,
@Nonnull ModelMetadata.FieldPointer fieldPointer) {
// resolve any generic column options and spanner extension options
var columnOpts = columnOpts(fieldPointer);
- Read upRead up
- Create a ticketCreate a ticket
Cognitive Complexity
Cognitive Complexity is a measure of how difficult a unit of code is to intuitively understand. Unlike Cyclomatic Complexity, which determines how difficult your code will be to test, Cognitive Complexity tells you how difficult your code will be to read and comprehend.
A method's cognitive complexity is based on a few simple rules:
- Code is not considered more complex when it uses shorthand that the language provides for collapsing multiple statements into one
- Code is considered more complex for each "break in the linear flow of the code"
- Code is considered more complex when "flow breaking structures are nested"
Further reading
Method convergeColumnField
has 291 lines of code (exceeds 200 allowed). Consider refactoring. Open
private void convergeColumnField(@Nonnull Message.Builder target,
@Nonnull Struct source,
@Nonnull ModelMetadata.FieldPointer fieldPointer) {
// resolve any generic column options and spanner extension options
var columnOpts = columnOpts(fieldPointer);
- Create a ticketCreate a ticket
Refactor this method to reduce its Cognitive Complexity from 136 to the 15 allowed. Open
private void convergeColumnField(@Nonnull Message.Builder target,
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Cognitive Complexity is a measure of how hard the control flow of a method is to understand. Methods with high Cognitive Complexity will be difficult to maintain.
See
End this switch case with an unconditional break, return or throw statement. Open
case TIMESTAMP:
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
When the execution is not explicitly terminated at the end of a switch case, it continues to execute the statements of the following case. While this is sometimes intentional, it often is a mistake which leads to unexpected behavior.
Noncompliant Code Example
switch (myVariable) { case 1: foo(); break; case 2: // Both 'doSomething()' and 'doSomethingElse()' will be executed. Is it on purpose ? doSomething(); default: doSomethingElse(); break; }
Compliant Solution
switch (myVariable) { case 1: foo(); break; case 2: doSomething(); break; default: doSomethingElse(); break; }
Exceptions
This rule is relaxed in the following cases:
switch (myVariable) { case 0: // Empty case used to specify the same behavior for a group of cases. case 1: doSomething(); break; case 2: // Use of return statement return; case 3: // Use of throw statement throw new IllegalStateException(); case 4: // Use of continue statement continue; default: // For the last case, use of break statement is optional doSomethingElse(); }
See
- MITRE, CWE-484 - Omitted Break Statement in Switch
- CERT, MSC17-C. - Finish every set of statements associated with a case label with a break statement
- CERT, MSC52-J. - Finish every set of statements associated with a case label with a break statement
Add a default case to this switch. Open
switch (innerType) {
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
The requirement for a final default
clause is defensive programming. The clause should either take appropriate action, or contain a
suitable comment as to why no action is taken.
Noncompliant Code Example
switch (param) { //missing default clause case 0: doSomething(); break; case 1: doSomethingElse(); break; } switch (param) { default: // default clause should be the last one error(); break; case 0: doSomething(); break; case 1: doSomethingElse(); break; }
Compliant Solution
switch (param) { case 0: doSomething(); break; case 1: doSomethingElse(); break; default: error(); break; }
Exceptions
If the switch
parameter is an Enum
and if all the constants of this enum are used in the case
statements,
then no default
clause is expected.
Example:
public enum Day { SUNDAY, MONDAY } ... switch(day) { case SUNDAY: doSomething(); break; case MONDAY: doSomethingElse(); break; }
See
- MITRE, CWE-478 - Missing Default Case in Switch Statement
- CERT, MSC01-C. - Strive for logical completeness
Fall through from previous branch of the switch statement. Open
case DATE:
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Checks for fall-through in switch
statements. Finds locations where a case
contains Java code but lacks a break
, return
,throw
or continue
statement.
The check honors special comments to suppress the warning.By default the texts"fallthru", "fall thru", "fall-thru","fallthrough", "fall through", "fall-through""fallsthrough", "falls through", "falls-through" (case sensitive).The comment containing these words must be all on one line,and must be on the last non-empty line before thecase
triggering the warning or onthe same line before the case
(ugly, but possible).
Note: The check assumes that there is no unreachablecode in the case
.
This documentation is written and maintained by the Checkstyle community and is covered under the same license as the Checkstyle project.