Skip to content

proverb: add to track - #1077

Merged
sjwarner-bp merged 17 commits into
exercism:masterfrom
rdavid1099:add-proverb-exercise
Dec 20, 2017
Merged

sjwarner-bp merged 17 commits into
exercism:masterfrom
rdavid1099:add-proverb-exercise

Conversation

@rdavid1099

@rdavid1099 rdavid1099 commented Dec 6, 2017 •

Copy link
Copy Markdown
Contributor

Add proverb exercise to Java track.

Used the Ruby version as inspiration.

Test suite covers base functionality covered in the canonical data.


Reviewer Resources:

Track Policies

@sjwarner-bp sjwarner-bp 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 think that this looks really good so far - I can only see one minor issue.

This is just to limit scope as much as possible. I've left an annotation on an example of where scope could be limited, though it is in a few places in this submission.

Let me know what you think 🙂

this.words = words;
}

public void setQualifier(String qualifier) {

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 an example of where the package-private access level could be used - we try to limit scope.

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.

Thanks for the feedback. I really appreciate it. I'm relatively new to Java and am looking at the docs right now. When you say limiting scope by using package-private access level, are you referring to removing the public ahead of the method names? For example public void setQualifier(......) would simply become void setQualifier(.....). This means that the method (or whatever member) can only be accessed within its own package. If that's correct, I'll go ahead and push up those changes.

@sjwarner-bp sjwarner-bp Dec 11, 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.

That's exactly right - I think this is a good change, as nothing outside of the package requires access to those files, and generally it is best practise to limit scope 🙂

This is in a couple of places across the submission - however, classes and methods in the test file are normally left as public!

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. Thank you so much. Exercism.io is one of the greatest resources when learning a new language.

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

Thanks for implementing this @rdavid1099, it's looking good :)

I've added some comments, mainly to do with how your implementation diverges a bit from how the readme and the canonical data define the exercise. We try to stick to the readme and canonical data as closely as possible so that the exercises are relatively consistent across the track. I hope that makes sense :)

Feel free to ask questions or disagree with my comments! :)

Comment thread config.json Outdated
"loops",
"strings"
],
"unlocked_by": null,

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 be unlocked by a core exercise. We only have a few very easy exercises that aren't unlocked by anything. hello-world or two-fer might be sensible suggestions for this to be unlocked by since they also deal with strings and are easier than this one. But feel free to look through the core exercises to find a different one for this to be unlocked by if you want :)

@@ -0,0 +1,27 @@
final class Proverb {
private String[] words;
private String initWord, qualifier, result = "";

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.

These should all be local variables (apart from words) in the getProverb method since they're only used in that method :)

@@ -0,0 +1,27 @@
final class Proverb {

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 class doesn't really seem to follow the canonical-data. It should just have one method, recite, which when given a list of words (you can give the list of words in the constructor if you want) outputs the proverb. It should not have, and should not need, a setQualifier method. Does that make sense? :)

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.

Just to clatify, you can name the method something else if you like, for example getProverb, but I think it's very important that it's only one method without any setQualifier method :)

private String[] words;
private String initWord, qualifier, result = "";

Proverb(String... words) {

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 use String[]. Using String... adds some unnecessary complixity to the exercise and I don't think it's really needed :)

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

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 class needs to have a signature that matches what is expected in the tests, so it should have the signature for all the methods that are used in the tests and a constructor which matches the one used in the test (or no constructor if the one used in the tests takes no argument, since a constructor with no arguments is provided by default in Java) :)

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

public class ProverbTest {

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.

These tests should exactly match the canonical data which I don't think they do at the moment :)

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.

I was taking a look at the canonical data, and I copied the tests I'm using from the Ruby exercise which includes the qualifier and diverts a bit from the canonical data. To be fair, it also looks like its the third version of the Proverb exercise for Ruby. Since this is the first version of this exercise for Java, I will only include the canonical tests and leave the qualifier and such for future iterations.

@rdavid1099

Copy link
Copy Markdown
Contributor Author

Thanks for all of the constructive input @FridaTveit! I really appreciate all of the attention to detail you guys put into these exercises. I will work on these fixes later today, and following this, I hope to add more exercises to other tracks. 😄

@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! :) I've added a couple of small comments but apart from that this looks ready to merge! :)


String recite() {
if (words.length < 1) return "";
String initWord = "",

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.

Could this just be set to words[0] instead of setting it to an empty string and then immediately checking if it's empty?

String recite() {
if (words.length < 1) return "";
String initWord = "",
result = "";

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 use a StringBuilder instead of a string for result. A StringBuilder is more efficient when you keep appending to it in a loop :)

} else {
result += "the " + word + " was lost.\n";
}
if (i + 1 != words.length) result += "For want of a " + word + " ";

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 have curly brackets around it, i.e.

if (i + 1 != words.length) {
  result += "For want of a " + word + " ";
}

It's considered best practice to always put curly brackets around if statements, even when they're only one line, to prevent errors if someone tried to add another line to it. I admit errors are less likely when the body of the if is on the same line as the if statement, but I still think it's worth using curly brackets just in case :)

@@ -0,0 +1,12 @@
class Proverb {
private String[] words;

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 this in the starter implementation. You only need enough so that the tests will compile :)

private String[] words;

Proverb(String[] words) {
this.words = words;

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 be replaced with throw new UnsupportedOperationException("Delete this statement and write your own implementation.");. We only want to provide the users with the signatures for the exercise, not any of the implementation :)

@rdavid1099

Copy link
Copy Markdown
Contributor Author

@FridaTveit Thanks again for all the feedback. Regarding the if-statement syntax, I'm used to Ruby syntax and single-line if statements, so I really appreciate the insight on best Java practices.

I applied all of the fixes and refactored a bit to cut down on all the if-statements. If there is anything else, please feel free to tell me 😃

@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! I've added one comment but it's a very minor thing and apart from that I think this is ready to merge! Great work! :)

}

String recite() {
if (words.length < 1) return "";

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.

Please put curly brackets around the body of this if :)

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.

Awesome. Thank you so much. I wasn't sure about that if-statement since it was extremely simple, but I do agree it's much more readable and debuggable with curly braces.

@sjwarner-bp

Copy link
Copy Markdown
Contributor

This looks great - once the CI has passed I am happy to merge this in 😄

@sjwarner-bp
sjwarner-bp merged commit c88d6e2 into exercism:master Dec 20, 2017
@sjwarner-bp

Copy link
Copy Markdown
Contributor

Thanks @rdavid1099 - that's a great exercise to have implemented now 🙂

@rdavid1099
rdavid1099 deleted the add-proverb-exercise branch December 20, 2017 18:53
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