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

Ignore idiomatic error checking #40

Open
MohammadBnei opened this issue Dec 12, 2024 · 3 comments
Open

Ignore idiomatic error checking #40

MohammadBnei opened this issue Dec 12, 2024 · 3 comments

Comments

@MohammadBnei
Copy link

Nice project !

Since golang has the if err !=nil { return err } way of handling errors, it may be a nice touch to not increment the complexity when using the idiomatic golang error checking implementation.

This is just a proposition.

@uudashr
Copy link
Owner

uudashr commented Dec 17, 2024

Hi @MohammadBnei, thanks in advance.

Gocognit calculates complexity based on the paper, I'm not trying to bring new way to calculate cognitive complexity.
Not every language is the same, for example Go language has several feature that not defined on the paper, so that we might consider the complexity calculation for it, such as select statement.

I do understand Go use "err checking and return" instead of throw exception. Which means it easy for Go to have complexity higher compare to language that has exception handling. But again, the cognitive load of our brains is the same like we read another if statement, it is the same if, nothing special.

Sometimes inside the if body it has logging inside, or do some action before return the err. Sometimes is not a simple return error, but it need to return another value as well. There is nothing special, and it might still require the same cognitive load inside our brain.

I very appreciate. I promise nothing, but let me consider this.

I'm open if you want to have a discussion on this. Or PoC/PR would be very welcome.

@getvictor
Copy link

getvictor commented Feb 27, 2025

+1 the proposal. I would like to see a switch to ignore if err != nil { statements. (Or customize the penalty for this code.)

Cognitive complexity penalizes for nested if statements, and if those nested if statements contain idiomatic error checking, the penalty is increased.

I'm seeing the cognitive complexity is 2X higher than cyclomatic complexity for some of my functions, but I'm not feeling that when looking at code in my IDE.

The other relevant point is that a good IDE will hide/collapse the idiomatic error checking code -- this makes code more readable and less cognitively complex.

@uudashr
Copy link
Owner

uudashr commented Feb 28, 2025

I'm seeing the cognitive complexity is 2X higher than cyclomatic complexity for some of my functions, but I'm not feeling that when looking at code in my IDE.

If this is the case then no cognitive load for the if err != nil {}

The other relevant point is that a good IDE will hide/collapse the idiomatic error checking code -- this makes code more readable and less cognitively complex.

If the IDE automatically hide this, then (it can be) true there is no cognitive load. Which IDE doing this?

Btw, I'm open for the proposal and contribution for this. We can make it by default disabled and enable it by consent

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

3 participants