Comments (5)
@JiahangLi you are right.
I know some tools e.g. Sonar does count the return also for CCN, in which I strongly disagree. The underlying reason is best explained here: http://programmers.stackexchange.com/questions/118703/where-did-the-notion-of-one-return-only-come-from/118793#118793
In a modern programming language a method always start from a single entry, which is the beginning of the method. And a method always return to a single exit, which is where the method is called (unless exception is thrown). So adding a return doesn't really create any extra path.
I think the best way of viewing the effect of CCN is from testing's perspective. In a way, CCN is a measurement for how hard it is to test a method. An early return in any branch of a method doesn't add extra path to make the testing harder. Actually, because of the early return, it potentially simplifies the test.
Methods with early returns are also easier to read.
That is to say, I'm a strong supporter for early returns. Lizard isn't an opinionated tool, though. Unless I'm show some papers saying counting returns is useful, I'd like to keep it this way (the standard way).
from lizard.
This can get tricky with trailing return type and automatic return type deduction of C++11/14.
auto foo() -> int { return 42; } // C++11
auto foo() -> decltype(bar(baz)) { return bar(baz); } // C++ 11
auto foo() { return bar(baz); } // C++14
Why is it important to include the return type with the name of the function?
Is this only relevant to reporting?
Would it then be suggested to include the full function signature as well return_type name (args)
?
from lizard.
No, so far return type is not really needed in any scenario yet.
So there's still little motivation to implement it...
from lizard.
One follow up on this discussion, since I was using the other tool to measure CCN before and it gave me the different number of CCN due to the return statement from my assumption.
Here is an example:
int main (){
int a ,b ;
a = 26;
b =10;
if (a > 10 ||b < 45 && (a+b)< 55){
return 1;
}
return 2;
}
I might be wrong, but here is my observation and thoughts.
The other tool give me CCN =5, but by counting decision points CCN supposed to be 4.
In order to figure out which part of the code caused this problem. I deleted the "return 2" line.
int main (){
int a ,b ;
a = 26;
b =10;
if (a > 10 ||b < 45 && (a+b)< 55){
return 1;
}
}
The other tool returns CCN=4 which agreed with the assumption.
int main (){
for( int i = 0 ; i < 10 ; i ++){
print ("hello");
}
return 0;
}
The other tool returns CCN =2, but personally I think it should be 1. Am I right?
Is counting decision points always the acceptable shortcut to calculate CCN or it might fall into pitfalls sometimes?
from lizard.
@terryyin Nice research on CCN.
int foo() {
if (bar()) return 42;
return -42;
}
void foo(int* eax) {
if (bar) *eax = 42;
*eax = -42;
return;
}
These functions have the same control flow. (returns to the same place)
It is interesting to consider long jumps and exceptions.
Can they be considered as multiple returns?
from lizard.
Related Issues (20)
- Python Hinting causes issues with function parameter count
- Show CCN only, hide other fields
- lizard --sort cyclomatic_complexity --CCN 10 shows a file with CCN score of 1
- lizard -w --sort cyclomatic_complexity --CCN 10 -H includes all files instead of CCN >10
- Is it accurate to get the result data of fan-in fan-out HOT 1
- lizard gets confused by C++ thousands seperator in hex and binary numbers
- Lizard groups nearly 100 lines into one "function" in a Rust file HOT 4
- Installing lizard using pip doesn't work
- Process error with Objective-C block
- Lizard script doesn't working with jython in springboot applicatoion
- Wrong PARAM count for void type in C/C++
- top_nesting_level stay to 0
- Extension documentation HOT 2
- are there plans to add support for python 3.11 HOT 1
- Not used function_stack property
- Implement argument to set duplicate threshold and add possibility to skip part of file
- Extensions "mjs" and "cjs" do not work HOT 1
- Allow for alternatives to 'GENERATED CODE' to indicate generated files
- Allow for failing cases to be printed to stderr
- Fail brew install on XcodeCloud HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lizard.