0

I have the following code:

private void generateKeyList(String pdm, List<Key> uKs,
                                 List<PhysicalKey> pKs) {
        if (pKs == null && uKs == null)
            return;
        JsonObject concepts = getConcepts(pdm);
        String indexTypeRequired = uKs == null ? "PRIMARY-KEY" : "UNIQUE";
        for (String concept : concepts.keySet()) {
            var indexes = concepts.getAsJsonObject(concept).getAsJsonArray("indexes");
            if (indexes == null)
                continue;
            for (JsonElement index : indexes) {
                JsonObject indexO = (JsonObject) index;
                var indexType = indexO.getAsJsonPrimitive("indexType").getAsString();
                var name = indexO.getAsJsonPrimitive("name").getAsString();
                var keys = indexO.getAsJsonArray("key").getAsJsonArray();
                if (indexType.equals(indexTypeRequired)) {
                    for (JsonElement indexKey : keys) {
                        var keyString = indexKey.getAsString();
                        var key = pKs == null ? new Key() : new PhysicalKey();
                        key.setName(name).setType(indexType).setTable(concept).setColumn(keyString);
                        if (uKs == null)
                            pKs.add((PhysicalKey) key);
                        else
                            uKs.add(key);
                    }
                }
            }
        }
    }

I am getting an issue from sonarLint that the Cognitive Complexity of this code is 26 and it should be <=15
Not sure how to reduce the complexity here.
Any ideas of hints?

pensee
  • 375
  • 1
  • 10
  • 2
    Extracting a part of the logic to another method will help decrease the complexity. Many IDEs can help you do that easily. – Arnaud Aug 09 '23 at 13:44
  • 1
    I'm not sure exactly how SonarLint calculates Cognitive Complexity, but what version of Java are you using? Are Streams available to you? At the least, it would make your code much more readable. – Zircon Aug 09 '23 at 13:50
  • 2
    You could start by extracting the `for (JsonElement index : indexes) {` loop. But your goal should not be to get rid of the warning but to make your code as clear as possible. As such, you should make sure the method makes sense and has a descriptive name. SonarLint's cognitive complexity is calculated using the number of control flow statements (e.g. conditions or loops) while increasing the weight of nested statements. For example, if you have an `if` in a `while` loop, you get 1 "complexity" for the `while` and 2 for the `if` because it's nested once. – dan1st Aug 09 '23 at 13:51
  • @dan1st extracting the for loop you mentioned removed the error. That was so easy. I understand though that the methods should be more descriptive – pensee Aug 10 '23 at 12:43
  • @dan1st you can post your solution, so I can mark it – pensee Aug 18 '23 at 18:28

1 Answers1

0

Below I listed a couple of hints based on your code. Of course, there are many many more. You might find Clean Code by Robert Martin an interesting read.

  1. Extract code that is in for loops to a separate method (in your case you can do this twice)
  2. Extract code that is in if or else conditions to separate methods
if (condition){
   doSomething();
} else{
   doSomethingElse();
}
  1. Avoid continue

Better to change

if (indexes == null)
                continue;

to

if (indexes != null){
    for (JsonElement index : indexes) {...}
}
WLefever
  • 296
  • 2
  • 9