Skip to content

refactor: enable nestif in golgang-ci, cognitive and cyclomatic complexity in revive - #2286

Closed
faddat wants to merge 290 commits into
cometbft:mainfrom
faddat:faddat/nestif-cyclomatic-complexity-cognitive-complexity
Closed

faddat wants to merge 290 commits into
cometbft:mainfrom
faddat:faddat/nestif-cyclomatic-complexity-cognitive-complexity

Conversation

@faddat

@faddat faddat commented Feb 10, 2024 •

Copy link
Copy Markdown
Contributor

This PR will enable some new linters, and be based off of:

main + #1907

... so #1907 should be merged before this one, because that will greatly reduce the number of changed files from 203 to somewhere in the teens. If at any time it's needed to break any of this into its own PR's, just let me know.

New Linters

  • nestif set to 15
  • cyclomatic complexity via revive set to 71
  • cognitive complexity via revive set to 71
  • cyclop
  • gocyclo
  • gocognit

I got (in my opinion) amazing results from this. After the configuration, I was guided to what are more or less known trouble areas in comet, nearly every time.

Ongoing plan

Over time, we can lower the complexity rules and nestif to their defaults, and I think that this would likely have a great effect on the software overall.

Notes

I really liked some of the changes I started to get as I worked with these linters, which are all related to each other to some degree.

This PR takes some of the largest functions in cometbft, and breaks them into smaller pieces.

When I started it, I wasn't too sure, but by using this approach I was definitely able to make changes that seem to:

  • make things more reliable
  • make things easier to understand

Changes unique to this PR

This PR takes anything that the nestif/cyclomaticcomplexity/cognitivecomplexity linters finds to be too complex, and refactors it into individual functions. I wasn't too sure about this course of action when I started, but in a number of places, the results I started to get are (I think) quite convincing.

In places where the actual code has been changed, I'm finding that the reworked style is actually easier to read and think about.

It's likely that I will write out the whole PR, kind of like

And then submit the whole thing as a series of smaller patches.

For example, this technique has led me to poolRoutine, which is indeed a large and complex function. It runs consistently, and it is probably better for many reasons to break it into separate functions.


PR checklist

  • Tests written/updated
  • Changelog entry added in .changelog (we use unclog to manage our changelog)
  • Updated relevant documentation (docs/ or spec/) and code comments
  • Title follows the Conventional Commits spec

PR checklist

  • Tests written/updated
  • Changelog entry added in .changelog (we use unclog to manage our changelog)
  • Updated relevant documentation (docs/ or spec/) and code comments
  • Title follows the Conventional Commits spec

faddat added 30 commits January 1, 2024 20:07
cache_test.go, clist_mempool_test.go,
helpers_test.go, and status_test.go
netaddress_test.go, addrbook_test.go, and
switch_test.go
secp256k1_internal_test.go, group_test.go,
pool_test.go, verify_test.go, and state_test.go
http_test.go, and validation_test.go
setup_test.go, http_test.go, netaddress_test.go,
rpc_test.go, http_json_handler_test.go, and
tx_test.go
event_test.go, status_test.go, rpc_test.go,
http_json_handler_test.go, parse_test.go, and
results_test.go
chunks_test.go, store_test.go,
clist_mempool_test.go, peer_set_test.go,
addrbook.go, switch_test.go, rpc_test.go, and
validator_set_test.go
@faddat
faddat requested a review from a team as a code owner February 10, 2024 14:14
@faddat
faddat requested a review from a team February 10, 2024 14:14
@faddat faddat mentioned this pull request Feb 10, 2024
3 of 35 tasks
@faddat
faddat marked this pull request as draft February 10, 2024 23:06
@github-actions

Copy link
Copy Markdown

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions Bot added the stale For use by stalebot label Feb 23, 2024
@github-actions github-actions Bot closed this Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale For use by stalebot

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants