This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Cattle Drive and the fly likes variable names Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "variable names" Watch "variable names" New topic
Author

variable names

Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
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


Richard Boren
Ranch Hand

Joined: Mar 01, 2001
Posts: 233
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.
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
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.
Richard Boren
Ranch Hand

Joined: Mar 01, 2001
Posts: 233
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).]
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
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

Matthew Phillips
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
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.
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
Sorry I only saw this thread now. I think it can/should be in the "open" forum I'm moving it there
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1744
    
    2
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.
paul wheaton
Trailboss

Joined: Dec 14, 1998
Posts: 20488
    ∞

I use s regularly. Usually in a context where it is pretty obvious what is going on.

If the use of s is small, this works well. If s is used a lot, it should probably be renamed.

permaculture Wood Burning Stoves 2.0 - 4-DVD set
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: variable names
 
Similar Threads
Button Problems
Code to be easily readable by a Junior Programmer
Printing an array
From VB to Java
NIO read/write review