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

Add a rationale for the "first and last" rule #929

Merged
merged 1 commit into from Nov 22, 2023

Conversation

ydakuka
Copy link
Contributor

@ydakuka ydakuka commented Nov 4, 2023

I understand the benefits of this rule when using ActiveRecord (for example), but I don't see any reason why I should do this when using pure Ruby.

@bbatsov
Copy link
Collaborator

bbatsov commented Nov 4, 2023

It's just easier to read (e.g. not every language has the concept of negative indices), but I guess we failed to explain this. :-) I think it'd be better to add some rationale instead.

@ydakuka ydakuka changed the title Remove "first and last" rule Add a rationale for the "first and last" rule Nov 22, 2023
@ydakuka
Copy link
Contributor Author

ydakuka commented Nov 22, 2023

Ok, I've done it.

@@ -4240,6 +4240,7 @@ arr[100] = 1 # now you have an array with lots of nils
=== `first` and `last` [[first-and-last]]

When accessing the first or last element from an array, prefer `first` or `last` over `[0]` or `[-1]`.
`first` and `last` take less effort to understand, especially for a less experienced Ruby programmer or someone from a language with different indexing semantics.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Might be nice to add a couple of examples here as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added

@bbatsov bbatsov merged commit 378e9d0 into rubocop:master Nov 22, 2023
3 checks passed
@ydakuka ydakuka deleted the remove-first-and-last-rule branch November 22, 2023 21: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.

None yet

2 participants