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

Android Build a Simple Android App (2014) Improving Our Code Adding More Colors

BAD example of setting variables

variable "color" and Java Object "Color" makes learning so difficult. Isn't better to avoid variables "color" or "relativeLayout" and make them something easy to use and remember ?

2 Answers

Justin Horner
STAFF
Justin Horner
Treehouse Guest Teacher

Hello Atul,

I understand how this could be confusing at first but you'll find that this is actually a common pattern for naming variables. You want variable names that are descriptive and in context. Being too short or arbitrary will not carrying meaning for the next dev working in the project or even for yourself for long-term maintenance.

Matching the variable name close to the type ensures that we understand what the type of the variable is without constantly referring to it's declaration. This is especially true when working in a large code base.

When naming variables always ask yourself, "Will I know the purpose and type of this variable based on the name weeks, months or even years down the road?". Ultimately, naming conventions are personal preference and will be different across companies and teams but they should always favor better understanding and maintainability.

I hope this helps.

Hi Justin,

I guess the good programming practices (atleast in VB, which I use) call for prefixing string variables with "str" in front of variable so that somewhere 1000 lines down the person working on code knows he/she is dealing with string variables.

I think just plainly setting a variable as "color" wouldn't it have been much better if the instructor had used "strcolor" or "varcolor" not sure if this is standard practice in Java though.

HTH