👻 ghost is a command-line tool for locating overly complex lines of code in Go.
It is designed with the intention that code should strive to be written in a linear, rather than nested way. This makes code easier to understand, highlights duplicate logic and ultimately leads to less bugs.
Pass one or multiple Go files:
ghost works out of the box with sane defaults, but there are some other options you can provide:
- -ignore-tests - Ignore test files.
- -max-line-complexity - The maximum allowed line complexity. (default 5)
- -never-fail - Always exit with 0.
The output of ghost (with default options) describes that line 50 is too complex:
The line is:
There is nothing logically incorrect with that line, but it is long, difficult to understand and can be tricky to inspect with a debugger.
There are lots of ways the above code can be rewritten. For me, once I understand what it's really doing I can create the function:
Now it can be simply written as: