Open terryyin opened 10 years ago
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)
?
No, so far return type is not really needed in any scenario yet.
So there's still little motivation to implement it...
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?
@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).
@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?
The return type is not identified and counted. E.g. int foo() { }
the function will be identified as foo()