Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cognitive Code complexity #55

Closed
3 tasks done
marcelloh opened this issue Oct 27, 2023 · 1 comment
Closed
3 tasks done

Cognitive Code complexity #55

marcelloh opened this issue Oct 27, 2023 · 1 comment

Comments

@marcelloh
Copy link

Bug Description

read about it here:
https://dev.to/marcello_h/solve-code-complexity-in-go-56hg
It is up to you what you want to do with this knowledge.

My report:
litecart/internal/app.go:34:1 - NewApp has complexity: 27
complexity = 1

  • 1 (found 'if' at line: 40, complexity = 2)
  • 1 (found 'if' at line: 45, complexity = 3)
  • 1 (found 'if' at line: 57, complexity = 4)
  • 1 (found 'if with lines >= 10' at line: 57, complexity = 5)
    • 2 (found 'if' at line: 59, complexity = 7)
    • 2 (found 'else' at line: 61, complexity = 9)
      • 3 (found 'if' at line: 62, complexity = 12)
  • 1 (found 'if' at line: 83, complexity = 13)
  • 1 (found 'if' at line: 97, complexity = 14)
  • 1 (found 'if' at line: 107, complexity = 15)
  • 1 (found 'if with lines >= 10' at line: 107, complexity = 16)
  • 1 (found 'if with lines >= 25' at line: 107, complexity = 17)
    • 2 (found 'if' at line: 122, complexity = 19)
    • 2 (found 'if' at line: 127, complexity = 21)
  • 1 (found 'if' at line: 133, complexity = 22)
  • 1 (found 'else' at line: 135, complexity = 23)
  • 1 (found 'else with lines >= 10' at line: 135, complexity = 24)
    • 2 (found 'go' at line: 138, complexity = 26)
    • 2 (found 'go with lines >= 10' at line: 138, complexity = 27)

litecart/internal/handlers/private/setting.go:19:1 - Version has complexity: 14
complexity = 1

  • 1 (found 'if' at line: 23, complexity = 2)
  • 1 (found 'logical operator' at line: 23, complexity = 3)
  • 1 (found 'if' at line: 28, complexity = 4)
  • 1 (found 'if with lines >= 10' at line: 28, complexity = 5)
    • 2 (found 'if' at line: 32, complexity = 7)
    • 2 (found 'if' at line: 36, complexity = 9)
    • 2 (found 'if' at line: 41, complexity = 11)
    • 2 (found 'if' at line: 47, complexity = 13)
  • 1 (found 'if' at line: 52, complexity = 14)

litecart/internal/handlers/public/cart.go:17:1 - Checkout has complexity: 13
complexity = 1

  • 1 (found 'if' at line: 19, complexity = 2)
  • 1 (found 'range' at line: 24, complexity = 3)
  • 1 (found 'if' at line: 32, complexity = 4)
  • 1 (found 'if' at line: 37, complexity = 5)
  • 1 (found 'range' at line: 42, complexity = 6)
  • 1 (found 'range with lines >= 10' at line: 42, complexity = 7)
  • 1 (found 'range with lines >= 25' at line: 42, complexity = 8)
    • 2 (found 'range' at line: 44, complexity = 10)
    • 2 (found 'if' at line: 61, complexity = 12)
  • 1 (found 'if' at line: 81, complexity = 13)

litecart/internal/queries/cart.go:21:1 - Carts has complexity: 17
complexity = 1

  • 1 (found 'if' at line: 39, complexity = 2)
  • 1 (found 'defer' at line: 42, complexity = 3)
  • 1 (found 'for' at line: 44, complexity = 4)
  • 1 (found 'for with lines >= 10' at line: 44, complexity = 5)
  • 1 (found 'for with lines >= 25' at line: 44, complexity = 6)
    • 2 (found 'if' at line: 60, complexity = 8)
    • 2 (found 'if' at line: 64, complexity = 10)
    • 2 (found 'if' at line: 68, complexity = 12)
    • 2 (found 'if' at line: 72, complexity = 14)
    • 2 (found 'if' at line: 76, complexity = 16)
  • 1 (found 'if' at line: 83, complexity = 17)

