Coder Social home page Coder Social logo

Comments (5)

terryyin avatar terryyin commented on July 18, 2024 1

@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.

rakhimov avatar rakhimov commented on July 18, 2024

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.

terryyin avatar terryyin commented on July 18, 2024

No, so far return type is not really needed in any scenario yet.

So there's still little motivation to implement it...

from lizard.

JiahangLi avatar JiahangLi commented on July 18, 2024

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.

rakhimov avatar rakhimov commented on July 18, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.