The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Multilevel Variables and Hungarian

I've recently converted to Hungarian notation (apps, not system, of course), but I quickly ran into a snag. And I'm programming in Perl, so I abandoned the default hungarian types in favor of my own.

For multi-level dictionaries, should the dictionary sublevel names be treated as variables, or not? Here's a pseudocode example (apologies if the format gets mangled, leading periods are only for indenting purposes):

string ssubCur = 'main'
string ssubNxt = 'user'

dictionary hCfg = (
. string sEmail = '',
. string surProtocol = 'http',
. string surServer = '',
. dictionary hssub = (),

hCfg(hssub)(ssubCur) = (
. string shtTitle = 'Main Menu',
. array aspg = ('default'),

hCfg(hssub)(ssubNxt) = (
. string shtTitle = 'Manage Users',
. array aspg = ('add', 'remove', 'view'),

Custom Types:
a - array
h - hash (perl equivalent of Dictionaries)
s - Safe string, suitable for interacting with the system.
ht - Contains HTML
ur - Contains URL
sub - Module
pg - Page, one part of a Module

I'm mostly concerned about whether hungarian is being used appropriately here... I've only just started refactoring the application, and some aspects of using hungarian in dictionaries confuses me... for example, to reference the title of the 'Manage Users' page, I would get hCfg(hssub)(user)(ahtTitle)... it seems wrong to have a portion of the reference be a value ('user') rather than a hungarian variable name. But, it seems more wrong to have the contents of a variable be the hungarian variable name... I don't think the DATA is supposed to be converted to hungarian notation... it would be silly to have a list of modules named 'ssubMain', 'ssubUser' or something equally bizarre.

Are there any conventions for this? Should I even be using hungarian notation for the other dictionary contents, like shtTitle?

Thank you for your opinions. I reserve the right to ignore your advice, but be assured I will weigh it thoughtfully, since anyone likely to respond probably has vastly more experience using Hungarian notation than I.
Myrddin Emrys Send private email
Wednesday, November 09, 2005
The other issue I've found is that it confuses the normal matching for Hungarian when using dictionary variables... for example, this is (I think) correct:

string sacl = 'admin'
array asmod = hsacl(sacl)(asmod)

In a dictionary of acl's (indexed by their name, sacl), I'm getting an array of module names out. Hungarian needs to match the dictionary name to it's first paramenter, and the last parameter needs to match the name of what I'm assigning it to. So:

A = hB(B)..(A)

I cannot see a way to massage the code so that you get the typical "A = A.." simplicity of hungarian back...
Myrddin Emrys Send private email
Wednesday, November 09, 2005
I personally use Hungarian notation to make my code easier to read, but looking over your example I can't see how it is making things easier to read :(

For example you write this:

  string ssubCur = 'main'

which using your "Custom Types" reads as:

  a "safe string, module, current"

What exactly is a current "safe string, module"?

Isn't "current" nothing but a "safe string" so why not name it as:

  string sCurrent = 'main'

or alternatively:

  string sCurrentModule = 'main'

Being a c/c++ programmer, I'm not familiar with your programming language, but I do believe one of the main reasons for using Hungarian notation is make things easier to code.

So if your scheme works for you that is all that really matters :)
Jussi Jumppanen
Wednesday, November 09, 2005
Well, it's a safe string that holds the name of a module. It would not make sense to assign the name of a module to the name of a page... so this would be silly:

ssub = spg;

But it might make sense to assign the Current module name to, say, a temporary variable:

ssubSav = ssubCur;

From what I've read, if the beginnings match, then it should be appropriate, under the right circumstances, to assign one variable to another. But if the beginnings do not match, then it is never appropriate to assign the two variables to one another. By giving module names (ssub, a safe string representation of the subroutine) and page names (spg, a safe string representation of the page) different hungarian types I ensure that I never assign one type to another.

As an extension, I also (of course) use the Hungarian type 'us'. From the page URL I collect a dictionary of the provided query string values 'husf' (A hash/dictionary of unsafe field names). One of those unsafe field names is 'sub', which gets assigned to 'ussub', because I do not know if such a module really exists or not... just because the user requested it doesn't mean it exists. Later, I have a function that checks whether this module really exists... SsubFromUssub().

I hope that clears that up for you.
Myrddin Emrys Send private email
Wednesday, November 09, 2005
John Logan Send private email
Sunday, November 13, 2005
"ssubSav = ssubCur;"

This is where hungarian notation seems good in theory but breaks down in practice.  All these names are horrible!

Honestly, if you ever get *that* confused about what your variables are then you've got too many in scope.  And abbrevations suck -- at least do ssubCurrent!!
Almost H. Anonymous Send private email
Tuesday, November 15, 2005

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz