4

I have some piece of code that basically looks like this:

public MyObject getData(boolean someFlag) {

    String select1 = "SELECT * FROM myTable WHERE someInteger = ?";
    SqlHostvariablen hostvars = new SqlHostvara();
    hostvars.addInteger(myField.getSomeInteger);
    String[][] selarray = SqlHelper.doSelectAsMatrix(select1, hostvars);
    if (selarray.length == 0) {
        throw new IllegalArgumentException("Nothing found");
    }
    MyObject foo = new MyObject();
    int i = 0;
    foo.setSomething1(selarray[0][i++]);
    foo.setSomething2(selarray[0][i++]);
    foo.setSomething3(selarray[0][i++]);
    foo.setSomething4(selarray[0][i++]);
    foo.setSomething5(selarray[0][i++]);
    foo.setSomething6(selarray[0][i++]);
    foo.setSomething7(selarray[0][i++]);
    foo.setSomething8(transformSomething8(selarray[0][i++]));
    foo.setSomething9(selarray[0][i++]);
    foo.setSomething10(selarray[0][i++]);
    String someValue1 = selarray[0][i++];
    String someValue2 = selarray[0][i++];
    foo.setSomething11(selarray[0][i++]);

    doSomethingWithFoo(foo, someFlag, someValue1, someValue2);
    doSomethingElseWithFoo(foo);

    return foo;
}

The identifiers and SQL statement are anonymized but otherwise my method looks the same.

Now Checkstyle claims that the cyclomatic comlexity if this method is 12. I always thought I knew what CC was and from my knowledge I'd say this methods CC is 2. There is one if that creates a new path through the code and the control flow graph therefore has 2 paths/exit points. I don't see where else there should be a path through the code.

Am I missing something entirely or is Checkstyle just wrong?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
André Stannek
  • 7,773
  • 31
  • 52
  • 1
    Maybe some hidden paths due to index-out-of-bounds unhandled exceptions on the array operations? Just a thought. – criticalfix Aug 08 '13 at 16:09
  • @criticalfix thought about that too but couldn't find any information on something like that :-/ – André Stannek Aug 08 '13 at 16:09
  • Perhaps it would be more straightforward to test the hypothesis directly. If you comment out one of your foo.setSomething lines and your CC goes down to 11, criticalfix may be on to something – Pete Baughman Aug 08 '13 at 16:37
  • Turns out turning it off an on again helped. The warning has disapeared this morning. Even cleaning the project didn't help yesterday. Sorry to have bothered you guys and thanks for the help. I will close this question now and delete it later so that interested people can still read this comment for some time. – André Stannek Aug 09 '13 at 06:06
  • @stonedsquirrel: Better [answer the question](http://stackoverflow.com/help/self-answer) with "*Checkstyle did need a restart*" – Bergi Aug 09 '13 at 13:37
  • @Bergi discarded that option because I considered the problem too localized now. But if you also think thats a good option I'll do so :-) – André Stannek Aug 09 '13 at 13:56

1 Answers1

2

Turned out this was a Checkstyle error. While not even cleaning the problem did the trick, after a system restart the warning was gone. An Eclipse restart might have been enough, no way to know for sure.

André Stannek
  • 7,773
  • 31
  • 52