How do you usually write variables when programming?

Hi! I haven’t been programming for a long time, I have a question, maybe it’s silly, but I would like to know if in general people tend to write variables when they are more than one word, like costDrone or like cost_drone, or if they you write differently.
This year at the university has been my first contact with programming and I have gotten used to writing variables in the form myVariable, but nevertheless in the Codecademy courses they write them in the other way.
And you, how do you write them?

Good question, for which you’re not going to like my answer: There is no set rule for variable name convention, but most languages won’t let you start a variable name with a number, or use special characters anywhere in the name. A shady answer since I don’t know very many languages, and none that well.

In the JavaScript world, and correct me if I’m wrong, it was NetScape or Mozilla who adopted camelCase for naming properties. (One suspects Eich or Crockford would have an answer.)

Eg. background-color in CSS is backgroundColor in JS.

With that the style trended into production code and text books so we find that most people who code in JS, use camelCase variable names. It is common in C#, as well. Can’t speak for Java or the other C’s.

Python and Ruby users generally use snake_case as the underscore is permitted (by most languages, even as the first character) and it lets us read the words apart.

Bottom line, it really comes down to what language is the flavor of the day (or course). Nobody is going to object if one uses snake_case in JS or C, or camelCase in Python, Ruby, et al, it will just look weird, I guess?

As for ‘me’, if I said that everything is true to form and consistent, I couldn’t be certain. Either form is comfortable to work with, and I generally conform to the group convention, so camelCase in JS and C#, snake_case in Python and Ruby, and dashes only ever in CSS. Not sure any language allows the dashes.

2 Likes

Curiously, different languages have different standards.

For python it’s mostly lower_underscore, with camel case for ClassName. So, def do_thing(thing_num)

For C#, it’s void DoThing(int thingNum) {.

For JavaScript, usually function doThing(thingNum) {.

Older languages like C are kind of the wild west, where you pick a style and stick with it, Java uses a C variant close to C’s K&R or 1TBS. However, this dictates brace intention more than naming. With Java coming out in the extreme camel case camp.

For naming in the ancient world, there is a nasty artifact you might find called Hungarian notation. Types simply don’t fit neatly enough in most languages for this be be anything but cumbersome, but the camel case variable thing did seem to stick.

For me, unless it’s python or some other language with explicit opinions, I’ll be using some kind of camel case.