griesemer / tryhard

tryhard finds and rewrites code suitable for try.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

tryhard reports only 44 'if' statements for gorm, which seems low

thepudds opened this issue · comments

Sorry if this is just user error, but tryhard seems to be reporting stats that are lower than I would expect for github.com/jinzhu/gorm.

In particular, the current version of tryhard is only reporting 44 if statements, but grepping and other spot checking (e.g., using https://github.com/MichaelTJones/gg) seems to indicate it should be several hundred if statements.

Steps to reproduce:

$ export GO111MODULE=off
$ git clone https://github.com/jinzhu/gorm /tmp/scratchpad/gorm
$ cd /tmp/scratchpad/gorm
$ go get -u github.com/griesemer/tryhard
$ tryhard .

Example output:

--- stats ---
    626 (100.0% of     626) func declarations
     50 (  8.0% of     626) func declarations returning an error
    234 (100.0% of     234) statements
     44 ( 18.8% of     234) if statements
      5 ( 11.4% of      44) if <err> != nil statements
      1 ( 20.0% of       5) try candidates
      0 (  0.0% of       5) <err> name is different from "err"
--- non-try candidates (-l flag lists file positions) ---
      0 (  0.0% of       5) { return ... zero values ..., expr }
      0 (  0.0% of       5) single statement then branch
      0 (  0.0% of       5) complex then branch; cannot use try
      0 (  0.0% of       5) non-empty else branch; cannot use try```

Go version:

go version go1.12.6 linux/amd64

tryhard only counts if statements in functions returning an error. There seem to be only 50 such functions. A cursory glance at the code doesn't suggest anything is wildly off.

Ahh, OK.

Sorry, I think you have already explained that elsewhere in response to other questions.

I think the statements line might be the only line that does not have the denominator tie back to one of the earlier lines?

Maybe rather than reading statements, maybe it could say something along the lines of statements within funcs returning an error, or something like that? Or maybe that is too verbose.

In any event, sorry for the noise.

I just looked at the code in some detail. It does indeed seem like there's only one obvious candidate for try - but there appears also only five err != nil checks that are try candidates. Though there are many if err != nil checks which handle errors in ways that are not amenable to try as the errors either are ignored, cause a panic, or are reported via some other mechanism; e.g. in tests. Using an if statement seems like a fine choice in these cases as the handling of the error often dominates the code (rather than the if err != nil.

I'm going to close this for now.

Fair enough, the statements count could be clearer. I will change that. Note also that function literals are completely ignored at the moment.