litecart/internal/queries/cart.go:151:1 - CartSendMail has complexity: 74
complexity = 1

  • 1 (found 'if' at line: 158, complexity = 2)
    • 2 (found 'if' at line: 159, complexity = 4)
  • 1 (found 'if' at line: 165, complexity = 5)
  • 1 (found 'if' at line: 170, complexity = 6)
  • 1 (found 'defer' at line: 173, complexity = 7)
  • 1 (found 'defer with lines >= 5' at line: 173, complexity = 8)
    • 2 (found 'if' at line: 174, complexity = 10)
  • 1 (found 'logical operator' at line: 174, complexity = 11)
    • 2 (found 'else' at line: 176, complexity = 13)
  • 1 (found 'range' at line: 181, complexity = 14)
  • 1 (found 'range with lines >= 10' at line: 181, complexity = 15)
  • 1 (found 'range with lines >= 25' at line: 181, complexity = 16)
  • 1 (found 'range with lines >= 50' at line: 181, complexity = 17)
    • 2 (found 'if' at line: 184, complexity = 19)
      • 3 (found 'if' at line: 185, complexity = 22)
    • 2 (found 'if' at line: 191, complexity = 24)
    • 2 (found 'if with lines >= 10' at line: 191, complexity = 25)
      • 3 (found 'if' at line: 193, complexity = 28)
      • 3 (found 'defer' at line: 196, complexity = 31)
      • 3 (found 'for' at line: 198, complexity = 34)
      • 3 (found 'for with lines >= 10' at line: 198, complexity = 35)
        • 4 (found 'if' at line: 206, complexity = 39)
    • 2 (found 'if' at line: 213, complexity = 41)
    • 2 (found 'if with lines >= 10' at line: 213, complexity = 42)
      • 3 (found 'if' at line: 216, complexity = 45)
  • 1 (found 'logical operator' at line: 216, complexity = 46)
    • 3 (found 'if' at line: 219, complexity = 49)
    • 3 (found 'if with lines >= 10' at line: 219, complexity = 50)
      • 4 (found 'if' at line: 221, complexity = 54)
        • 5 (found 'if' at line: 222, complexity = 59)
      • 4 (found 'if' at line: 227, complexity = 63)
  • 1 (found 'if' at line: 236, complexity = 64)
  • 1 (found 'if' at line: 240, complexity = 65)
  • 1 (found 'if' at line: 243, complexity = 66)
  • 1 (found 'if' at line: 250, complexity = 67)
    • 2 (found 'range' at line: 252, complexity = 69)
  • 1 (found 'if' at line: 258, complexity = 70)
    • 2 (found 'range' at line: 260, complexity = 72)
  • 1 (found 'if' at line: 274, complexity = 73)
  • 1 (found 'if' at line: 278, complexity = 74)

litecart/internal/queries/install.go:19:1 - Install has complexity: 17
complexity = 1

  • 1 (found 'defer' at line: 21, complexity = 2)
  • 1 (found 'if' at line: 24, complexity = 3)
  • 1 (found 'defer' at line: 27, complexity = 4)
  • 1 (found 'defer with lines >= 5' at line: 27, complexity = 5)
    • 2 (found 'if' at line: 28, complexity = 7)
  • 1 (found 'logical operator' at line: 28, complexity = 8)
    • 2 (found 'else' at line: 30, complexity = 10)
  • 1 (found 'if' at line: 37, complexity = 11)
  • 1 (found 'if' at line: 42, complexity = 12)
  • 1 (found 'defer' at line: 45, complexity = 13)
  • 1 (found 'if' at line: 49, complexity = 14)
  • 1 (found 'range' at line: 62, complexity = 15)
    • 2 (found 'if' at line: 63, complexity = 17)

litecart/internal/queries/pages.go:26:1 - ListPages has complexity: 13
complexity = 1

  • 1 (found 'if' at line: 32, complexity = 2)
  • 1 (found 'if' at line: 37, complexity = 3)
  • 1 (found 'defer' at line: 40, complexity = 4)
  • 1 (found 'for' at line: 42, complexity = 5)
  • 1 (found 'for with lines >= 10' at line: 42, complexity = 6)
    • 2 (found 'if' at line: 48, complexity = 8)
    • 2 (found 'if' at line: 52, complexity = 10)
    • 2 (found 'if' at line: 56, complexity = 12)
  • 1 (found 'if' at line: 63, complexity = 13)

