Method loadArenasFromFile
has a Cognitive Complexity of 25 (exceeds 5 allowed). Consider refactoring. Open
public void loadArenasFromFile() {
if (main.getArenasConfig().getConfig().get("arenas") == null) {
return;
}
for (String key : main.getArenasConfig().getConfig().getConfigurationSection("arenas").getKeys(false)) {
- 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 loadArenasFromFile
has 71 lines of code (exceeds 25 allowed). Consider refactoring. Open
public void loadArenasFromFile() {
if (main.getArenasConfig().getConfig().get("arenas") == null) {
return;
}
for (String key : main.getArenasConfig().getConfig().getConfigurationSection("arenas").getKeys(false)) {
- Create a ticketCreate a ticket
Method getRandomArena
has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring. Open
public DArena getRandomArena() {
List<DArena> tempArenaList = new ArrayList<>();
for (DArena arena : arenas) {
boolean hasDefault = false;
for (DGamemode mode : arena.getUsableModes().keySet()) {
- 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 setUsableGamemodes
has a Cognitive Complexity of 14 (exceeds 5 allowed). Consider refactoring. Open
public void setUsableGamemodes(DArena a) {
for (DGamemode mode : a.getUsableModes().keySet()) {
if (a.getPossibleGeneratorLocations().size() < mode.getMaxgenerators()) {
a.getUsableModes().put(mode, false);
continue;
- 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 saveArenasToFile
has 29 lines of code (exceeds 25 allowed). Consider refactoring. Open
public void saveArenasToFile() {
for (DArena arena : arenas) {
String path = "arenas." + arena.getId() + ".";
FileConfiguration config = main.getArenasConfig().getConfig();
List<String> gamemodeStrings = new ArrayList<>();
- Create a ticketCreate a ticket
Method setUsableGamemodes
has 27 lines of code (exceeds 25 allowed). Consider refactoring. Open
public void setUsableGamemodes(DArena a) {
for (DGamemode mode : a.getUsableModes().keySet()) {
if (a.getPossibleGeneratorLocations().size() < mode.getMaxgenerators()) {
a.getUsableModes().put(mode, false);
continue;
- Create a ticketCreate a ticket
Refactor this method to reduce its Cognitive Complexity from 25 to the 15 allowed. Open
public void loadArenasFromFile() {
- 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
Define a constant instead of duplicating this literal "locations.exitGates." 6 times. Open
String facing = (String) config.get(path + "locations.exitGates." + numb + ".facing");
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Duplicated string literals make the process of refactoring error-prone, since you must be sure to update all occurrences.
On the other hand, constants can be referenced from many places, but only need to be updated in a single place.
Noncompliant Code Example
With the default threshold of 3:
public void run() { prepare("action1"); // Noncompliant - "action1" is duplicated 3 times execute("action1"); release("action1"); } @SuppressWarning("all") // Compliant - annotations are excluded private void method1() { /* ... */ } @SuppressWarning("all") private void method2() { /* ... */ } public String method3(String a) { System.out.println("'" + a + "'"); // Compliant - literal "'" has less than 5 characters and is excluded return ""; // Compliant - literal "" has less than 5 characters and is excluded }
Compliant Solution
private static final String ACTION_1 = "action1"; // Compliant public void run() { prepare(ACTION_1); // Compliant execute(ACTION_1); release(ACTION_1); }
Exceptions
To prevent generating some false-positives, literals having less than 5 characters are excluded.
Merge this if statement with the enclosing one. Open
if (arena.getUsableModes().get(mode)) {
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Merging collapsible if
statements increases the code's readability.
Noncompliant Code Example
if (file != null) { if (file.isFile() || file.isDirectory()) { /* ... */ } }
Compliant Solution
if (file != null && isFileOrDirectory(file)) { /* ... */ } private static boolean isFileOrDirectory(File file) { return file.isFile() || file.isDirectory(); }
Extract this nested try block into a separate method. Open
try {
- Read upRead up
- Create a ticketCreate a ticket
- Exclude checks
Nesting try
/catch
blocks severely impacts the readability of source code because it makes it too difficult to understand
which block will catch which exception.