opencost icon indicating copy to clipboard operation
opencost copied to clipboard

Remove redundant `len` and `nil` check before loop

Open Juneezee opened this issue 1 year ago • 8 comments

What does this PR change?

From the Go specification (https://go.dev/ref/spec#For_range):

"1. For a nil slice, the number of iterations is 0." "3. If the map is nil, the number of iterations is 0."

len returns 0 if the slice or map is nil (https://pkg.go.dev/builtin#len). Therefore, having these checks before a loop is unnecessary:

  • len(v) != 0
  • len(v) > 0
  • v != nil

Does this PR relate to any other PRs?

No.

How will this PR impact users?

No user-facing change.

Does this PR address any GitHub or Zendesk issues?

How was this PR tested?

go build ./... and go test ./...

Does this PR require changes to documentation?

No.

Have you labeled this PR and its corresponding Issue as "next release" if it should be part of the next OpenCost release? If not, why not?

No. External contributor cannot label PR.

Juneezee avatar Sep 28 '23 16:09 Juneezee

Thank you for this PR. I suspect these hooks were left in the code to quickly add an

else { // log error }

since many of these seem to imply exceptional cases except for this https://github.com/opencost/opencost/pull/2184/files#diff-084f229ce009968d50ab3e5cbeb8ff6f42eb0fd08fdc7b9d027f6931d47ea483R1765

AjayTripathy avatar Oct 04 '23 00:10 AjayTripathy

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
opencost ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 7, 2024 2:15am

vercel[bot] avatar Nov 02 '23 10:11 vercel[bot]

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
No Duplication information No Duplication information

sonarqubecloud[bot] avatar Nov 02 '23 20:11 sonarqubecloud[bot]

@cliffcolvin Friendly ping :smiley:

Juneezee avatar Jan 29 '24 14:01 Juneezee

Hey @Juneezee I think we'd rather log something about these values being nil in an else clause, as I mentioned earlier, than drop the if altogether since these cases feel exceptional.

AjayTripathy avatar Feb 06 '24 00:02 AjayTripathy

This pull request has been marked as stale because it has been open for 90 days with no activity. Please remove the stale label or comment or this pull request will be closed in 5 days.

github-actions[bot] avatar May 07 '24 01:05 github-actions[bot]

@AjayTripathy do you want to take a quick look?

mattray avatar May 10 '24 08:05 mattray

This pull request has been marked as stale because it has been open for 90 days with no activity. Please remove the stale label or comment or this pull request will be closed in 5 days.

github-actions[bot] avatar Aug 09 '24 01:08 github-actions[bot]

This pull request was closed because it has been inactive for 95 days with no activity.

github-actions[bot] avatar Aug 14 '24 01:08 github-actions[bot]