Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialpoltexious
Courses Plus Student 6,993 PointsWhy did Craig completely remove ... + "\" - @" + ... from the refactoring?
I was wondering why did Craig remove this from line 18 at time 11.01 in the video:
... + "\" - @" + ...
I understand the reasoning behind the refactoring, but not why he removed that part of the code, and he did not even mention what happened to that part.
He went from this:
@Override
public String toString() {
return "Tweet: \"" +
mDescription +
"\" - @" +
mAuthor +
" on " +
mCreationDate;
}
To this:
@Override
public String toString() {
return String.format("Treet: \"%s\" by %s on %s", mDescription, mAuthor, mCreationDate);
}
3 Answers
Steve Hunter
57,712 PointsHi,
Craig switched from adding strings together and using escape characters for the quotation marks to using string interpolation. With that, the %s
within the string is replaced with the paramter passed in after the string has ended. In this instance there were three strings to interpolate, so there's three %s
inside the string, whcih get replaced by the values stored in the three variables that follow the comma in the order they are written.
I hope that makes sense!
Steve.
Craig Dennis
Treehouse TeacherYeah I just derped it up ;) I didn't meant to remove the @ for by. It could totally be exactly the same using the string formatter. Sorry for the confusion, and thanks for the attention to detail!
poltexious
Courses Plus Student 6,993 PointsHi Steve,
Thanks for the answer. However, it really doesn't answer my question. Also, I did not ask why or how he did the refactoring as I completely understand this.
He could simply have done this:
@Override
public String toString() {
return String.format("Treet: \"%s\" - @%s on %s", mDescription, mAuthor, mCreationDate);
}
instead of:
@Override
public String toString() {
return String.format("Treet: \"%s\" by %s on %s", mDescription, mAuthor, mCreationDate);
}
Then I would not be confused.
But I think I understand it now. He made a small change without telling about it explicitly, and that was why I got confused.
poltexious
Courses Plus Student 6,993 Pointspoltexious
Courses Plus Student 6,993 PointsOk, i think I got it.
He basically removed the tweet text - @user with tweet text by user.
Perhaps he wanted to simplify the point for us so much that he ignored it. Or maybe he forgot it :-)