How many values are errors in Go?

This post is written as a follow-up on Go’s Error Handling Sucks – A quantitative analysis upon the request of Herb Sutter who asked for a rough percentage of function calls that return error information to all function calls.

For this purpose, I have used the following regex for capturing function calls that return an error:

[a-zA-Z0-9_]*[eE]rr(or)? *:?= *([a-zA-Z0-9_]*\.)*[a-z-A-Z0-9_]+\(

and the following regex for capturing any function call:

([a-zA-Z0-9_]*\.)*[a-z-A-Z0-9_]+\(

The first regex relies on the convention that error is the last (or the only) value of any function that returns error information.

The second regex has the following shortcomings which should be understood:

  • Casts are mistaken as function calls.
    E.g. ENFILE = syscall.Errno(0x17)
  • Counting built-in “functions” such as len, append etc might be undesirable.
  • Function definitions, imports, and consts are confused as function calls, but I filtered them in my calculations.

Lastly, I have used the following commands to count:

ag --go --stats-only "[a-zA-Z0-9_]*[eE]rr(or)? *:?= *([a-zA-Z0-9_]*\\.)*[a-z-A-Z0-9_]+\\("

ag --go -v "func|const \\(|import \\(" | ag --stats-only "([a-zA-Z0-9_]*\\.)*[a-z-A-Z0-9_]+\\("

Here are the results:

# of Calls Returning error# of Function CallsRatio
Kubernetes83,442740,38511.27%
Go27,741482,5465.75%
Moby (Docker)37,044310,73611.92%
Cockroach DB24,498254,9269.61%
Mattermost (server)22,737214,75610.59%
etcd13,761135,95710.12%
Syncthing2,43625,4729.56%
Hugo1,87623,9837.82%
Helm1,92711,44316.84%
Gin2425,3074.56%
TOTAL215,7042,205,5119.78%

For the record, here are the repositories used and their respective commits:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.