Main Content

Hungarian, Camel, Snake and Kebab - variable naming conventions

Archive - Originally posted on "The Horse's Mouth" - 2016-01-03 12:03:54 - Graham Ellis

In my program, I'm going to use a variable to hold the number of people (in a group or in a category) perhaps. What should I call that variable?

There are a whole lot of different conventions available to you - here are some I have used / come across

Camel case: numberOfPeople
A series of words, with each intermeidiate word started with a capital letter. Called "Camel Case" because the capital letters make it look like the humps of a camel.

Kebab case: number-of-people
Hypehated words - like chunks of meat or vegatables on a kebab skewer. Note that cannot case only works in a gew languahes such as Tcl and Perl 6, as the minus sign most usually is the subtraction operator.

Snake case: number_of_people
Words separated with underscores - the word snakes along past the underscores. Unlike Kebab case which is of limited (language) use, you can use Snake Case with most modern languages.

Hungarian (Systems) notation: iNumberOfPeople
In hungarian notation, you preceed the variable name with an additional character or characters to indicate the type of variable - in Hungarian Systems notation, that's an indiction of the internal data type such as i for Integer. You can see the developemnt of Hungarin Systems Notation from old Fortran conventions where variable names starting with I J K L M or N were integers, and others were floats.

Hungarian (Apps)) notation: cntNumberOfPeople
Prefixing the name of the variable with character(s) to indicate the use make of a variable within the application - in my example, I have used cnt to incicate it's a counter ... and in my old Fortran programs you'll find "JPxxxxx" all over th eplace to indicate an integer pointer.

Shortening and English / American
Should you use init, initialise or initialize as the name for a piece of setup code? Some languages will force a built in standard on you for such specials, but I strongly recommend that you use your own convention (for your organisation) rather than mixing them up. There's nothing quite so frustrating as typing to debug a piece of code where sometimes the word colour is used, and at other times the word is color.

Although I've not given you any particular recommentation between camel and snake case, nor whethe to use Hungarian prefixes, I am going to strongly recommend that you use one of the two - with additonal hungarian prefixes if you wish. The consistent use of such a notation makes much lighter work for the maintenace programmer, and the people of Pen Island will tell you they have been misread and misunderstood as to what they're about when their name's been written out without separators in a single case ...