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 trial

Java Java Data Structures - Retired Organizing Data Serialization

Repeated use of the same word to name classes, variables, and parameters

Is this some kind of best practice? At this point in the lesson, there are so many subtle variations and uses of the word "treet" in multiple uses, that I have to wonder whether this is a good way to do things, or a bad way to do things. For example, I would think that the "Treets.java" class should have a name that indicates its use in Serialization. Something like "serTreets." And, I would think that the names for parameters that are used locally should not be identical to the name of the external value being passed into the method. like here:

public static void save(Treet[] treets) {

It all just seems unnecessarily confusing. But, perhaps repetition like this is preferred? I don't get it.

2 Answers

Craig Dennis
STAFF
Craig Dennis
Treehouse Teacher

Hmmm...that's probably actually pretty standard naming conventions. Remember that someone looking to use that method is going to be looking at those method definitions trying to think about what to put in there.

"Oh I see! It takes an array of Treets"

I agree at this point in time that probably isn't helping. If things do indeed become confusing, it is probably more likely it would be named something that helps it read better like treetsToSerialize or something along those lines. "This takes an array of treets, which are the ones to serialize".

I do see the point of contention though. It'll get easier to weed through with more exposure.

As a VB6/VBA programmer, I am used to prefixing most variables and objects with a 2, 3 or 4 letter abbreviation that identifies the data type, e.g. strFirstName, intCount, dteStartDate, tblDataTable etc. I imagine with practice I'll become more accustomed to Java coding conventions but I still find them very confusing.

OK. So, I guess I was expecting to see a few more instances of the use of some prefixes or suffixes, such as in "mAuthor", where m indicates that this is a member variable. I'm also accustomed to using more single letter variable names, like i, j, or k, in things like for loops, when learning other programming languages. So, it might be more like

public static void save(Treet[] t) {
    try (
      FileOutputStream fos = new FileOutputStream("treets.ser");
      ObjectOutputStream oos = new ObjectOutputStream(fos);
    ) {
      oos.writeObject(t);
    } catch(IOException ioe) {
      System.out.println("Problem saving Treets");
      ioe.printStackTrace();
    }

But, I can understand that maybe this is a bad practice. When I teach kids how to code, they want to give variables silly names like "joebob" and "poop"....obviously that's not a best practice! :)

Craig Dennis
Craig Dennis
Treehouse Teacher

I still use the variable poop to debug code. So does Andrew Chalkley . Old habits die hard.

Craig Dennis
Craig Dennis
Treehouse Teacher

Yeah it's more likely to be super specific suffixes