litecart/internal/queries/products.go:22:1 - ListProducts has complexity: 21
complexity = 1

  • 1 (found 'if' at line: 44, complexity = 2)
  • 1 (found 'if with lines >= 10' at line: 44, complexity = 3)
    • 2 (found 'range' at line: 51, complexity = 5)
  • 1 (found 'if' at line: 58, complexity = 6)
  • 1 (found 'if' at line: 64, complexity = 7)
  • 1 (found 'defer' at line: 67, complexity = 8)
  • 1 (found 'for' at line: 69, complexity = 9)
  • 1 (found 'for with lines >= 10' at line: 69, complexity = 10)
  • 1 (found 'for with lines >= 25' at line: 69, complexity = 11)
    • 2 (found 'if' at line: 82, complexity = 13)
    • 2 (found 'if' at line: 86, complexity = 15)
  • 1 (found 'logical operator' at line: 86, complexity = 16)
    • 2 (found 'if' at line: 90, complexity = 18)
  • 1 (found 'if' at line: 97, complexity = 19)
  • 1 (found 'if' at line: 103, complexity = 20)
  • 1 (found 'logical operator' at line: 103, complexity = 21)

litecart/internal/queries/products.go:111:1 - Product has complexity: 13
complexity = 1

  • 1 (found 'if' at line: 132, complexity = 2)
  • 1 (found 'else' at line: 134, complexity = 3)
  • 1 (found 'if' at line: 158, complexity = 4)
    • 2 (found 'if' at line: 159, complexity = 6)
  • 1 (found 'if' at line: 165, complexity = 7)
  • 1 (found 'if' at line: 169, complexity = 8)
  • 1 (found 'logical operator' at line: 169, complexity = 9)
  • 1 (found 'if' at line: 173, complexity = 10)
  • 1 (found 'if' at line: 177, complexity = 11)
  • 1 (found 'if' at line: 181, complexity = 12)
  • 1 (found 'if' at line: 185, complexity = 13)

litecart/internal/queries/products.go:350:1 - ProductDigital has complexity: 31
complexity = 1

  • 1 (found 'defer' at line: 354, complexity = 2)
  • 1 (found 'if' at line: 357, complexity = 3)
  • 1 (found 'defer' at line: 361, complexity = 4)
  • 1 (found 'defer with lines >= 5' at line: 361, complexity = 5)
    • 2 (found 'if' at line: 362, complexity = 7)
  • 1 (found 'logical operator' at line: 362, complexity = 8)
    • 2 (found 'else' at line: 364, complexity = 10)
  • 1 (found 'if' at line: 372, complexity = 11)
    • 2 (found 'if' at line: 373, complexity = 13)
  • 1 (found 'if' at line: 379, complexity = 14)
  • 1 (found 'else' at line: 381, complexity = 15)
  • 1 (found 'if' at line: 387, complexity = 16)
  • 1 (found 'defer' at line: 390, complexity = 17)
  • 1 (found 'for' at line: 392, complexity = 18)
  • 1 (found 'for with lines >= 10' at line: 392, complexity = 19)
    • 2 (found 'if' at line: 400, complexity = 21)
  • 1 (found 'if' at line: 407, complexity = 22)
  • 1 (found 'if' at line: 413, complexity = 23)
  • 1 (found 'defer' at line: 416, complexity = 24)
  • 1 (found 'for' at line: 418, complexity = 25)
  • 1 (found 'for with lines >= 10' at line: 418, complexity = 26)
    • 2 (found 'if' at line: 427, complexity = 28)
    • 2 (found 'if' at line: 431, complexity = 30)
  • 1 (found 'if' at line: 438, complexity = 31)

litecart/internal/queries/products.go:492:1 - DeleteDigital has complexity: 16
complexity = 1

  • 1 (found 'if' at line: 495, complexity = 2)
  • 1 (found 'switch' at line: 499, complexity = 3)
  • 1 (found 'case' at line: 500, complexity = 4)
    • 2 (found 'if' at line: 503, complexity = 6)
    • 2 (found 'if' at line: 507, complexity = 8)
    • 2 (found 'range' at line: 515, complexity = 10)
      • 3 (found 'if' at line: 516, complexity = 13)
  • 1 (found 'case' at line: 521, complexity = 14)
    • 2 (found 'if' at line: 522, complexity = 16)

