Skip to main content
18 votes

if and else or if and return?

It depends on the semantics of your code, if the else branch is the point of the method, i.e. it's named after what happens in the else branch, the early return is probably correct, especially if the ...
esoterik's user avatar
  • 3,937
6 votes
Accepted

Reducing Cognitive Complexity of Single-Responsibility "Arrow-Code"

According to your own comment, you do not build the result based on parameters but you rather mapped specific parameter combinations to some constant. So why not use some ParameterTuple=>Constant ...
Matthias's user avatar
6 votes
Accepted

cyclomatic complexity, number of tests for full coverage?

Forget line coverage. This metric is useless and misleading. Here, what you should consider is branch coverage and path coverage. Wikipedia article you mentioned gives you the actual formula: branch ...
Arseni Mourzenko's user avatar
5 votes
Accepted

Why does this activity diagram not have the same amount of paths as the cyclomatic complexity number?

(i) how is one supposed to know that (a>=0)&&(a<=100)&&(b>=0)&&(b<=100)&&(c>=0)&&(c<=100) should lead to three different test cases You're ...
candied_orange's user avatar
4 votes

Cyclomatic Complexity Question

I am not convinced your control flow graph is correct because it shows two terminal nodes 9 and 12. Instead, there should be a single terminal node for the return. A difficulty with your code is that ...
amon's user avatar
  • 136k
4 votes

Reducing Cognitive Complexity of Single-Responsibility "Arrow-Code"

If you are attempting to optimize for readability and not optimizing for cleverness or performance or brevity, this is the way I would write it. String getMyString(String parameter1, String parameter2)...
John Wu's user avatar
  • 27k
4 votes

Does it make sense to compute cyclomatic complexity/lines of code ratio?

As noted in a previous reply, this statement in the accepted answer is clearly incorrect. The ratio has about the same prediction ability as either used separately. CC density has been found to make ...
Nikos Houssos's user avatar
3 votes

Reducing cyclomatic complexity of a state machine

If the sections have a non-overlapping structure, then strongly consider encoding the state machine via ordinary control flow. Regardless of whether that is possible, you can likely separate low-level ...
amon's user avatar
  • 136k
3 votes

if and else or if and return?

If "something" and "other thing" are simple, straightforward, and understandable, it doesn't matter because both work. // The first way is completely readable private void test(String str) { if (...
Chris G's user avatar
  • 204
2 votes
Accepted

Are short-circuiting paths considered for path coverage?

So I understand what you are saying to mean that this: if(A && B && C) { ... } Has this graph: Which after drawing it out, I think indeed has to be correct. If A,B and C have no side ...
JimmyJames's user avatar
  • 30.9k
2 votes
Accepted

Summing cyclomatic complexity of function or files

Your assumption that the sum of CCs is the aggregate CC is correct, but perhaps not very useful. Cyclomatic complexity is based on the control flow graph. Usually, we only look at the control flow ...
amon's user avatar
  • 136k
2 votes

Reducing cyclomatic complexity of a state machine

You seem to find the code reasonable; it doesn't seem crazy to me (but I'm not the one working on it) and you only care about this because a coworker ran some third-party code analysis tool which ...
Stack Exchange Broke The Law's user avatar
1 vote

Reducing cyclomatic complexity of a state machine

This is a prototype You have figured out how to do what you need on a trial-and-error basis and now know what you have and what you need. If your boss think that the people who think that the PyLint ...
Thorbjørn Ravn Andersen's user avatar
1 vote
Accepted

Reducing cyclomatic complexity of a state machine

use pipelines OP describes a common problem. Here is a motivating use case I have often encountered: $ grep "Smith" /var/log/messages But, alas! The Smith customer appears in both errors ...
J_H's user avatar
  • 7,891
1 vote

Summing cyclomatic complexity of function or files

We are talking about the property of one unit of code as a measure of maintainability and of how hard it is to understand what it does. Applying it to a file is meaningless because just having a lot ...
Martin Maat's user avatar
  • 18.6k
1 vote

What are the potential tradeoffs for lower down Cyclomatic Complexity

Low cyclomatic complexity does not necessarily imply that code is easier to understand, so often trying to lower it in a function just amounts to sweeping complexity under the rug. I see a lot of ...
Beefster's user avatar
  • 303
1 vote
Accepted

How to compute cyclomatic complexity for empty whiles and if + breaks?

I would first say that you should generally favor not using infinite loops with an internal break. They are much harder to follow for readers of your code and likely to cause maintenance problems. ...
user1118321's user avatar
  • 4,981

Only top scored, non community-wiki answers of a minimum length are eligible