The problem is, it failed for the wrong reason. We want to see a failure because
the books are not sorted. Let's take a look at the Bookshelf class.
Well, that explains our "NotImplementedException".
So, what is it going to take to get this test to fail for the right reason? Let's
take a look at
Uncle Bob's Transformation Priority Premise. The first transformation we should
try is "nil". Can we get the test to fail by doing nothing? No, our code will not
compile unless we return something. The next step is a constant. Let's just return
the books list as is.
1 test failed Expected: Partnoy, Frank. Wait: The Art and Science of Delay But was:
Watt, Andrew. Beginning Regular Expressions
In Linq OrderBy we overrode the Book.ToString()
to get the author and title.
We saw a failing test (Red), and we made sure the message was helpful. Now we can
make it green.
Let's translate this into C#. Instead of a, i, k, and n, I'm going to give the variables
Let me explain why this code differs from the algorithm.
The first book's index is zero. We will be comparing a book to all of the other
books on the shelf. We keep looping through the books as long as bookIndex is less than
the number of books. We add 1 to bookIndex to keep moving through the list.
We set a variable selectedBookIndex to bookIndex so that we can see if we found a book that precedes the
currently selected book.
Our inner loop starts with the book that is next to the one defined in the outer
loop. If you are looking at the book shelf. You would grab the second book and compare
it to the first book. If the book you are holding precedes the book that is next
to it, update selectedBookIndex. I reversed the > symbol in my loop from the one defined in the
algorithm because I want the books in ascending order.
After looking at each book, we swap books by taking the book off the shelf and putting
it in a swapBook variable. Then we select the book that precedes it and put it in
the book's spot. Then we put the book into that empty slot.
When we run the test we see:
1 test passing
I recommend setting a breakpoint and watching the list of books change order.
Please contact me about this Kata. I would
love to get your feedback.