Making coding easier with Hungarian notation

Hungarian notation is quite simple.

It involves pre-pending your descriptive variable name with a lower case character or short string that tells you the type of the variable.

So:

Global variables outside setup() and loop(): 'g_', e.g. int g_nPinNum (this is a personal HN customization that I use)
Class (or struct) data members: 'm_', e.g. int m_nPinNum
This is particularly handy with set(...) functions in your class
E.G. void setPinNum(const int nPinNum)
{
m_nPinNum = nPinNum;
}
If you decide you need to change the descriptive part of the name then it is easy to do in one
operation with text search and replace.

Any array: 'array' or 'arry', e.g. int arrayPins[6];
Character array string: 'sz', e.g. char szName[9];
Arduino String object: 'str', e.g. String strName;
Any pointer variable: 'p', e.g. int *pData = NULL;
bool: 'b', e.g. bool bFlag = false;
char: 'c', e.g. char cCh = 0;
int8_t / uint8_t / byte: 'n', e.g. uint8_t nPinNum = 1;
int16_t / uint16_t / int / unsigned: 'n', e.g. uint16_t nData = 1;
int32_t / uint32_t / long / long unsigned: 'n', e.g. uint32_t nLastMillis = 0;

Now you can extend the integers a little if you find it useful, but it will also mean changing your variable names if you change its data type. I don't bother myself and find 'n' for all of them adequate.

uint8_t / byte: 'u8', e.g. uint8_t u8PinNum = 1;
int8_t: 'n8', e.g. int8_t n8PinNum = 1;
uint16_t / unsigned / unsigned int: 'u16', e.g. uint16_t u16Data = 1;
int16_t / int: 'n16', e.g. int16_t n16Data = 1;
uint32_t / long unsigned: 'u32', e.g. uint32_t nLastMillis = 1;
int32_t, long: 'n32', e.g. int32_t nLastMillis = 1;

Now where it is really useful in situations like this:

FirstName + Surname + Age + StudentNumber

Now if you came back to this code 12 months later you might not remember what was going on here.
'FirstName' and 'Surname' are probably string. But what is 'Age'? Is it a string too or is it an object? And what about StudenNumber? Is it a String, a number or some sort of object?

But if you did this instead then it becomes perfectly clear.

strFirstName, + strSurname + szAge + strStudentNumber

It is string concatenation where the first two are Arduino String objects and the last two are regular 'C' or char array strings.

Hungarian notation can also tell you rather a lot about a function as in this example:

String strDetails = formatStudent(strSurname, strFirstName, szAge, szStudentNumber);

Please note that descriptive variable names and Hungarian notation don't have to be mutually exclusive, there is nothing stopping you from using both naming conventions in a complementary way.

I object to this being parachuted in here. There was a long discussion about this subject, mostly disagreeing with the concept.

IMHO Tutorials should only be posted here when there is a consensus supporting them.

...R

Robin2:
I object to this being parachuted in here. There was a long discussion about this subject, mostly disagreeing with the concept.

IMHO Tutorials should only be posted here when there is a consensus supporting them.

...R

If you don't agree with it then don't use it.

This is an open forum and it is not, nor should be, subject to censorship because you don't agree with it!

Thanks for explaining,

Hungarian notiation (HN) is indeed one way to help people to understand code on the dataType level.
I personally are not a great fan of it as the compiler will check on that level.

HN does not help to understand code on architectual/design or requirements level which are imho
far more important. The programmer should know WHY to use an uint32_t, or a volatile, static etc.

After 12 months I have more problems wiyh why a certain algorithm was used, how the flow of information etc is, or how does it work at all. Yes it is still important to know what the datatypes of the variables are but these are often direct deducable from code.

e.g. Serial.println(Age); // will print the Age, no matter if it was a string or number

Age = Age + 5; // clearly a nummeric type

Age = Age + "years" // bet it will be a String

Yes HN would help in all those case, but it does not explain why e.g. 5 was added

For me it is more important that people give a good descriptive name to the variables, classes and functions.

Would you consider using Hungarian notiation also for functions?

String strDetails = formatStudent(strSurname, strFirstName, szAge, szStudentNumber);
==>
String strDetails = strFormatStudent(strSurname, strFirstName, szAge, szStudentNumber);

