Incorporate minimum value into 99 Bottles verse#2
Open
trishrempel wants to merge 20 commits intomainfrom
Open
Conversation
This file contains hidden or 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
CountdownSongspecifies amin:parameter, the finalBottleVerseshould:Go to the store and buy some moreforactionquantityandcontainertypically return for the minimum bottle numbermaxbottle number forsuccessorFor 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.forandBottleVerseBottleNumberMinsubclass ofBottleNumber. It uses theBottleNumber.forfactory to construct aBottleNumberwith amin:value ofniland uses it as a proxy forquantityandcontainerBottleNumber.forto return early with aBottleNumberMininstance ifnumberequalsmin:CountdownSong#verseto pass amin:value toverse_template.lyrics#actionand#successorfromBottleNumber0Problem
I wasn't fond of this line in
BottleNumberMin#new:Here, it uses the
BottleNumber.forfactory to construct aBottleNumberfornumberso that it can be used as a proxy forquantityandcontainer. In order to bypass constructing aBottleNumberMinonce again, it explicitly passesmin:asnil.Doing this presumes an intimate knowledge of the inner workings of
BottleNumber.forand 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
BottleNumberMinless responsible and knowledgeable of theBottleNumber.forfactory. To do this, I needed to shift the responsibility toBottleNumber.for.BottleNumberMin#newto accept abottle_numberparameter, to act as the proxy for#quantityand#containerBottleNumber.forto first construct aBottleNumberfor thenumberparameter. Ifnumberis 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.fornow has a newline between two blocks of code, which usually represents a method doing too many things. The first block constructs aBottleNumberaccording tonumber. The second block decides whether to construct aBottleNumberMinwith the already createdBottleNumberas a parameter, or to return the existingBottleNumber.To me, these are still clearly the responsibility of the
BottleNumberfactory. 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.foris a class method, I think introducing more class methods that are private would be even more clunky and confusing.