Skip to content

circular-buffer: add to track - #682

Merged
Smarticles101 merged 2 commits into
exercism:masterfrom
vivshaw:circular-buffer
Jul 8, 2017
Merged

Smarticles101 merged 2 commits into
exercism:masterfrom
vivshaw:circular-buffer

Conversation

@vivshaw

@vivshaw vivshaw commented Jul 4, 2017 •

Copy link
Copy Markdown
Contributor

test spec directory


Reviewer Resources:

Track Policies

@FridaTveit FridaTveit changed the title WIP: dibs on implementing exercise circular-buffer circular-buffer: dibs on implementing exercise Jul 4, 2017
@FridaTveit

Copy link
Copy Markdown
Contributor

Thanks for contributing @vivshaw! :D Sorry for messing with the PR name, it's just to make it consistent so that it's easy to see when someone is working on a new exercise :) Once you're ready for it to be reviewed, feel free to change it to "circular-buffer: add to track". That way it's easy for us to tell when you want a review so that we don't start leaving comments while you're still working on it :)

@vivshaw vivshaw changed the title circular-buffer: dibs on implementing exercise circular-buffer: add to track Jul 5, 2017
@vivshaw

vivshaw commented Jul 5, 2017

Copy link
Copy Markdown
Contributor Author

Alright, finished implementing all canonical test cases & the build passes.

The one big question I have is, for the exceptions that we want the CircularBuffer to throw when reading an empty buffer, should we use an existing exception type (and if so, which), or expect the user to implement a custom BufferEmptyException and BufferFullException? I went with the latter, because learning how to implement your own exceptions is useful, but I'm worried it'll make the exercise overly complex to newcomers who are inexperienced with exception handling.

@FridaTveit

Copy link
Copy Markdown
Contributor

What I've done before is make those exception classes and including them in both the example implementation and the starter implementation (see the bank account exercise for an example). That way the users get experience with custom exceptions but, like you said, the exercise doesn't get too complex :)

@vivshaw

vivshaw commented Jul 6, 2017

Copy link
Copy Markdown
Contributor Author

@FridaTveit Alright, I've just done that & updated the test suite accordingly. Let me know if anything else needs changed.

@Smarticles101 Smarticles101 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I looked at this and left a few comments that mention some minor things I saw. Other than that it looks good.

Comment thread config.json
"difficulty": 8,
"topics": [
"Queues",
"Classes",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

None of the other exercises have topics at the moment. This has come up recently in #384 and #523 thoughts @exercism/java?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it should be fine for people to add topics when they implement a new exercise if they want to, as long as they're valid topics as specified here :) What do you think @stkent?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It looks like we're going to be adding topics for every exercise soon, so I'm a +1 to what @FridaTveit suggested :)

In order to prepare your track for the new track progression model, you'll need to do the following:

  • add topics to each exercise. We have a list of topics that you can use for inspiration, but this list is not exhaustive. The topics you choose should make sense for your language.
  • add a rough guess at the difficulty of each exercise.
  • choose 10-20 exercises to mark as core
  • give the core exercises an order
  • for all other exercises, mark them as unlocked_by one of the core exercises (identified by the slug)

While we shouldn't embark on the above en masse just yet, adding topics is therefore good future-proofing!

@@ -0,0 +1,161 @@
import static junit.framework.TestCase.assertEquals;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

All the other exercises use import static org.junit.Assert.assertEquals;

import org.junit.rules.ExpectedException;

public class CircularBufferTest {
@Rule

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Usually there is a blank line after the class declaration.

@@ -0,0 +1,5 @@
public class BufferIOException extends RuntimeException {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This should extend Exception instead of RuntimeException. RuntimeException should be for errors you can't fix, generally programmer errors where the best thing to do is for the program to crash. If you want the exception to be handled then extend Exception because the compiler will force you to handle the exception. See this article for some more information about it :)

@vivshaw

vivshaw commented Jul 7, 2017

Copy link
Copy Markdown
Contributor Author

Thanks for the feedback! I've just finished implementing all the changes @Smarticles101 and @FridaTveit requested.

@FridaTveit, thanks in particular for jogging my memory on Exception vs. RuntimeException. I was having trouble getting Exception to do what I expected, until I took a look at your bank-account exercise & remembered I have to throw in all the tests too & not just the implementation!

@Smarticles101 Smarticles101 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I believe this is good now. Thanks :)

@FridaTveit FridaTveit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I just have a small question about your asserts in the test, but apart from that I agree with @Smarticles101, this is good to go :) Great work! :D

CircularBuffer<Integer> buffer = new CircularBuffer<>(1);

buffer.write(1);
assertEquals((int) buffer.read(), 1);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why do you need this cast?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Also these are the wrong way around. It's assertEquals(expected, actual). I get that wrong all the time too :P

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I thought the same thing about the cast the first time I looked at it so I ran it and found out why.

error: reference to assertEquals is ambiguous
        assertEquals(buffer.read(), 1);
        ^
  both method assertEquals(long,long) in Assert and method assertEquals(Object,Object) in Assert match

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@FridaTveit, the int cast was indeed due to the error that @Smarticles101 posted. I wrestled with that int cast for a couple hours & just now figured out a) why it does that and b) how to avoid it! It seems to be due to some wackiness with how Java autoboxing & unboxing interacts with target type inference.

The problem goes like this: Since Java Collections can't store raw types, any method we use to pull values out of our data structure will return Integer rather than int. So, my line return buffer.remove() returns an Integer. This then becomes an argument to assertEquals(), but the other argument is an int. assertEquals() is overloaded, but doesn't have any implementation with the type signature Integer, int, so it's stuck deciding between its two best guesses for the type signature: long, long and Object, Object.

So if we stick with assertEquals(), we can either: 1) remove the cast, in which case any users trying to implement the exercise with a Collection may run into this confusing bug, or 2) kludge both the arguments to the same type with a cast so they're both unboxed or both boxed.

But, assertThat() to the rescue! Turns out that asssertThat(Integer i, is(int j)) is unambiguously typed & neatly sidesteps the problem. So I redid the tests with that & we should be golden, I think.

@Smarticles101 Smarticles101 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Glad to see that you've got this figured out! I like the use of assertThat. It reads more like a sentence to me. Anyways, this is ready to merge! Thanks for the hard work @vivshaw :)

@Smarticles101
Smarticles101 merged commit ece1ba7 into exercism:master Jul 8, 2017
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.

4 participants