Repository navigation
proverb: add to track - #1077
Conversation
sjwarner-bp
left a comment
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
This is an example of where the package-private access level could be used - we try to limit scope.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
Perfect. Thank you so much. Exercism.io is one of the greatest resources when learning a new language.
FridaTveit
left a comment
There was a problem hiding this comment.
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! :)
| "loops", | ||
| "strings" | ||
| ], | ||
| "unlocked_by": null, |
There was a problem hiding this comment.
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 = ""; | |||
There was a problem hiding this comment.
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 { | |||
There was a problem hiding this comment.
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? :)
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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 { | |||
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
These tests should exactly match the canonical data which I don't think they do at the moment :)
There was a problem hiding this comment.
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.
|
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
left a comment
There was a problem hiding this comment.
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 = "", |
There was a problem hiding this comment.
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 = ""; |
There was a problem hiding this comment.
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 + " "; |
There was a problem hiding this comment.
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; | |||
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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 :)
|
@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
left a comment
There was a problem hiding this comment.
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 ""; |
There was a problem hiding this comment.
Please put curly brackets around the body of this if :)
There was a problem hiding this comment.
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.
|
This looks great - once the CI has passed I am happy to merge this in 😄 |
|
Thanks @rdavid1099 - that's a great exercise to have implemented now 🙂 |
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