{"id":15648995,"url":"https://github.com/nakabonne/nestif","last_synced_at":"2025-04-07T13:06:46.893Z","repository":{"id":46315251,"uuid":"233225050","full_name":"nakabonne/nestif","owner":"nakabonne","description":"Detect deeply nested if statements in Go source code","archived":false,"fork":false,"pushed_at":"2021-10-31T11:41:00.000Z","size":53,"stargazers_count":43,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-04T18:50:14.242Z","etag":null,"topics":["complexity","go","golang","golang-tools","linter","static-analysis"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-2-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nakabonne.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-01-11T12:01:23.000Z","updated_at":"2025-01-28T10:57:10.000Z","dependencies_parsed_at":"2022-08-20T10:31:09.901Z","dependency_job_id":null,"html_url":"https://github.com/nakabonne/nestif","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nakabonne%2Fnestif","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nakabonne%2Fnestif/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nakabonne%2Fnestif/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nakabonne%2Fnestif/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nakabonne","download_url":"https://codeload.github.com/nakabonne/nestif/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247657281,"owners_count":20974345,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["complexity","go","golang","golang-tools","linter","static-analysis"],"created_at":"2024-10-03T12:27:19.645Z","updated_at":"2025-04-07T13:06:46.866Z","avatar_url":"https://github.com/nakabonne.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# nestif\n\n[![Go Doc](https://img.shields.io/badge/godoc-reference-blue.svg?style=flat-square)](http://godoc.org/github.com/nakabonne/nestif)\n\nReports complex nested if statements in Go code, by calculating its complexities based on the rules defined by the [Cognitive Complexity white paper by G. Ann Campbell](https://www.sonarsource.com/docs/CognitiveComplexity.pdf).\n\nIt helps you find if statements that make your code hard to read, and clarifies which parts to refactor.\n\n## Installation\n\n### By go get\n\n```\ngo get github.com/nakabonne/nestif/cmd/nestif\n```\n\n### By golangci-lint\n\n`nestif` is already integrated with [golangci-lint](https://github.com/golangci/golangci-lint). Please refer to the instructions there and enable it.\n\n## Usage\n\n### Quick Start\n\n```bash\nnestif\n```\n\nThe `...` glob operator is supported, and the above is an equivalent of:\n\n```bash\nnestif ./...\n```\n\nOne or more files and directories can be specified in a single command:\n\n```bash\nnestif dir/foo.go dir2 dir3/...\n```\n\nPackages can be specified as well:\n\n```bash\nnestif github.com/foo/bar example.com/bar/baz\n```\n\n### Options\n\n```\nusage: nestif [\u003cflag\u003e ...] \u003cGo files or directories or packages\u003e ...\n  -e, --exclude-dirs strings   regexps of directories to be excluded for checking; comma-separated list\n      --json                   emit json format\n      --min int                minimum complexity to show (default 1)\n      --top int                show only the top N most complex if statements (default 10)\n  -v, --verbose                verbose output\n```\n\n### Example\n\nLet's say you write:\n\n```go\npackage main\n\nfunc _() {\n    if foo {\n        if bar {\n        }\n    }\n\n    if baz == \"baz\" {\n        if qux {\n            if quux {\n            }\n        }\n    }\n}\n```\n\nAnd give it to nestif:\n\n```console\n$ nestif foo.go\nfoo.go:9:2: `if baz == \"baz\"` is nested (complexity: 3)\nfoo.go:4:2: `if foo` is nested (complexity: 1)\n```\n\nNote that the results are sorted in descending order of complexity. In addition, it shows only the top 10 most complex if statements by default, and you can specify how many to show with `-top` flag.\n\n### Rules\n\nIt calculates the complexities of if statements according to the nesting rules of Cognitive Complexity.\nSince the more deeply-nested your code gets, the harder it can be to reason about, it assesses a nesting increment for it:\n\n```go\nif condition1 {\n    if condition2 { // +1\n        if condition3 { // +2\n            if condition4 { // +3\n            }\n        }\n    }\n}\n```\n\n`else` and `else if` increase complexity by one wherever they are because the mental cost has already been paid when reading the if:\n\n```go\nif condition1 {\n    if condition2 { // +1\n        if condition3 { // +2\n        } else if condition4 { // +1\n\t} else { // +1\n\t    if condition5 { // +3\n\t    }\n        }\n    }\n}\n```\n\n## Inspired by\n\n- [uudashr/gocognit](https://github.com/uudashr/gocognit)\n- [fzipp/gocyclo](https://github.com/fzipp/gocyclo)\n\n## Further reading\n\nPlease see the [Cognitive Complexity: A new way of measuring understandability](https://www.sonarsource.com/docs/CognitiveComplexity.pdf) white paper by G. Ann Campbell for more detail on Cognitive Complexity.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnakabonne%2Fnestif","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnakabonne%2Fnestif","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnakabonne%2Fnestif/lists"}