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

Remove gomnd from Golang CI #3330

Merged
merged 1 commit into from
Jan 8, 2025
Merged

Remove gomnd from Golang CI #3330

merged 1 commit into from
Jan 8, 2025

Conversation

timraymond
Copy link
Member

Most developers agree that magic numbers are problematic. However, the gomnd linter lacks finesse to understand when magic numbers aren't magic at all. Examining the context of where the number is used is often enough to deem it non-magical.

Consider some common magic numbers flagged by gomnd: 0644, 8080. Without any additional context, it's fairly likely these are a file mode and a port number, respectively. It's even more obvious when presented with the context:

f, err := os.OpenFile("notes.txt", os.O_RDWR|os.O_CREATE, 0644)

The linter would flag this, even though it's completely obvious what 0644 is. The remediations for the linter's flag creates categorically worse code. The most common learned behavior for developers facing this linter is to simply constantize everything it flags:

flags := 0644
f, err := os.OpenFile("notes.txt", os.O_RDWR|os.O_CREATE, flags)

This makes the code harder to read, since you now have to trace the reference back (it's probably defined in some const block another file away).

Option number two for remediating this is to drop a comment providing literally zero information to the reader that they don't already have or could get from the documentation of the function where it's used.

//nolint:gomnd // 0644 is a file mode
f, err := os.OpenFile("notes.txt", os.O_RDWR|os.O_CREATE, 0644)

This is not to say that magic numbers are not a problem--they definitely are. However, this tool has very low signal-to-noise ratio and causes people to take action that makes the codebase worse just to silence it. Reviewers understand when a magic number is confusing and can just flag it by hand more reliably.

Most developers agree that magic numbers are problematic. However, the
gomnd linter lacks finesse to understand when magic numbers aren't magic
at all. Examining the context of where the number is used is often
enough to deem it non-magical.

Consider some common magic numbers flagged by gomnd: 0644, 8080. Without
any additional context, it's fairly likely these are a file mode and a
port number, respectively. It's even more obvious when presented with
the context:

```go
f, err := os.OpenFile("notes.txt", os.O_RDWR|os.O_CREATE, 0644)
```

The linter would flag this, even though it's completely obvious what
0644 is. The remediations for the linter's flag creates categorically
worse code. The most common learned behavior for developers facing this
linter is to simply constantize everything it flags:

```go
flags := 0644
f, err := os.OpenFile("notes.txt", os.O_RDWR|os.O_CREATE, flags)
```

This makes the code *harder* to read, since you now have to trace the
reference back (it's probably defined in some const block another file
away).

Option number two for remediating this is to drop a comment providing
literally zero information to the reader that they don't already have or
could get from the documentation of the function where it's used.

```go
//nolint:gomnd // 0644 is a file mode
f, err := os.OpenFile("notes.txt", os.O_RDWR|os.O_CREATE, 0644)
```

This is not to say that magic numbers are not a problem--they definitely
are. However, this tool has very low signal-to-noise ratio and causes
people to take action that makes the codebase worse just to silence it.
Reviewers understand when a magic number is confusing and can just flag
it by hand more reliably.
Copy link
Contributor

@rbtr rbtr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately i agree. wish it was smarter 🥲

@rbtr
Copy link
Contributor

rbtr commented Jan 7, 2025

/azp run Azure Container Networking PR

@rbtr rbtr enabled auto-merge January 7, 2025 19:06
Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@rbtr rbtr added this pull request to the merge queue Jan 7, 2025
Merged via the queue into master with commit 693642f Jan 8, 2025
14 checks passed
@rbtr rbtr deleted the traymond/remove-gomnd branch January 8, 2025 02:55
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

Successfully merging this pull request may close these issues.

2 participants