Skip to content

Binary search #186

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

Merged
merged 9 commits into from
Dec 2, 2016
Merged

Binary search #186

merged 9 commits into from
Dec 2, 2016

Conversation

javaeeeee
Copy link
Contributor

Added the binary search exercise to the Java track.

@stkent stkent self-assigned this Nov 26, 2016
@stkent
Copy link
Contributor

stkent commented Nov 26, 2016

@javaeeeee thanks for this addition! Overall the exercise implementation looks very solid; I'll comment inline with a few comments/questions and we can discuss from there :)


dependencies {
testCompile "junit:junit:4.12"
testCompile "org.assertj:assertj-core:3.2.0"
Copy link
Contributor

Choose a reason for hiding this comment

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

In general, my preference (moving forward) would be for all new exercise implementations to rely on the default JUnit assertions. While I definitely think AssertJ assertions read better, it's another API surface that newcomers need to explore/understand to make sense of the tests we present.

@exercism/java thoughts on this? Is there somewhere we can capture standards such as the one I'm proposing for future reference? Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed, @stkent for the exact reason you gave.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hello, also agree. Actually used jUnit assertions in the test. In some other projects AssertJ is used, so It looks like a standard for the project. Removed the dependency.

Copy link
Contributor

Choose a reason for hiding this comment

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

You are right, @javaeeeee, we haven't completely removed "not technically necessary" third party dependencies. Sorry for the confusion that caused.


public class BinarySearch<T extends Comparable<T>> {

public static String ARRAY_MUST_BE_SORTED = "Array should be sorted.";
Copy link
Contributor

Choose a reason for hiding this comment

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

This field can be marked final to make it an honest-to-goodness constant :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, missed this. Static analyzers had several other complaints including Javadocs, so didn't spot it. Are Javadocs actually allowed in the project?

Copy link
Contributor

Choose a reason for hiding this comment

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

No problem! As far as I know, adding Javadocs to example implementations (when appropriate) should be just fine. Pinging @exercism/java for confirmation of that though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hello, Stuart, thank you for the answer. So the takeaway is that comments may be used only in the example implementations, not the tests. Am I right?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think comments are just fine in the tests too - as mentioned somewhere else in this PR, we have them when JUnit rules are implemented, so that users can find relevant context more quickly. However, as is generally considered good practice, we definitely don't want comments on every line of code just for the sake of it ;)

Copy link
Contributor

Choose a reason for hiding this comment

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

In general, the code should be self-explanatory will little need for comment.

If one is tempted to add a comment, ask first, "is there a way I can reword this code to make it so clear, a comment is no longer necessary?"

Comments are best used to explain unusual/surprising context... that is to say, they share why a chunk of code is shaped in the way it is.

For example, in robot-simulator, @stkent smartly overloaded Object::equals instead of overriding it and didn't implement Object::hashCode. He left a comment explaining why and how one would do it in the real world.

assertEquals(actual, SORTED_LIST);
}

@Ignore
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for ignoring all but the first test by default! Implementors thank you.

final int actual = sut.indexOf(number);
final int expected = -1;
assertEquals(expected, actual);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The binary-search problem has associated canonical test data located here.

In general, when implementing a new exercise in a track we should reference those canonical test cases (if they exist) to ensure consistency across languages. I think you have most of the expected cases covered, but for future maintainability it would be awesome if you could adjust your tests to match the canonical naming/data exactly. Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay. Thank you for the reference.

);

@Rule
public ExpectedException thrown = ExpectedException.none();
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice use of Rules! Since this is a relatively advanced concept that we've only just begun integrating into the track, we've been marking all usages with a helpful comment (for an example, see https://github.com/exercism/xjava/blob/master/exercises/queen-attack/src/test/java/QueenAttackCalculatorTest.java#L11-L14). Please add that same comment here. Once we have established a progression of exercises per #142, then we'll likely reduce this redundancy some, but better safe than sorry for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the @rule from the tests altogether as according to the canonical test data one shouldn't test if the array is sorted.

@javaeeeee
Copy link
Contributor Author

javaeeeee commented Nov 27, 2016 via email


import java.util.List;

public class BinarySearch<T extends Comparable<T>> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just noticed that this generic type means the example implementation is a lot more general than the tests require. @exercism/java is this a good feature in a reference solution, or does it veer towards YAGNI territory?

Copy link
Contributor

Choose a reason for hiding this comment

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

Personally, I currently give wide berth to implementors in what happens in the example. As long as it passes the tests and represents a true solution, it's generally been implementor's choice.

I think this way because practitioners don't see these. The high value conversations in PRs are about how the test suite flows, the API decisions therein and whether we're implementing the exercise correctly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hello, and thank you for your comments. Here is a question: Do we need .keep files? Spotted that some were removed in one of the commits.

Copy link
Contributor

Choose a reason for hiding this comment

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

Since there are other files in exercises/binary-search/src/main/java/ and exercises/binary-search/src/test/java/, it should be safe to remove exercises/binary-search/src/main/java/.keep and exercises/binary-search/src/test/java/.keep. Thanks for checking!

@stkent
Copy link
Contributor

stkent commented Nov 29, 2016

One more merge with master needed!

@javaeeeee
Copy link
Contributor Author

Merged!

@stkent
Copy link
Contributor

stkent commented Dec 1, 2016

Sorry for the silence yesterday; I should be able to take another look at this tonight :o)

@javaeeeee
Copy link
Contributor Author

It's Okay. Thank you. There is no hurry.

@stkent
Copy link
Contributor

stkent commented Dec 2, 2016

Looks good! One more merge, then I'll hit the button as fast as possible so we don't need any more :)

@jtigger
Copy link
Contributor

jtigger commented Dec 2, 2016

Take your time; I'll make sure not to merge until this one goes through (assuming no corrective feedback).

@javaeeeee
Copy link
Contributor Author

javaeeeee commented Dec 2, 2016 via email

@stkent
Copy link
Contributor

stkent commented Dec 2, 2016

@jtigger hah, yes, I wouldn't really race, but thank you for holding off on others. (I haven't dealt with my own merge conflict for that same reason :)

@stkent stkent merged commit a47d37e into exercism:master Dec 2, 2016
@javaeeeee
Copy link
Contributor Author

javaeeeee commented Dec 2, 2016 via email

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.

3 participants