Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C
Tip/Trick

Hungarian Notation in Microcontroller Code

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
8 Jun 2014CC (ASA 3U)2 min read 12.4K   2   5
I'm advocating for the use of Hungarian notation in microcontroller code

What happened to Hungarian Notation?

Hungarian notation had fallen out of use in the mainstream programming, compared to late 1990s and early 2000s. In my observation, today the default setting in the majority of the code writers' minds is Hungarian notation: off. What had caused this? Is there really no use for Hungarian notation?

Mainstream IDEs have Evolved

Mainstream Integrated Development Environments (IDE) have been getting increasingly more powerful. Now they provide on-the-fly some reference information about variables. This reduces the need to encode additional meta-information (type, scope) into the name of the variable.

OO Languages vs. Non-OO Languages

OO languages (such as C# and Java) vs. non-OO languages (such as plain C). OO program may use a larger set of data types, which can include a large number of classes. Remembering (and sometimes inventing) a map of Hungarian prefixes becomes more tedious.

Using the prefix p for pointers in C++ is still a good idea. Pointers should be treated with additional care, so it wouldn’t hurt to have this additional bit of labeling. Similarly, fp for function pointers.

Refactoring

Refactoring has emerged as a major force in software development.  Hungarian notation, however, predates the wide use of refactoring.  For example, an initial version of the code may have status represented by a string.  Later it gets refactored to an enum which provides condition for switch statements.  Later it gets refactored again, and condition is replaced with polymorphism. 

  • Without Hungarian, the metamorphoses of the member variable (field) would be something along the lines of
    string m_status -> StatusEnum m_status -> Status m_status
    The type of the member variable changes but the name remains the same.
  • With Hungarian
    string m_strStatus -> StatusEnum m_iStatus -> Status m_objStatus
    Notice that both the type and the name change.

The first case requires less change to the source code.  Then again, automated refactoring aids would make everything smoother.

Embedded Programming

Code for microcontrollers (especially, the relatively small 8- and 16-bit ones) is often written in plain C. IDEs for microcontrollers often lack Intellisense and other productivity features. As a result, I think that Hungarian notation is still strongly indicated for microcontroller code.

License

This article, along with any associated source code and files, is licensed under The Creative Commons Attribution-Share Alike 3.0 Unported License


Written By
Systems Engineer Prolitech
United States United States
doing business as Prolitech
Redwood City, CA

blog (mostly technical)
http://prolifictec.com/blog/index.html

Comments and Discussions

 
GeneralThoughts Pin
PIEBALDconsult22-Sep-14 9:47
mvePIEBALDconsult22-Sep-14 9:47 
GeneralRe: Thoughts Pin
Kochise28-Aug-20 2:53
Kochise28-Aug-20 2:53 
QuestionWhy Hungarian Notation is no longer common. Pin
Bill_Hallahan9-Jun-14 16:17
Bill_Hallahan9-Jun-14 16:17 
I don't even use Hungarian notation when I write C code.

I used to use Hungarian notation early in my career, but abandoned it after I read several books and papers about research on coding styles.

Hungarian notation fell out of use because it generally makes the code less readable than clear names. It also makes more work to change the type of a variable. The longevity of code matters more than the current favored platform-types too. It should be easy to change types for a new platform.

As to readability, imagine prefixing all verbs in a story with "vb", nouns with "no", ajectives with "aj", adverbs with "av", articles with "ar", pronouns with "pn", prepositions with "pr", and conjunctions with "co".

arThe noText vbWritten ajlike pnThat vbWould vbBe avSignificantly ajMore ajDifficult prTo vbRead coThan ajNormal noText!

I believe it was Dennis Ritchie, one of the co-creators of the C language, who said code should be written like text in a book. I believe he was referring to whitespace too.

I agree using special syntax for pointers is useful. Also, decorating data-members in an object oriented language is common too. I do both of those, because the usage of such variables is very different from other variables.

And, occasionally it "might" be useful to decorate a variable with a type, particularly when writing certain types of shift-and mask processing with different size types, but it has fallen out of general use for the reasons I mentioned above.
AnswerRe: Why Hungarian Notation is no longer common. Pin
Kochise28-Aug-20 2:47
Kochise28-Aug-20 2:47 
GeneralLanguage more than target system Pin
John Brett9-Jun-14 6:28
John Brett9-Jun-14 6:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.