litecart/internal/queries/setting.go:25:1 - Settings has complexity: 21
complexity = 1

  • 1 (found 'if' at line: 32, complexity = 2)
  • 1 (found 'if' at line: 42, complexity = 3)
  • 1 (found 'defer' at line: 45, complexity = 4)
  • 1 (found 'for' at line: 68, complexity = 5)
  • 1 (found 'for with lines >= 10' at line: 68, complexity = 6)
    • 2 (found 'if' at line: 71, complexity = 8)
    • 2 (found 'if' at line: 76, complexity = 10)
      • 3 (found 'branch' at line: 77, complexity = 13)
    • 2 (found 'type switch' at line: 80, complexity = 15)
    • 2 (found 'case' at line: 81, complexity = 16)
    • 2 (found 'case' at line: 83, complexity = 17)
      • 3 (found 'if' at line: 85, complexity = 20)
  • 1 (found 'if' at line: 92, complexity = 21)

litecart/internal/queries/setting.go:100:1 - UpdateSettings has complexity: 26
complexity = 1

  • 1 (found 'defer' at line: 102, complexity = 2)
  • 1 (found 'if' at line: 105, complexity = 3)
  • 1 (found 'defer' at line: 108, complexity = 4)
  • 1 (found 'defer with lines >= 5' at line: 108, complexity = 5)
    • 2 (found 'if' at line: 109, complexity = 7)
  • 1 (found 'logical operator' at line: 109, complexity = 8)
    • 2 (found 'else' at line: 111, complexity = 10)
  • 1 (found 'if' at line: 117, complexity = 11)
  • 1 (found 'defer' at line: 120, complexity = 12)
  • 1 (found 'switch' at line: 123, complexity = 13)
  • 1 (found 'case' at line: 124, complexity = 14)
  • 1 (found 'case' at line: 133, complexity = 15)
    • 2 (found 'if' at line: 135, complexity = 17)
    • 2 (found 'if' at line: 139, complexity = 19)
  • 1 (found 'case' at line: 147, complexity = 20)
  • 1 (found 'case' at line: 152, complexity = 21)
  • 1 (found 'case' at line: 160, complexity = 22)
  • 1 (found 'case' at line: 169, complexity = 23)
  • 1 (found 'range' at line: 173, complexity = 24)
    • 2 (found 'if' at line: 174, complexity = 26)

litecart/internal/queries/setting.go:306:1 - SettingStripe has complexity: 13
complexity = 1

  • 1 (found 'if' at line: 311, complexity = 2)
  • 1 (found 'defer' at line: 314, complexity = 3)
  • 1 (found 'for' at line: 316, complexity = 4)
  • 1 (found 'for with lines >= 10' at line: 316, complexity = 5)
    • 2 (found 'if' at line: 319, complexity = 7)
    • 2 (found 'switch' at line: 323, complexity = 9)
    • 2 (found 'case' at line: 324, complexity = 10)
    • 2 (found 'case' at line: 326, complexity = 11)
    • 2 (found 'case' at line: 328, complexity = 12)
  • 1 (found 'if' at line: 333, complexity = 13)

litecart/internal/queries/setting.go:341:1 - ListSocials has complexity: 19
complexity = 1

  • 1 (found 'if' at line: 348, complexity = 2)
  • 1 (found 'defer' at line: 351, complexity = 3)
  • 1 (found 'for' at line: 353, complexity = 4)
  • 1 (found 'for with lines >= 10' at line: 353, complexity = 5)
    • 2 (found 'if' at line: 356, complexity = 7)
    • 2 (found 'if' at line: 360, complexity = 9)
    • 2 (found 'if with lines >= 10' at line: 360, complexity = 10)
      • 3 (found 'switch' at line: 361, complexity = 13)
      • 3 (found 'case' at line: 362, complexity = 14)
      • 3 (found 'case' at line: 364, complexity = 15)
      • 3 (found 'case' at line: 366, complexity = 16)
      • 3 (found 'case' at line: 368, complexity = 17)
      • 3 (found 'case' at line: 370, complexity = 18)
  • 1 (found 'if' at line: 376, complexity = 19)

