Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Java Java Data Structures Organizing Data Interfaces

Why 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);

}

Ok, 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 :-)

3 Answers

Hi,

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
STAFF
Craig Dennis
Treehouse Teacher

Yeah 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!

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