0
how to reduce CYCLOMATIC COMPLEXITY in this code
3 Réponses
+ 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
+ 1
Reduce the number of decisions the program has to make. I think you can reduce one of your if statements.
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.