how to reduce CYCLOMATIC COMPLEXITY in this code | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

how to reduce CYCLOMATIC COMPLEXITY in this code

https://code.sololearn.com/cuX3KWU0933P

12th Dec 2021, 7:37 PM
morgan
morgan - avatar
3 Answers
+ 2
Well, this h pointer is unnecessary in your code, why would you store all those inputs if you don't make any use of them? You should free the memory if you allocate it. You do not need to include math.h too. This piece of code does the same thing as your code: https://code.sololearn.com/cSo01CcH93Wj/?ref=app I guess it looks cleaner to read, and I got rid of unnecessary pointer. Also I moved 'h' into for loop so it is now limited by its scope
13th Dec 2021, 12:21 AM
Michal Doruch
+ 1
Reduce the number of decisions the program has to make. I think you can reduce one of your if statements.
12th Dec 2021, 8:16 PM
William Owens
William Owens - avatar
0
Set max = k before the loop to potentially reduce the number of times max gets updated. You can eliminate the if branch within the loop by using a boolean expression. Though it reduces cyclomatic complexity, it is probably less efficient. Below is a sample of how it can be done. Replace:         if(h[height_i]>max)             max=h[height_i]; With:         max += (h[height_i]>max)*(h[height_i] - max); The boolean expression evaluates to 0 or 1. If it is 0, there is no change to max. If it is 1, then it increases max by the difference between the higher value and max.
13th Dec 2021, 7:40 PM
Brian
Brian - avatar