File CharacterBuffer.java
has 1175 lines of code (exceeds 250 allowed). Consider refactoring. Open
package de.uniks.networkparser.buffer;
import java.util.List;
/*
* NetworkParser The MIT License Copyright (c) 2010-2016 Stefan Lindel
CharacterBuffer
has 70 methods (exceeds 20 allowed). Consider refactoring. Open
public class CharacterBuffer extends BufferedBuffer implements CharSequence, BaseItem {
/** The value is used for character storage. */
char[] buffer;
/**
Method replace
has a Cognitive Complexity of 43 (exceeds 5 allowed). Consider refactoring. Open
public boolean replace(String search, String replace) {
if (search == null || search.length() < 1) {
return false;
}
int deleted = 0;
- Read upRead up
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 equalsLevenshtein
has a Cognitive Complexity of 34 (exceeds 5 allowed). Consider refactoring. Open
public double equalsLevenshtein(CharacterBuffer t) {
if (t == null || t.length() < 1) {
return this.length();
}
if (this.length() < 1) {
- Read upRead up
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 withObjects
has a Cognitive Complexity of 30 (exceeds 5 allowed). Consider refactoring. Open
public CharacterBuffer withObjects(Object... items) {
if (items == null) {
return this;
}
if (this.buffer == null) {
- Read upRead up
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 replace
has a Cognitive Complexity of 24 (exceeds 5 allowed). Consider refactoring. Open
public boolean replace(int start, int end, String replace) {
int pos = 0;
if (replace == null || buffer == null || start > end || start > buffer.length) {
return false;
}
- Read upRead up
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 rtrim
has a Cognitive Complexity of 23 (exceeds 5 allowed). Consider refactoring. Open
public CharacterBuffer rtrim(char... items) {
validateValue();
if (items != null) {
int z;
while (length > 0) {
- Read upRead up
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 with
has a Cognitive Complexity of 23 (exceeds 5 allowed). Consider refactoring. Open
public CharacterBuffer with(CharSequence values, int start, int end) {
if (values == null || (buffer != null && start > buffer.length)) {
return this;
}
if (this.buffer == null) {
- Read upRead up
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 with
has a Cognitive Complexity of 22 (exceeds 5 allowed). Consider refactoring. Open
public CharacterBuffer with(CharSequence... items) {
if (items == null) {
return this;
}
if (this.buffer == null) {
- Read upRead up
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 replace
has 66 lines of code (exceeds 25 allowed). Consider refactoring. Open
public boolean replace(String search, String replace) {
if (search == null || search.length() < 1) {
return false;
}
int deleted = 0;
Method withStart
has a Cognitive Complexity of 19 (exceeds 5 allowed). Consider refactoring. Open
public CharacterBuffer withStart(CharSequence item, boolean newLine) {
if (item == null || start < 0) {
return this;
}
int len = item.length();
- Read upRead up
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 equalsLevenshtein
has 61 lines of code (exceeds 25 allowed). Consider refactoring. Open
public double equalsLevenshtein(CharacterBuffer t) {
if (t == null || t.length() < 1) {
return this.length();
}
if (this.length() < 1) {
Method replace
has 50 lines of code (exceeds 25 allowed). Consider refactoring. Open
public boolean replace(int start, int end, String replace) {
int pos = 0;
if (replace == null || buffer == null || start > end || start > buffer.length) {
return false;
}
Method equalsText
has a Cognitive Complexity of 15 (exceeds 5 allowed). Consider refactoring. Open
public boolean equalsText(char... other) {
if (other == null) {
return true;
}
if (start > length || start < 0 || buffer == null) {
- Read upRead up
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 withObjects
has 40 lines of code (exceeds 25 allowed). Consider refactoring. Open
public CharacterBuffer withObjects(Object... items) {
if (items == null) {
return this;
}
if (this.buffer == null) {
Method startsWith
has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring. Open
public boolean startsWith(CharSequence prefix, int toffset, boolean ignoreCase) {
if (buffer == null) {
return false;
}
char ta[] = buffer;
- Read upRead up
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 replace
has a Cognitive Complexity of 13 (exceeds 5 allowed). Consider refactoring. Open
public void replace(char search, char replace) {
int len = this.length + start;
int pos = position + start;
if (replace > 0) {
while (pos < len) {
- Read upRead up
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 withStart
has 39 lines of code (exceeds 25 allowed). Consider refactoring. Open
public CharacterBuffer withStart(CharSequence item, boolean newLine) {
if (item == null || start < 0) {
return this;
}
int len = item.length();
Method getStringPart
has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring. Open
public String getStringPart(Character start, Character end) {
if (start == null || end == null) {
return null;
}
int count = 1;
- Read upRead up
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 withRepeat
has a Cognitive Complexity of 12 (exceeds 5 allowed). Consider refactoring. Open
public boolean withRepeat(String string, int rest) {
if (string == null) {
return false;
}
int newCapacity = this.length() + rest * string.length();
- Read upRead up
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 with
has 35 lines of code (exceeds 25 allowed). Consider refactoring. Open
public CharacterBuffer with(CharSequence values, int start, int end) {
if (values == null || (buffer != null && start > buffer.length)) {
return this;
}
if (this.buffer == null) {
Method equalsText
has 33 lines of code (exceeds 25 allowed). Consider refactoring. Open
public boolean equalsText(char... other) {
if (other == null) {
return true;
}
if (start > length || start < 0 || buffer == null) {
Method with
has 32 lines of code (exceeds 25 allowed). Consider refactoring. Open
public CharacterBuffer with(CharSequence... items) {
if (items == null) {
return this;
}
if (this.buffer == null) {
Method indexOf
has a Cognitive Complexity of 11 (exceeds 5 allowed). Consider refactoring. Open
public int indexOf(CharSequence str, int fromIndex) {
final int max = length();
if (fromIndex < 0) {
fromIndex = 0;
} else if (fromIndex >= length) {
- Read upRead up
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 getLine
has a Cognitive Complexity of 10 (exceeds 5 allowed). Consider refactoring. Open
public CharacterBuffer getLine(int pos) {
CharacterBuffer buffer = new CharacterBuffer();
if (pos > 0 && pos < length()) {
int start = pos;
while (this.buffer[start] != '\r' && this.buffer[start] != '\n' && start > 0) {
- Read upRead up
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 rtrim
has 27 lines of code (exceeds 25 allowed). Consider refactoring. Open
public CharacterBuffer rtrim(char... items) {
validateValue();
if (items != null) {
int z;
while (length > 0) {
Method getStringPart
has 26 lines of code (exceeds 25 allowed). Consider refactoring. Open
public String getStringPart(Character start, Character end) {
if (start == null || end == null) {
return null;
}
int count = 1;
Method trim
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
public CharacterBuffer trim() {
if (buffer == null) {
return this;
}
if (length > buffer.length) {
- Read upRead up
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 with
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
public CharacterBuffer with(char src) {
if (this.buffer == null) {
this.buffer = new char[5];
start = 0;
length = 1;
- Read upRead up
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 write
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
public CharacterBuffer write(byte[] values, int length) {
if (values == null) {
return this;
}
if (this.length < 0) {
- Read upRead up
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 with
has a Cognitive Complexity of 8 (exceeds 5 allowed). Consider refactoring. Open
public CharacterBuffer with(byte[] values, int start, int length) {
int newLen = length + this.length;
if (buffer == null || newLen + this.start > buffer.length) {
char[] oldValue = this.buffer;
this.buffer = new char[(newLen * 2 + 2)];
- Read upRead up
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
Consider simplifying this complex logical expression. Open
if (pos > 0 && pos < length()) {
int start = pos;
while (this.buffer[start] != '\r' && this.buffer[start] != '\n' && start > 0) {
start--;
}
Method with
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
public CharacterBuffer with(char[] values, int start, int end, boolean copy) {
if (copy) {
if (values != null) {
if (start > values.length) {
return this;
- Read upRead up
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 isEmptyCharacter
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
public boolean isEmptyCharacter() {
if (super.isEmpty() || start > length || start < 0) {
return true;
}
if (length > buffer.length) {
- Read upRead up
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 addValues
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
public boolean addValues(char sep, String... values) {
if (values == null) {
return false;
}
int size = this.size();
- Read upRead up
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 splitStrings
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
public SimpleList<String> splitStrings(char split) {
SimpleList<String> result = new SimpleList<String>();
if (buffer == null) {
return result;
- Read upRead up
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 remove
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
public char remove(int position) {
if (position < 0 || this.buffer == null || position > this.buffer.length) {
return 0;
}
char oldChar = this.buffer[position];
- Read upRead up
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 withCollection
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
public CharacterBuffer withCollection(String splitter, Object... values) {
if (values == null) {
return this;
}
int len = values.length;
- Read upRead up
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 with
has a Cognitive Complexity of 7 (exceeds 5 allowed). Consider refactoring. Open
public CharacterBuffer with(char[] values, int start, int length) {
if (values == null || start + length > values.length) {
return this;
}
if (this.length < 0) {
- Read upRead up
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 validateValue
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
private boolean validateValue() {
if (buffer == null) {
this.start = 0;
this.length = 0;
return true;
- Read upRead up
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 add
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
public boolean add(Object... values) {
if (values == null) {
return true;
}
boolean addValues = true;
- Read upRead up
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 withStart
has a Cognitive Complexity of 6 (exceeds 5 allowed). Consider refactoring. Open
public CharacterBuffer withStart(char item) {
if (start > 0) {
if (start > this.buffer.length) {
start = this.buffer.length - 1;
}
- Read upRead up
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
Avoid using a branching statement as the last in a loop. Open
return false;
- Read upRead up
- Exclude checks
AvoidBranchingStatementAsLastInLoop
Since: PMD 5.0
Priority: Medium High
Categories: Style
Remediation Points: 50000
Using a branching statement as the last part of a loop may be a bug, and/or is confusing. Ensure that the usage is not a bug, or consider using another approach.
Example:
// unusual use of branching statement in a loop
for (int i = 0; i < 10; i++) {
if (i*i <= 25) {
continue;
}
break;
}
// this makes more sense...
for (int i = 0; i < 10; i++) {
if (i*i > 25) {
break;
}
}
These nested if statements could be combined Open
if (Character.toLowerCase(c1) == Character.toLowerCase(c2)) {
continue;
}
- Read upRead up
- Exclude checks
CollapsibleIfStatements
Since: PMD 3.1
Priority: Medium
Categories: Style
Remediation Points: 50000
Sometimes two consecutive 'if' statements can be consolidated by separating their conditions with a boolean short-circuit operator.
Example:
void bar() {
if (x) { // original implementation
if (y) {
// do stuff
}
}
}
void bar() {
if (x && y) { // optimized implementation
// do stuff
}
}