How do you handle HN for class class instances?
And fields/methods of those classes?

what is more readable?

if (vkbeKeyBoardEvent.u16ErrorCode == u16ErrorTable[u8ErrorIndex]) ...

or

if (KeyBoardEvent.ErrorCode == ErrorTable[ErrorIndex]) ....

// vkbe - volatile KeyBoardEvent, jus made that up ...

There are similar style discussions about adding comments to code. Yes it helps people a lot if the comment explains the WHY of the code is written that way. Comment does not help if it just repeats the code, or worse it does not match the code anymore. Choosing good descriptive names for variables makes code readable.

Serial.begin(9600); // open the serial port with 9600 baud

one review later
Serial.begin(115200); // open the serial port with 9600 baud

another review
Serial.begin(config.baudrate); // open the serial port with 9600 baud

while it should just be
Serial.begin(config.baudrate);

Thanks for explaining

Robin2:
I object to this being parachuted in here. There was a long discussion about this subject, mostly disagreeing with the concept.

IMHO Tutorials should only be posted here when there is a consensus supporting them.

...R

Think the community is too large for having consensus, I appreciate the link you post to a previous discussion as that is more constructive in the long run.

robtillaart:
Thanks for explaining,

Hungarian notiation (HN) is indeed one way to help people to understand code on the dataType level.
I personally are not a great fan of it as the compiler will check on that level.

HN does not help to understand code on architectual/design or requirements level which are imho
far more important. The programmer should know WHY to use an uint32_t, or a volatile, static etc.

After 12 months I have more problems wiyh why a certain algorithm was used, how the flow of information etc is, or how does it work at all. Yes it is still important to know what the datatypes of the variables are but these are often direct deducable from code.

e.g. Serial.println(Age); // will print the Age, no matter if it was a string or number

Age = Age + 5; // clearly a nummeric type

Age = Age + "years" // bet it will be a String

Yes HN would help in all those case, but it does not explain why e.g. 5 was added

For me it is more important that people give a good descriptive name to the variables, classes and functions.

Would you consider using Hungarian notiation also for functions?

String strDetails = formatStudent(strSurname, strFirstName, szAge, szStudentNumber);
==>
String strDetails = strFormatStudent(strSurname, strFirstName, szAge, szStudentNumber);

How do you handle HN for class class instances?
And fields/methods of those classes?

what is more readable?

if (vkbeKeyBoardEvent.u16ErrorCode == u16ErrorTable[u8ErrorIndex]) ...

or

if (KeyBoardEvent.ErrorCode == ErrorTable[ErrorIndex]) ....

// vkbe - volatile KeyBoardEvent, jus made that up ...

There are similar style discussions about adding comments to code. Yes it helps people a lot if the comment explains the WHY of the code is written that way. Comment does not help if it just repeats the code, or worse it does not match the code anymore. Choosing good descriptive names for variables makes code readable.

Serial.begin(9600); // open the serial port with 9600 baud

one review later
Serial.begin(115200); // open the serial port with 9600 baud

another review
Serial.begin(config.baudrate); // open the serial port with 9600 baud

while it should just be
Serial.begin(config.baudrate);

Thanks for explaining

Look I figure it this way....

You can have strictly descriptive variable names and a comment explaining that all the variables are strings being concatenated.

Or you can just use 'str' in front of your variable names which explains the same thing with a lot less typing.

And no HN tells you nothing about code structure or efficient coding etc.

But it is not designed to do that anyway.

Code structure and efficiency are things you have to learn separately.

Age = Age + 5;   //  clearly a nummeric type

Age = Age + "years"  // bet it will be a String

I appreciate this example, but their are situations where it is not so clear cut, e.g. when literals are not involved.

But regardless of literals you don't go mixing your conventions do you?

In your example you would use nAge or strAge regardless - all HN or nothing.

And the u8, n8, u16, n16,..... idea - I personally find it unnecessary and don't use it.

I just put it out there as an option and as an example of customization of HN to suit your personal needs. As the MFC programmers have done with LPCSTR and lpsz etc.

Wasn't Hungarian notation designed for C?

But we're in C++ here.

