Questions tagged [cognitive-complexity]

15 questions
1
vote
1 answer

How can I reduce Cognitive Complexity in this block ? By the way how can this be bigger than the 15 allowed?

I have this code: private void processMedia(Integer mediaId, List hiresPhysicalPaths) { final AtomicBoolean isHls = new AtomicBoolean(false); for (String hiresPhysicalPath : hiresPhysicalPaths) { …
JhinKazama
  • 55
  • 5
1
vote
0 answers

Usability testing with users with ASD?

Has anyone ever conducted usability testing with users with ASD? I need to test an existing website to make sure it is suitable for neurodivergent users as well. I would need to ask testers to perform a list of tasks on a targeted website and I've…
1
vote
0 answers

How to reduce cognitive complex in javascript event handler?

I have some element event handler uses if statement inside, why sonar complains this if statement causes cognitive complexity? Is there a way I can avoid this or improve this? const handleOnBlur = () => { if (isEditMode) { …
1
vote
0 answers

Viewing the cognitive complexity of all methods in eclipse

Is it possible to view the "Cognitive complexity" for all methods in Eclipse irrespective of whether it exceeds the limit or not using SonarLint plugin One workaround I used was to reduce the limit to 0 in Windows -> Preferences -> SonarLint ->…
0
votes
1 answer

Reduce cognitive Complexity of a method from 26 to 15

I have the following code: private void generateKeyList(String pdm, List uKs, List pKs) { if (pKs == null && uKs == null) return; JsonObject concepts = getConcepts(pdm); …
pensee
  • 375
  • 1
  • 10
0
votes
2 answers

Refactoring a method having multiple switch case statements & generics

I have an existing project based on Java in which an existing utility method is present which gets some values as an input and provides result as a boolean value. I need to refactor this method since there is an upcoming requirement which will cause…
0
votes
2 answers

Reduce cognitive complexity when mapping

if(obj.getAttribute() != null) { newobject.setAttribute(obj.getAttribute()); } if(obj.getAttribute() != null) { newobject.setAttribute(obj.getAttribute()); } if(obj.getAttribute() != null) { …
Robert
  • 78
  • 1
  • 8
0
votes
0 answers

Are there any good inter-method / inter-class complexity metrics?

A common advice on method/function length is to write small methods to increase both readability and testability. These are measured, for example, by cyclomatic and cognitive complexity metrics. Similar metrics apply to classes, or in general,…
0
votes
0 answers

How do I avoid multiple if/else statements or simply reduce the cognitive complexity?

How do I refactor ngOnChanges to reduce its Cognitive Complexity from the current 41 to the recommended 15? What I currently understand is reducing cognitive complexity is making it more readable to a new user. I tried certain things but I could not…
0
votes
5 answers

How to reduce cognitive complexity of nested if

I have an if statement set up like this if (A && B) { // do something 1 } else { if (B) { // set some boolean to false } // do something 2 } I'm wondering if I can lower the cognitive complexity? Right now this is a score of 4.
aspiringsomeone
  • 535
  • 2
  • 9
  • 18
0
votes
0 answers

Why does SonarLint allow a negative threshold for Cognitive complexity (Eclipse)

In Windows -> Preferences -> SonarLint -> Rules Configuration -> Java -> (search cognitive complexity), while selecting the rule for cognitive complexity, I was able to set a negative value for the threshold. I was able to successfully apply the…
Gautham M
  • 4,816
  • 3
  • 15
  • 37
-1
votes
2 answers

How to refactor this function to reduce its cognitive complexity issue for using of multiple if-elif-else statements in python?

My question is about how to refactor if-elif-else conditions in the code below. Piece of code has been changed here due to new implementation.Here V, X, Y are numbers. This code is for generating a random strings. def get_uid(type, key): if…
-1
votes
3 answers

How can I avoid using too much if statements / refactor this method?

It looks horrible, but I don't see how can I factorize that ? I thought of creating small boolean methods but I think it won't change too much, there will always be as many ifs ? private String getFolderValue(TableRow row) { String cote =…
-1
votes
1 answer

Cognitive complexity reduce issue

i made a login system with JavaScript for a game idea i had, but apparently my ide says it is too complex, do i need to split one function in more pieces? Do it reduces computer processing time? I just don't know if it's critical or not. Anyway this…
Zambon
  • 37
  • 1
  • 8
-1
votes
1 answer

Reduce Class Complexity - Cognitive Complexity C#

How can I improve the Cognitive Complexity in my code? I have a method which has while loop and inside that lot of IF ELSE blocks, I tried to remove IF ELSE with SWITCH Statements but no improvement with Cognitive Complexity as per SONAR cube…
Manas
  • 35
  • 6