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

General Discussion

Adam Sawicki
Adam Sawicki
15,967 Points

Naming Variables

Hey,

I wonder what do you think about practice in naming varibales for eg. when we declare String variable we can name it sVariable or if int varibale we can name iVariable

3 Answers

I've seen that type of naming before, usually strVariable or intVariable though, which makes it a little more clear what you are doing.

Adam Sawicki
Adam Sawicki
15,967 Points

thanks, that's even better approach.

Michael Hulet
Michael Hulet
47,911 Points

Generally, you want to keep variable names as descriptive about their purpose as you possibly can, just like how you want to keep the names of functions as clear as you possibly can. Take this JavaScript code, for example:

var oVariable = {
    sVariable: null,
    nVariable: null,
    aVariable: null,
    bVariable: null
};

Can you tell me what that object represents, or what any of its keys or values is supposed to represent? Sure, you could probably tell me what data types it'll hold, but those are very easy to figure out. You should use your code to convey the meaning of what it's intended to do with each line, because it's way harder to figure out what the code above does than code like this:

var me = {
    name: "Michael",
    age: 18,
    languages: ["English", "JavaScript", "Python", "Objective-C", "Swift"],
    treehouseModerator: true
};

That way, code is much easier to maintain in the future, both by people who haven't read your code before and are trying to understand and modify it, and by yourself months later after you've forgotten what's going on