(I've asked for a moderator to move this to "General Discussion", because I don't see how it helps beginners, who struggle with datatypes as it is, without confusing them with how the variables should be named)

GrooveFlotilla:
Wasn't Hungarian notation designed for C?

But we're in C++ here.

(I've asked for a moderator to move this to "General Discussion", because I don't see how it helps beginners, who struggle with datatypes as it is, without confusing them with how the variables should be named)

Who cares? Who says you can't use HN in C++?

Or Javascript, PHP, C#,......

…because some of those other languages allow you to “add” an integer to a string, for example?

(And because your software ends up looking like it was written by Microsoft in the 1990s, and nobody wants that :smiley: )

GrooveFlotilla:
..because some of those other languages allow you to "add" an integer to a string, for example?

(And because your software ends up looking like it was written by Microsoft in the 1990s, and nobody wants that :smiley: )

Big woop.....that means the HN will indicate that doing so is allowed in that particular language.

boylesg:
Big woop.....that means the HN will indicate that doing so is allowed in that particular language.

...but makes porting the code (specifically the variable name) to a different language that much more difficult.

What is it with the coding nazis in here?

Why is a language independent coding convention causing so much controversy???

It is a coding convention!

You use it if it helps your coding and not if it doesn’t!

And there is ABSOLUTELY NO REASON why it needs to be restricted to any particular language.

GrooveFlotilla:
...but makes porting the code (specifically the variable name) to a different language that much more difficult.

Utter rubbish!

HN is language independent!

If you want to port your javascript to C++ then 'var strName' or 'var Name' are equally difficult to convert the the C++ equivalent!

boylesg:
What is it with the coding nazis in here?

Bingo! Godwin's Law.

Check out Torvalds' and Stroustrup's opinions on Hungarian.

I think you'd better arguing for using a better IDE.

GrooveFlotilla:
Bingo! Godwin's Law.

Check out Torvalds' and Stroustrup's opinions on Hungarian.

I think you'd better arguing for using a better IDE.

Well credit where credit is due as they say.

My line has been, all along, is HN if it helps you.

But the coding nazis seem intent on vetoing HN or any suggestion that newbies should try it on for size.

I object to this being parachuted in here. There was a long discussion about this subject, mostly disagreeing with the concept.

IMHO Tutorials should only be posted here when there is a consensus supporting them.

It is not entirely IMHO.

This is (as expected) degenerating into a re-run of the OP's earlier Thread on the same subject. I suggest that this Thread be merged with the other one.

...R

robtillaart:
Think the community is too large for having consensus,

If you tell me that there is a consensus among the Moderators in favour of leaving this Thread here I will, of course, accept that.

However it has not seemed to me that there has been a collective discussion among the Moderators about the appropriateness of anything in this Introductory Tutorials section, or whether the section should exist at all.

...R

At the moment I still use Hungarian for exactly three reasons, judiciously avoiding it for everything else:

To be consistent with an existing code base when doing maintenance.
For controls, eg. "txtFirstName". We often need to distinguish between (say) "firstName" the value and "firstName" the control. Hungarian provides a convenient way to do this. Of course, I could type "firstNameTextBox", but "txtFirstName" is just as easy to understand and is less characters. Moreover, using Hungarian means that controls of the same type are easy to find, and are often grouped by name in the IDE.
When two variables hold the same value but differ by type. For example, "strValue" for the value actually typed by the user and "intValue" for the same value once it has been parsed as in integer.

I certainly wouldn't want to set up my ideas as best practice, but I follow these rules because experience tells me that it occasional use of Hungarian benefits code maintainability but costs little. That said, I constantly review my own practice, so may well do something different as my ideas develop.

Update:

I've just read an insightful article by Eric Lippert, explaining how Hungarian can help make wrong code look wrong. Well worth reading.

But, as I've already said, newbies (which is who this section of the forum is aimed at) can't always tell what they can do and what they have to do.
("I want to append a digit to this variable" aka "I don't know what an array is")

It's simpler not to confuse them at this stage, otherwise they'll tie themselves (and others) in knots trying to explain their code, when one party will read one interpretation into their code, which may well not be the one they intended.

boylesg:
What is it with the coding nazis in here?

kcangfb.png