• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is String s = args[0] Hungarian notation?

 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Spurred on by the discussion in Barry's post this morning, I considered naming this thread Barry's Moaning Part II.
I've never really liked the use of s as an identifier for a generic String. That's it for my moan. On to the provocation...
I was thinking that, strictly speaking, String s = args[0] could be considered a minimalist's use of Hungarian notation. And therefore not in keeping with the Style Guide...
 
Johannes de Jong
tumbleweed
Bartender
Posts: 5089
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nah then it should have been String sS = args[0]
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sudharsan Govindarajan
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is Hungarian Notation perceived as against Object Oriented-ism? (atleast in Java)
-sudharsan
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I understand it, Hungarian Notation includes a reference to the data type of the identifier, which would be information specific to the program and its structure.
The idea of abstraction in object oriented programming encourages us to think of the elements in our program as representing something real outside of the program, in terms of the problem we're trying to solve.
From Bruce Eckel in "Thinking in Java":
...OOP allows you to describe the problem in terms of the problem, rather than in terms of the computer where the solution will run.

By highlighting data type, Hungarian Notation gets away from that emphasis on the problem.
Just my 2 cents...
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thinking in Java, by Bruce Eckel
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18098
50
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hungarian notation's designed to remind programmers in C that what they're planning on treating is a character is actually an integer, etc, since C's an insecure language when it comes to data typing.
I found it actually to use Hungarian notation dangerous in C++, where I might shift a variable from being a primitive to being a class and sometimes back again. I do a LOT of on-the-fly refactoring.
In Java the conversion rules are sufficiently tight that the compiler should be a much better guardian than some little mnemonic tack-on.
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if you named your identifier
iNumber because you were using an int

int iNumber = 99 ;

and later decided to use a long ...

long iNumber = 99 ;

or a double ...

double iNumber = 99.9 ;

another discussion on Hungarian notation
[ January 07, 2003: Message edited by: Marilyn de Queiroz ]
 
Marilyn de Queiroz
Sheriff
Posts: 9059
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pauline McNamara:
I've never really liked the use of s as an identifier for a generic String. That's it for my moan. On to the provocation...
I was thinking that, strictly speaking, String s = args[0] could be considered a minimalist's use of Hungarian notation. And therefore not in keeping with the Style Guide...

Interesting thought. Here is where you brought up the topic before (for whoever is interested), and Paul's response. You're not the only one, however, who doesn't like the use of 's' as an identifier. I think that you would be hard pressed to fight it based on the minimalist Hungarian notation theory though.
 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1817
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I was going to say that some of the best advice I ever received about variable names is to make them pronounceable. Imagine yourself reading your code to a colleague over the phone.
But Marilyn posted a link to a thread where I said that before, so I guess I don't have to say it again.
I have been picked on a couple times at work (yucky procedural language, not Java) for using some honking-long variable names, but noone's ever said my code's not clear.
My thoughts on one-letter variable names are slightly different from a year-and-a-half ago.
If a String is a title or a name or index or query or so forth, then call it that. But if you're manipulating a String that's generically a String (such as in Paul's atoi method), then what harm is there in it being just "s"?
The identifiers you use for formal parameters show up in Javadocs, right? So it's probably good to give them good names. No
public void myMethod( String bossSaidIHaveToPickBetterNames )
OR
public void myMethod( String s ) ?
 
Sudharsan Govindarajan
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

As I understand it, Hungarian Notation includes a reference to the data type of the identifier, which would be information specific to the program and its structure.

But from a programming point of view, having some naming conventions definitely do imporove the readability and understandability of the program. For instance in my swing program i have label and a textfield for entering user name.
I may name them as lblUserName and txtUserName. If i'm not going to use those 'lbl' and 'txt' how do i differentiate between them?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would use usernameLabel and usernameEntryField.
Or userNameLabel and userNameEntryField if it was important to distinguish between name and, say, address entities.
I mentioned in Moan I that if I was writing an implemention of a mathematical algorithm or physical formula, then I would want to stay as close as possible to the mathematical formulation using x, y, dx, x0, x1 and the like.
And...
If a local variable's "scope of use" (as distinct from "scope of existence" ) is over just a few lines of code then using a quicky like s for string, l for length, w for width, n for a number of times to do something is OK, especially if the variable appears a couple of times in the same or similar expressions. Depends on the "tightness" of the code, if you get my meaning.
Any more for any more of this fun stuff?
-Barry
 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1817
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barry Gaunt:

Any more for any more of this fun stuff?

Absolutely. Some of us old timers on the 'Drive have been round and round a few times on topics like these, but I'm always up for more. For me, it's particularly interesting too to see when and how my opinions have changed over time.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For instance in my swing program i have label and a textfield for entering user name.
I may name them as lblUserName and txtUserName. If i'm not going to use those 'lbl' and 'txt' how do i differentiate between them?

Group them togther into a new object with "label" and "field" attributes. Then you can use "userName.label" and "userName.field". Doesn't that look nicer?
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Matola:

public void myMethod( String bossSaidIHaveToPickBetterNames )
OR
public void myMethod( String s ) ?

just for chuckles:
public static void main( String[] args )
OR
public static void main( String[] commandLineArguments )
 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1817
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tres chic, Frank. Would you make that user name class top-level or something nested/local/anonymous? At what point would you switch to method calls for field and label instead of attributes?
 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1817
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Pauline McNamara:

public static void main( String[] args )
...

I know there's some history and baggage surrounding "args," but from where I sit (relatively newbie), "args" is just plain tacky.
Why not "argghhs"?
 
Michael Matola
whippersnapper
Ranch Hand
Posts: 1817
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
args
Plus there are those that would argue that identifiers for arrays and collections should be singular.
Doesn't args[ 0 ] look a little silly if you think about the fact that it means "give me the arg at position 0"? To those that would counter "but wouldn't arg.length look equally silly, for example" I would counter "remember that the type of 'arg' is 'array of something'".
I'm good at going around and around but not at reaching a decision.
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Barry Gaunt:
If a local variable's "scope of use" (as distinct from "scope of existence" ) is over just a few lines of code then using a quicky like s for string, l for length, w for width, n for a number of times to do something is OK, especially if the variable appears a couple of times in the same or similar expressions.

Hmmm. Quicky to write for sure. To read and comprehend maybe slightly less quicky. Maybe the code reader has been staying up too late staring at a monitor playing net games, lurking at JavaRanch, whatever, and sometimes mistakes an l for an I...
I like to picture my aging brain as a finely tuned sports car cruising down the highway. It's taking in someone else's code and it's all making sense - hear the quiet hum?
Then it comes across an l, a w and an n. Brain slows down ever so slightly (but noticably) and does an "oh there's an l, that means length, w, what was that again?, oh yeah width, n n n n oh right that's a number!"
For just a short moment the fine cruisin' is gone, brain slowed down, like an old VW at the bottom of a long hill (hear the sound, like chains rattling ?). Brain handled it fine, but took a performance hit. Over just a few lines of code. Multiplied by, let's see, how many occurences of similar "few lines of code"?
Yes, afer a bunch of times, brain recognizes l, w and n more quickly. But what about other code that uses len, wd and num? (Rattle rattle)
It's so easy to just write out length, width and number. (Hummm)
Optimizing machine performance may not have high priority 'round here; optimizing brain performance, now that's a whole 'nother thing. I for one know that my brain could use it. (Not me, my brain. I'm way smarter than my brain. )
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Michael Matola:
Why not "argghhs"?

Great minds...
I had actually considered a third option:
public static void main( String[] commonWayToExpressExasperation )
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pauline, you mean like crossword puzzles?
public static void main( String[] hotHeadedConversations )
And... regarding well tuned sports cars: Ever ridden in a Triumph TR2 at 105 Mph with no lid? Reading java source?
[ January 08, 2003: Message edited by: Barry Gaunt ]
 
Sudharsan Govindarajan
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Group them togther into a new object with "label" and "field" attributes. Then you can use "userName.label" and "userName.field". Doesn't that look nicer?

Looks nicer! But won't it increase the complexity of the program and there by decrease the ease of understanding it?
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ever ridden in a Triumph TR2 at 105 Mph with no lid?
If that's an offer, I'm in! (On the driver's side, of course )

Reading java source?

Oh, that's what you meant. Oh yeah, that's when I don't understand
a thing
, more like white knuckles grabbing the dashboard on the passenger side.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18098
50
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think anyone has noted the difference between prefixing a variable name with an indicator as to its TYPE (e.g. iFubar) vs. its FUNCTION (ctlFubar). I've been known to do prefixing on related control objects, which aren't always well-served by trying to cram them into a containing super-object. For example, lblAmountPaid is the control ID for the (static text) caption control and ctlAmountPaid is the control ID for the actual data-entry control.
The idea of minimal-length identifiers is in large part due to the fact that programming languages more or less evolved out of mathematical notation, where specific idenitifiers mean specific things. Often also depending on their typeface, alphabet, whether it's written upside down, etc.
For throwaways like loop indexes, it's certainly a lot more convenient to just say "i", "j", etc. and go on, but I've learned to my pain that anything that you'll ever want to give a more meaningful name later should be long enough that a text editor doing a global replace won't wreak havoc.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic