forked from sandimetz/99bottles_ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
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
Incorporate minimum value into 99 Bottles verse #2
Open
trishrempel
wants to merge
20
commits into
main
Choose a base branch
from
get-more-at-min
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Introduce the requirement that when
CountdownSong
specifies amin:
parameter, the finalBottleVerse
should:Go to the store and buy some more
foraction
quantity
andcontainer
typically return for the minimum bottle numbermax
bottle number forsuccessor
For example, the last verse of this song:
Should be:
Instead of:
The commits in this PR introduce an integration test with the new requirement, and then make the incremental changes necessary to make the test pass, according to the principles described in 99 Bottles of OOP by Sandi Metz.
First round of changes (till this commit)
min:
parameter toBottleNumber.for
andBottleVerse
BottleNumberMin
subclass ofBottleNumber
. It uses theBottleNumber.for
factory to construct aBottleNumber
with amin:
value ofnil
and uses it as a proxy forquantity
andcontainer
BottleNumber.for
to return early with aBottleNumberMin
instance ifnumber
equalsmin:
CountdownSong#verse
to pass amin:
value toverse_template.lyrics
#action
and#successor
fromBottleNumber0
Problem
I wasn't fond of this line in
BottleNumberMin#new
:Here, it uses the
BottleNumber.for
factory to construct aBottleNumber
fornumber
so that it can be used as a proxy forquantity
andcontainer
. In order to bypass constructing aBottleNumberMin
once again, it explicitly passesmin:
asnil
.Doing this presumes an intimate knowledge of the inner workings of
BottleNumber.for
and introduces tight coupling and fragility to the code, according to the principles taught in 99 Bottles of OOP.Second round of changes (from this commit till this commit)
I wanted to find a way to make
BottleNumberMin
less responsible and knowledgeable of theBottleNumber.for
factory. To do this, I needed to shift the responsibility toBottleNumber.for
.BottleNumberMin#new
to accept abottle_number
parameter, to act as the proxy for#quantity
and#container
BottleNumber.for
to first construct aBottleNumber
for thenumber
parameter. Ifnumber
is equal tomin
, return a newBottleNumberMin
, passing the usual properties and theBottleNumber
. Otherwise, return theBottleNumber
.Follow the commits to see how I made the changes incrementally while keeping tests green.
Problem
BottleNumber.for
now has a newline between two blocks of code, which usually represents a method doing too many things. The first block constructs aBottleNumber
according tonumber
. The second block decides whether to construct aBottleNumberMin
with the already createdBottleNumber
as a parameter, or to return the existingBottleNumber
.To me, these are still clearly the responsibility of the
BottleNumber
factory. It might be possible to move these two blocks to their own private methods. However, I don't think this would significantly improve clarity and readability, and becauseBottleNumber.for
is a class method, I think introducing more class methods that are private would be even more clunky and confusing.