This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
This doesn't reveal any solutions code so it doesn't necessarily belong here, but it does deal with something found in some solutions. I was curious what you all think about one letter variable names, for example, String s = ..., etc. I have fun trying to think of a way to describe a variable with one word, in addition to thinking it might be a good habit to increase readability... Not that it threw me off or anything, but I was surprised to see them in some solutions. Or is it kind of understood that naming certain variables with a single letter is the usual practice in certain cases? Pauline
Variable names should be meaningful. Is s a meaningful name? No. However, since its usage is only within a short coded segment it really doesn't hurt. Still to the strict letter (pardon the pun) of the style guide I don't think it should be used. Saying that, I have used it myself. Shame on me.
Joined: Jan 19, 2001
10 lashes with a wet noodle for you. More seriously, though, is it a programmer's habit? When something is used a lot it kind of takes on meaning, and maybe I should just get used to certain conventions. After all, environments other than the cattle drive probably (?) don't emphasize readability so much.
Joined: Mar 01, 2001
Originally posted by Pauline McNamara: More seriously, though, is it a programmer's habit? When something is used a lot it kind of takes on meaning, and maybe I should just get used to certain conventions.
When trying to determine why a programmer used a certain naming convention a lot of issues are raised. The language being used, the standards they are familiar with, what type of programming the most often do (system, business application, utilities, VCR's, etc), their point of view (should it be generic or specific), just to name a few without bring in personality issues (are they just lazy?).
After all, environments other than the cattle drive probably (?) don't emphasize readability so much.
This varies from company to company, from project to project, from team to team... Some are picky others are not. I worked for one company that all array subscripts had to be named SUB1, SUB2, SUB3 etc., and another that put a limit on the number of charters to 25 for variable names. You have to use the companies standards whether you have a better way or not. Generally most environments I've worked within required meaningful names be used and would frown on using a single letter for any type of name. When it comes to names there are as many opinions as there are people on earth. There will always be personal preferences when it comes to naming standards. I have been to my share of code walks where programmers came away mad because someone didn't like the names they had used. And there is always that power trip out there. It doesn't matter what you call it they'll always have something different for you to use. I have actually seen this to the point where you go to a walk and they tell you to rename your lastName variable to nameLast. Then when you go to a subsequent walk over the same code they tell you to rename your nameLast variable to lastName. In a nutshell I wouldn't worry about it too much. If you always use meaningful names then you should never have anything to worry about. If programmer A uses x and you use total, you have definitely done a better job at naming no matter who programmer A is.
[This message has been edited by Richard Boren (edited July 03, 2001).]
My personal opinion is that a variable name should be meaningful. I try to avoid one character names, unless conforming to a standard( i.e. i for a counting variable in Java). I like to look at a variable and know exactly what purpose it serves in a program. Matthew Phillips
Joined: Jan 19, 2001
Originally posted by Matthew Phillips: I like to look at a variable and know exactly what purpose it serves in a program.
Yeah, that helps me a lot to understand what's happening. Guess I was surprised to see one letter names in the solutions.
Two pieces of advice about naming variables that I've found to be helpful are (1) Make the name easily pronounceable. You're less likely to trip over variable names that you can pronounce. I sometimes carry this to the point of not even using common abbreviations. I would prefer stringLength over strLen, initialValue over initVal, object over obj, numberOfStudents over nbrOfStdnts, etc. (2) Choose the name from the problem domain and not from the solution domain. That is, choose names that have to do with the problem you're solving, not with the way you go about solving it. I tend to prefer names like beforeHyphen and afterHyphen over subStr1 and subStr2, names over strings, etc. That said, I have to admit that Marilyn's rightfully nitpicked some of my variable names. I've found that if I have a particularly bad variable name, there are probably some other things in the code that could be cleaned up too. That said, I have to admit that there are several variable names in the instructor's solutions that I don't like at all. I fall into the habit of using the one-letter variable names when I'm lazy. But I struggle against it.