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 trialLeandro Severino
12,674 PointsDoes length of variable name matter with Interpreted languages?
Does length of variable name matter with Interpreted languages? Frow what I know it doesn't matter with compiled languages. Thanks!
1 Answer
Mike Wagner
23,559 PointsTL;DR Not usually...At least not in some of the ways you might think.
This is a rather interesting question that seems to come up a lot, and I remember reading a ton about this when I first started out, though I couldn't link to any good reads. Essentially it depends on the language and how it was implemented, but even in some of the more intensive implementations, the difference in performance between a=0
and a variable name ridiculously long is minuscule.
Interpreted languages tend to parse the variable names and assign IDs and such, so usually all variable names tend to wind up with names of the same lengths internally anyway. Could an insanely long variable name affect this? Sure. A lot of languages actually have an internal maximum length setup for variable names anyway though, so chances are it would never get to a point where it affected the parsing or application load enough to be an issue, most of the time anyway.
There are a few other performance concerns that may arise depending on database access, data transfer, and so on. Since some of these processes might actually involve the storage or transfer of these huge variable names, it would indeed slow down the process, increase the amount of data transferred and various other issues (most of which could be avoided with proper "good practice" techniques anyway, but that's another story).
Another "performance" concern is code readability and reusability. Want to share your code? Ever work with anyone else? Tried to bring someone new into your little group? Needed help debugging an issue you were having? These problems are compounded exponentially with every goodIntentionedButSlightlyConfusingOverlyComplexVariableNameThatMayOrMayNotActuallyTellYouWhatIAM = "Um, yes? That's me...I think?"
Sometimes, less is more.
The general rule of thumb is to make your variable names as short as possible whilst maintaining maximum clarity. It's a balancing act between clarity and length that, all too often, juvenile programmers will tip in the wrong direction. There are also other "best practices" like naming bool variables in a way that asks the question you're expecting a True/False (Yes/No) answer to, or naming functions/methods for the things they do or return rather than something else. These things become common convention because they work. They make our lives easier and the lives of all we expose to the spaghettified mess that is our code. I think in this aspect, the question isn't "do variable name lengths matter?" It's "do I really need that extra long variable name?"
Leandro Severino
12,674 PointsLeandro Severino
12,674 PointsThanks! Very good information mwagner. :)
Mike Wagner
23,559 PointsMike Wagner
23,559 PointsLeandro Severino - Glad it was helpful. If you want to find out more about it, you can find some pretty good reads with a quick Google search. Honestly, I think that's half of the fun of learning how to program. People can be quite creative and, oftentimes, they'll come up with things you would never think of or didn't know about. It's a welcome change to be able to read something that's not mile after mile of code :)