Apps Hungarian Rocks

I’m cur­rently writ­ing a plu­gin for an open-source tool that will let me use LDAP as a back­end store. Rather than let it become vapor­ware, I’ll just post a note about it and what it does once I’ve sub­mit­ted the patch.

In the mean­time, I’ll note that hav­ing a clearly defined stan­dard for nam­ing things is always a win. Whether it is a coher­ent scheme for your vari­ables or your sys­tems, it’s one less thing you need to think about. It allows those who are famil­iar with the envi­ron­ment to make assump­tions — it raises the bar of com­plex­ity you’re able to han­dle with­out mem­o­riza­tion, and frees up your inter­nal reg­is­ters for what is actu­ally going on, either in the code or net­work topology.

In that vein, I finally got around to look­ing up some exam­ples of Apps Hungarian for use in the afore­men­tioned project. As soon as I had fin­ished refac­tor­ing to use the new AHN names, the entire project seemed (some­how) much clearer:

for ( strCatalog, mpKeyValue ) in mpCatalogMpKeyValue:
    # You can guess what everything is here pretty easily

First and fore­most, any sub­con­scious uncer­tainty about what a vari­able is used for dis­ap­pears. It’s a pal­pa­ble weight off your shoul­ders when you’re long into a pars­ing loop and have a dozen vari­ables in use at mul­ti­ple lev­els at any one time. I’m one who prefers ver­bose lan­guages (think: C#). So if I can add the biggest win of that ver­bosity back into a lan­guage like Python sim­ply by being dis­ci­plined in how I name my vari­ables, it’s totally worth it.

9 Responses

  1. Ben says:

    Map, but that doesn’t make the code more read­able or more beau­ti­ful. I tend to fol­low the “if you can­not see the use of a vari­able right away your method is too long” school, but that’s com­ing from C# and Java. Anyway: Eeek. With my back­ground this gives me the creeps ;)

  2. James Cape says:

    Ben:

    I tend to agree with the assess­ment of func­tion length, and per­haps the exam­ple is poor. A bet­ter one might be mark­ing “unsafe/user strings” as usSomeVariable.

    Which is nec­es­sary, par­tic­u­larly when your stack is a mile high and an inch across — and you’ve pretty much lost all rela­tion to how far you are away from user input…

  3. James Cape says:

    frag­gle:

    The mp pre­fix is from the orig­i­nal writeup for Simonyi’s Apps Notation. The choice of “str­Cat­a­log” is poor imag­i­na­tion on my part, of course, “stk­Cat­a­log” or “hksCat­a­log” or what­ever is just as well for that case.

    But it’s a one-line exam­ple in a blog post/I don’t care that much about mak­ing it perfect.

  4. James Henstridge says:

    James: if you are try­ing to dif­fer­en­ti­ate between dif­fer­ent types of strings, per­haps you could use dif­fer­ent types for the strings?

    As an exam­ple, Django has a SafeString type that the tem­plate sys­tem assumes has already been escaped when insert­ing into the page. Override a few oper­a­tors to pre­vent acci­den­tal mix­ing of safe and unsafe strings, and you have some­thing much more effec­tive than a vari­able nam­ing convention.

Leave a Reply

*