Hungarian notation (was RE: variable naming...)

John Schmitt jschmitt at vmlabs.com
Tue May 1 01:56:44 EDT 2001


Me personally, I despise it.  If that's "hate by conviction", so be it
because I can only guess at what you mean by "hate by conviction".

If you want "hate by logic", it goes something like this:

I think of writing software as modelling something.  You have something out
there in the 'real world' and you are trying to model it in your computer
program.  If you have a cartesion coordinate like this:

typedef struct
{
	int x;
	int y;
} POINT;

you are modelling the notion of a point in the cartesian coordinate system.
The fact that it is implemented using a C struct and that its members are
int types, is kind of incidental.  I do not see any value at all in naming
it this way:

typedef struct
{
	int ix;
	int iy;
} stPOINT;

void vstPOINTSet( pstPOINT* pstpoint, int ix, int iy )
{
	pstpoint.x=ix;
	pstpoint.y=iy;
}

/* ad nauseam */

{I haven't used Hungarian Notation in so long, I don't even remember all the
rules (the fact that there are a bunch of rules to remember is another
reason to hate it since not being able to recall the rules instantaneously
is an impediment to understanding someone else's code) so I went by this:
http://www.lohnet.org/~hornlo/resources/win/win-dt-notation.html The fact
that someone had felt a need to compile this list because he couldn't
remember it says it all}

Now, if you write a program that uses the above-mentioned struct in a bunch
of places, what will you do when it's time to refactor?  Supposing that
someone decides that we in fact need floats rather than ints to represent
points?  Search and replace?  Hand editing?  That's a good waste of my time.

The underlying fundamental problem is that the programmer speaking in the
programming language domain and not in the problem domain.  (I wish I knew
who originally pointed this out to me so I could attribute it appropriately)
Until that distinction is made by the programmer, that programmer is going
write code, and is not going to write programs.

It has been my experience that programmers who actively use hungarian
notation by choice produce terrible, hard-to-maintain code.  I think it's
because they don't write programs, they write code.  And maybe that's where
people's "hate by conviction" comes from - people's experience with code
written with hungarian notation.

  Just getting something to work usually means writing reams of code
  fast, like a Stephen King novel, but making it maintainable and
  high-quality code that really expresses the ideas well, is like
  writing poetry.  Art is taking away.  -- Erik Naggum, comp.lang.lisp


In the specific case of Python, since types are not statically declared, it
makes even less sense to embed the type inside the variable name.

Thinking about this a bit more, there are four (more actually, but these are
the interesting ones for this discussion) possible scopes for a variable:

1) function level scope, both C and Python
2) local to the file/module, global to all the functions in the file/module,
both C and Python
3) class members, both C++ and Python
4) global (visible by all modules in the program), C (in Python you have to
name its namespace/module, right?)

1) In C, the type of the arguments and return value is obvious because its
in the signature of the function.  I don't see any value in embedding the
type of the variable because it's instantly visible.  If you're functions
are so long that this is not true, then you have bigger problems than any
amount hungarian notation will ever solve.  In Python, people talk about
'file-like' objects.  (I've been trying to follow the discussion on
interfaces in another thread in c.l.python)  I can't think of a single
nonsensical way to come up with a hungarian notation scheme for a variable
in that context.

2) If you're accessing a variable in a function and it's not declared in
that function, try the top of that file.  If that's too much trouble or you
have so many variables at the file level, then you have bigger problems than
any amount hungarian notation will ever solve.

3) If you're accessing a variable in a method and it's not declared in the
method, look at the class definition.  If that's too much trouble or you
have too many variables at this level, then you have bigger problems than
any amount hungarian notation will ever solve.

4) If you have so many of these that they're hard to manage, then you have
bigger problems than any hungarian notation (or any other naming convention)
will ever solve.

John

I'm sorry, was my bias showing again? :-)
                                    -- William Tanksley, 13 May 2000

> -----Original Message-----
> From: ransen_spam_me_not at nemo.it [mailto:ransen_spam_me_not at nemo.it]
> Sent: Thursday, April 26, 2001 10:21 PM
> To: python-list at python.org
> Subject: Re: variable naming...
> 
> 
> On Thu, 26 Apr 2001 23:47:04 GMT, "Tom" <NoSpam at NoSpam.com> wrote:
> 
> >Yes, I find 'hungarian' notation quite useful too, but it seems to be
> >associated with MS and is frowned upon.  Too bad.
> >Tom.
> 
> Yes, theres a lot of "hate by conviction" rather than
> "hate by logic" ! ;)
> 
> 
> --
> Owen F. Ransen
> http://www.ransen.com/
> Home of Gliftic & Repligator Image Generators
> -- 
> http://mail.python.org/mailman/listinfo/python-list
> 




More information about the Python-list mailing list