Skip to content

isbn-verifier: implement exercise in Java track - #1004

Merged
FridaTveit merged 5 commits into
exercism:masterfrom
sjwarner:isbn-impl
Nov 17, 2017
Merged

FridaTveit merged 5 commits into
exercism:masterfrom
sjwarner:isbn-impl

Conversation

@sjwarner-bp

Copy link
Copy Markdown
Contributor

Implementation of isbn-verifier, as per problem-specifications.


Reviewer Resources:

Track Policies

@sjwarner-bp

sjwarner-bp commented Nov 16, 2017 •

Copy link
Copy Markdown
Contributor Author

Hmm, not too sure why the build failed - if anyone knows please tell me 🙂 The log says about settings.gradle, though I can't quite understand what it is trying to tell me!

Also, what I have noticed in implementing isbn-verifier is that the description in the canonical data (and therefore, the test names) are quite vague and inconsistent. I'm not sure if this is just me, or if it is an actual problem, in which case I'll open an issue - again, please let me know what you think!

@stkent

stkent commented Nov 16, 2017

Copy link
Copy Markdown
Contributor
+echo ==================================================
==================================================
+echo 'Solving settings.gradle'
Solving settings.gradle
+echo ==================================================
==================================================
+./exercism --config .journey-test.exercism.json fetch java settings.gradle
127.0.0.1 - - [16/Nov/2017:13:13:15 +0000] "GET /v2/exercises/java/settings.gradle HTTP/1.1" 200 339 0.0066
Not Submitted:         1 problem
java (Settings.gradle) /home/travis/build/exercism/java/build/exercism/java/settings.gradle
New:                   1 problem
java (Settings.gradle) /home/travis/build/exercism/java/build/exercism/java/settings.gradle
unchanged: 0, updated: 0, new: 1
+cp -R -H '/home/travis/build/exercism/java/exercises/settings.gradle/.meta/src/reference/java/*' /home/travis/build/exercism/java/build/exercism/java/settings.gradle/src/main/java/
cp: cannot stat ‘/home/travis/build/exercism/java/exercises/settings.gradle/.meta/src/reference/java/*’: Not a directory

This seems... weird. I can take a look this evening!

r.e. the descriptions: I'm not usually strict about mapping canonical data descriptions to test names, but improving the canonical data descriptions is always worthwhile! I'd recommend opening an issue for discussion in https://github.com/exercism/problem-specifications to see what others think :)

@FridaTveit

Copy link
Copy Markdown
Contributor

I think I've seen it give that error before. That time I restarted the build and it was fine. So I've tried restarting the build now to see if that works again :) But it would be good to know why it happened either way...

@stkent

stkent commented Nov 16, 2017

Copy link
Copy Markdown
Contributor

Spooky 😱

@sjwarner-bp

Copy link
Copy Markdown
Contributor Author

🎉 Hooray! Very odd. Just looking back at my implementation, I think I have misunderstood the exercise, so I might tidy it up before it gets merged, if that is ok?

@FridaTveit

Copy link
Copy Markdown
Contributor

Sure, we haven't reviewed it yet so that's not a problem. Just add a comment here when you think it's ready for review :) (you could also put "don't merge" or "not ready for review" in the title if you want)

@sjwarner-bp sjwarner-bp changed the title isbn-verifier: implement exercise in Java track isbn-verifier: implement exercise in Java track [Do not merge] Nov 16, 2017
@sjwarner-bp

Copy link
Copy Markdown
Contributor Author

Ok, I think that this is now all ready from my point of view 🙂

@sjwarner-bp sjwarner-bp changed the title isbn-verifier: implement exercise in Java track [Do not merge] isbn-verifier: implement exercise in Java track Nov 16, 2017
@FridaTveit

Copy link
Copy Markdown
Contributor

Great, I'll try to find time to review it later today :) One thing I can quickly see is that since it has difficulty 4 it should have starter implementation (see the POLICIES doc) and I don't think you've added any?

@sjwarner-bp

Copy link
Copy Markdown
Contributor Author

Oops! I had it locally as a 5 before dialling it back to 4 - change now made, thanks for the reminder 🙂

@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.

This looks great @sjwarner-bp! :)

I've made a few comments but they're mostly minor things. Feel free to disagree with me or ask questions :)

Comment thread config.json
"topics": [
"integers",
"loops",
"strings"

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.

@stkent I saw you recently did a PR to corrent the lengths of uuids. How long do they have to be? Is that documented anywhere? I can't see it in the exercise configuration document...

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.

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx is the correct pattern; the UUID used here is too long:

xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
0c5fcd83-01a5-3880-2784-277ff6cba7b5e51c3b0

If using configlet to generate UUIDs, note that configlet generated invalid UUIDs up until v3.6.0; as of v3.6.1 it generates correct ones.

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.

Perfect, I've updated my configlet


private String isbn;

boolean isValid(String s) {

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.

Single letter variable names are generally bad practice (with some exceptions like indices or coordinates) because they don't convey much information about what the variable is. What about stringToVerify? :)


boolean isValid(String s) {

this.isbn = s.replace("-", "");

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 don't think this should be a class variable since it's only used in this method :)


if (isbn.length() != 10) {
return false;
} else {

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.

You don't need the else here since you return in the if block :)


@Test
public void validIsbnNumber() {
Assert.assertEquals(true, isbnVerifier.isValid("3-598-21508-8"));

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.

You can use assertTrue and assertFalse instead of assertEquals, that will give better error messages when the tests fails :)

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 still needs to be changed to assertTrue :) And you could do a static import so that you don't have to do Assert.assertTrue:

import static org.junit.Assert.assertTrue;

assertTrue(something, somethingElse)

:)

}
}

if (Character.isDigit(isbn.charAt(9))) {

@FridaTveit FridaTveit Nov 16, 2017 •

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 would absorb this into the previous loop (just check if i == isbn.length - 1 && isbn.charAt(i) == 'X'). But it doesn't matter too much, it's pretty clear either way :) So if you want to keep it separate then that's fine :)

I would use isbn.length() - 1 instead of 9 though, to keep it consistent with the other loop and to prevent the use of "magic" numbers that can confuse anyone reading the code :)

return false;
}

if (total % 11 == 0) {

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 can be simplified to return total % 11 == 0 :)

return false;
} else {
for (int i = 0; i < isbn.length() - 1; i++) {
if(Character.isDigit(isbn.charAt(i))) {

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 would extract isbn.charAt(i) into a local variable since you're using it twice :)

@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.

This is looking great @sjwarner-bp! Thanks for making those changes so quickly :) I've left a few more minor comments but it's very nearly ready to merge! :)

boolean isValid(String stringToVerify) {

String isbn;
isbn = stringToVerify.replace("-", "");

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.

You could do this on one line:

String isbn = stringToVerify.replace("-", "");

}

for (int i = 0; i < isbn.length() - 1; i++) {
char currentDigit = isbn.charAt(i);

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.

Maybe currentChar? Either that or use currentDigitVal and finalDigit later to be consistent :)

@@ -0,0 +1,7 @@
class IsbnVerifier {

boolean isValid(String s) {

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 one should also be stringToVerify :)


@Test
public void validIsbnNumber() {
Assert.assertEquals(true, isbnVerifier.isValid("3-598-21508-8"));

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 still needs to be changed to assertTrue :) And you could do a static import so that you don't have to do Assert.assertTrue:

import static org.junit.Assert.assertTrue;

assertTrue(something, somethingElse)

:)

@sjwarner-bp

Copy link
Copy Markdown
Contributor Author
+echo ==================================================
==================================================
+echo 'Solving settings.gradle'
Solving settings.gradle
+echo ==================================================
==================================================
+./exercism --config .journey-test.exercism.json fetch java settings.gradle
127.0.0.1 - - [17/Nov/2017:08:57:41 +0000] "GET /v2/exercises/java/settings.gradle HTTP/1.1" 200 339 0.0053
Not Submitted:         1 problem
java (Settings.gradle) /home/travis/build/exercism/java/build/exercism/java/settings.gradle
New:                   1 problem
java (Settings.gradle) /home/travis/build/exercism/java/build/exercism/java/settings.gradle
unchanged: 0, updated: 0, new: 1
+cp -R -H '/home/travis/build/exercism/java/exercises/settings.gradle/.meta/src/reference/java/*' /home/travis/build/exercism/java/build/exercism/java/settings.gradle/src/main/java/
cp: cannot stat ‘/home/travis/build/exercism/java/exercises/settings.gradle/.meta/src/reference/java/*’: Not a directory

It happened again! 😱

@FridaTveit

Copy link
Copy Markdown
Contributor

Strange... I'll restart it :)

@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.

This looks great! Thanks for adding this exercise @sjwarner-bp, it's a great addition to the track :)

@FridaTveit
FridaTveit merged commit 9791d82 into exercism:master Nov 17, 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