litecart/internal/queries/setting.go:384:1 - SettingMail has complexity: 18
complexity = 1

  • 1 (found 'if' at line: 390, complexity = 2)
  • 1 (found 'defer' at line: 393, complexity = 3)
  • 1 (found 'for' at line: 395, complexity = 4)
  • 1 (found 'for with lines >= 10' at line: 395, complexity = 5)
    • 2 (found 'if' at line: 398, complexity = 7)
    • 2 (found 'if' at line: 402, complexity = 9)
    • 2 (found 'if with lines >= 10' at line: 402, complexity = 10)
      • 3 (found 'switch' at line: 403, complexity = 13)
      • 3 (found 'case' at line: 404, complexity = 14)
      • 3 (found 'case' at line: 406, complexity = 15)
      • 3 (found 'case' at line: 409, complexity = 16)
      • 3 (found 'case' at line: 411, complexity = 17)
      • 3 (found 'case' at line: 413, complexity = 18)

litecart/pkg/archive/tar.go:63:1 - ExtractTar has complexity: 17
complexity = 1

  • 1 (found 'if' at line: 65, complexity = 2)
  • 1 (found 'defer' at line: 68, complexity = 3)
  • 1 (found 'if' at line: 71, complexity = 4)
  • 1 (found 'defer' at line: 74, complexity = 5)
  • 1 (found 'for' at line: 77, complexity = 6)
  • 1 (found 'for with lines >= 10' at line: 77, complexity = 7)
    • 2 (found 'if' at line: 79, complexity = 9)
      • 3 (found 'if' at line: 80, complexity = 12)
    • 2 (found 'if' at line: 85, complexity = 14)
      • 3 (found 'if' at line: 88, complexity = 17)

litecart/pkg/archive/zip.go:51:1 - ExtractZip has complexity: 16
complexity = 1

  • 1 (found 'if' at line: 53, complexity = 2)
  • 1 (found 'defer' at line: 56, complexity = 3)
  • 1 (found 'if' at line: 59, complexity = 4)
  • 1 (found 'if' at line: 63, complexity = 5)
  • 1 (found 'range' at line: 67, complexity = 6)
  • 1 (found 'range with lines >= 10' at line: 67, complexity = 7)
    • 2 (found 'if' at line: 68, complexity = 9)
      • 3 (found 'branch' at line: 69, complexity = 12)
    • 2 (found 'if' at line: 73, complexity = 14)
    • 2 (found 'if' at line: 78, complexity = 16)

litecart/pkg/update/update.go:28:1 - Init has complexity: 19
complexity = 1

  • 1 (found 'defer' at line: 36, complexity = 2)
  • 1 (found 'if' at line: 39, complexity = 3)
  • 1 (found 'defer' at line: 46, complexity = 4)
  • 1 (found 'if' at line: 48, complexity = 5)
  • 1 (found 'else' at line: 52, complexity = 6)
    • 2 (found 'if' at line: 53, complexity = 8)
    • 2 (found 'if' at line: 49, complexity = 10)
  • 1 (found 'if' at line: 61, complexity = 11)
  • 1 (found 'defer' at line: 65, complexity = 12)
  • 1 (found 'if' at line: 68, complexity = 13)
    • 2 (found 'if' at line: 70, complexity = 15)
  • 1 (found 'if' at line: 75, complexity = 16)
  • 1 (found 'if' at line: 80, complexity = 17)
    • 2 (found 'if' at line: 81, complexity = 19)

54 = files
177 = functions
74 = highest complexity
4.84 = overall average complexity per function

How to Reproduce

All the code with high numbers can hide bugs because of the complex structure. These can be candidates for refactoring.

Expected Behavior

Simple code, that's easy to maintain.

litecart Version

latest on github

Code Snippet (optional)

No response

Checklist:

  • I agree to follow litecart's Code of Conduct.
  • I have checked for existing issues that describe my problem prior to opening this one.
  • I understand that improperly formatted bug reports may be closed without explanation.
@marcelloh marcelloh added the ☢️ Bug Something isn't working label Oct 27, 2023
@shurco shurco removed the ☢️ Bug Something isn't working label Oct 27, 2023
@shurco
Copy link
Owner

shurco commented Oct 27, 2023

@marcelloh Thank you for the comment.
This does not pertain to bugs.
I find some of the comments strange and misleading.

@shurco shurco changed the title 🐛 [Bug]: Cognitive Code complexity Cognitive Code complexity Oct 27, 2023
@shurco shurco closed this as completed Oct 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants