-
-
Notifications
You must be signed in to change notification settings - Fork 532
Remove non-idiomatic exclamation (!) suffix from method names
#1801
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ def watch_scary_movie? | |
| end | ||
|
|
||
| # Popcorn is 🍿 | ||
| def claim_free_popcorn! | ||
| def claim_free_popcorn | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This would be very surprising to have an assertion come up, but the tests require that an exception is raised, so the bang is appropriate.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, all Ruby methods can raise errors. It is not surprising, but it is exceptional!
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed. |
||
| raise 'Please implement the Moviegoer#claim_free_popcorn method' | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -34,7 +34,7 @@ def pop | |
| element | ||
| end | ||
|
|
||
| def reverse! | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because there is no reverse without, this does mean "change in place". If the tests verify that it is a mutation, then the bang should stand.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There are many methods in the Ruby standard library that mutate the receiver and do not include an exclamation mark in the name. For example, These are idiomatic because they have no counterpart method. |
||
| def reverse | ||
| previous = nil | ||
| pointer = @head | ||
| while pointer | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -103,15 +103,15 @@ def test_list_from_array_still_acts_as_lifo | |
| assert_equal 3, element.datum | ||
| end | ||
|
|
||
| def test_list_in_place_reverse! | ||
| def test_list_in_place_reverse | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the test name does not necessarily need the exclamation point. A not in place reverse method could also be tested. The bang here was only a literal reflection of the method name, without a way to fully indicate that. This could be removed. The method itself will be indicated in the actual method use in the test. |
||
| skip | ||
| first = Element.new(1) | ||
| second = Element.new(2) | ||
| third = Element.new(3) | ||
| list = SimpleLinkedList.new | ||
| list.push(first).push(second).push(third) | ||
|
|
||
| assert_equal [1, 2, 3], list.reverse!.to_a | ||
| assert_equal [1, 2, 3], list.reverse.to_a | ||
| end | ||
|
|
||
| def test_list_in_place_reverse_are_the_same_elements | ||
|
|
@@ -121,7 +121,7 @@ def test_list_in_place_reverse_are_the_same_elements | |
| list = SimpleLinkedList.new | ||
| list.push(first).push(second) | ||
|
|
||
| list.reverse! | ||
| list.reverse | ||
|
|
||
| assert_equal first, list.pop | ||
| assert_equal second, list.pop | ||
|
|
@@ -130,7 +130,7 @@ def test_list_in_place_reverse_are_the_same_elements | |
| def test_list_reverse_empty_list | ||
| skip | ||
| list = SimpleLinkedList.new | ||
| assert_equal list, list.reverse! | ||
| assert_equal list, list.reverse | ||
| end | ||
|
|
||
| def test_works_for_1_through_10 | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The bang indicates dangerous, as in may raise an exception.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All methods may raise an exception. I don't believe the bang has a definitive connection with raising errors.
Many Rails methods take this differentiation in behaviour when compared to their non-bang counterpart. But even the non-bang methods raise errors under the right circumstances. E.g. ActiveRecord's
savevssave!can both raise an exception if the Database goes down or the Ruby process runs out of memory.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, those are both exceptional behaviors, though. And exceptions are for exceptional behavior.
Contrary to some of our exercises, even. This exercise, even, as it is arguably using the exception as flow control. I am aware of this, yet the discussions happen in mentoring (at least I discuss this during mentoring).