r/dailyprogrammer • 1 3 • Jul 28 '14

[Weekly #4] Variable Names

Variable Names:

We use variables a lot in our programs. Over the many years I have seen and been told a wide range of "accepted" use of names for variables. I always found the techniques/methods/reasons interesting and different.

What are some of your standards for naming variables?

Details like are they language specific (do you change between languages) are good to share. Or what causes the names to be as they are.

Last Week's Topic:

[Weekly #3] (http://www.reddit.com/r/dailyprogrammer/comments/2bbvqp/weekly_3_favorite_data_structure/)

25 Upvotes

64 comments sorted by

View all comments

35

u/skeeto -9 8 Jul 28 '14
  • The larger the scope/namespace in which a variable/name resides, the longer and more descriptive its name should be. A global variable might be named open_database_list and, at the other extreme, short-lived loop variables are single letters, i, j, and k.
  • Follow your language's accepted style when it comes to CamelCase, snake_case, etc. In C it's generally snake_case. In Java and JavaScript it's CamelCase. In C++, Ruby, Python, and many more it's CamelCase for class names and snake_case for most other things. My personal favorite, though, is Lisp's dash style (with-open-file, first-name), where it's not a syntax issue.
  • I personally avoid shortened names (str instead of string or len instead of length), though there are exceptions. Full words are easier to read, especially if you're using a well-designed domain specific language that reads similarly to natural language.
  • Unless your language prohibits it (e.g. C89 and before), declare variables close to their first use. Also, don't choose a name that masks more widely-scoped variables. Following my first point above helps prevent this from happening.
  • In languages without good namespace support (C, Elisp), mind your namespaces when it comes global names. Prefer consistent prefixes (e.g. pthread_*) as a way to group your identifiers.

0

u/[deleted] Jul 29 '14

[deleted] — view deleted comment

6

u/ex_pc Jul 29 '14

You should never use something like 'i' or 'j' for a widely used variable(something that you might need to look up later) anyway.

3

u/Kaltiz Jul 29 '14

This. Variables that you name i and j shouldn't be used in bigger scopes and probably will be used multiple times, defeating the purpose of them been easily searchable.

4

u/KumbajaMyLord Jul 29 '14

What kind of of barbaric language are you using that you are doing manual search and replace instead of a proper tool assisted refactoring?

2

u/Whadios Jul 29 '14

Just to answer your question though I don't support the idea of ii and jj:

C++ Builder. Fucking thing is a piece of garbage and refactoring just does not work in the thing. Maybe they finally fixed it in newer versions (didn't work in 2007, doesn't work in 2010) but I haven't seen it. Such a shit IDE overall :(

2

u/XenophonOfAthens 2 1 Aug 01 '14

Some of us like to use vim :)

→ More replies (1)

2

u/[deleted] Aug 05 '14

Why would you search and replace 'i'?

3

u/newpong Aug 21 '14

well, this probably doesn't happen too often, but i actually remember having to do this before. i, j, and k are often used in physics as the unit vectors for the x, y, and z axes, respectively. I was working on some sort of computational model that was initially one-dimensional, so i and x were the units I chose, but I didn't really plan ahead or have any expectations of where I might go with it, so when it was extended to 2 then 3 dimensions, the labels didn't make sense with what was conventionally taught, so I swapped the i's and the k's to re-orient the system

2

u/sagequeen Jul 29 '14

That's really smart. Was doing a project in C that was suuuuuuper messy as a final lab, and we had some global variables named x and y, as well as nested for-loops with x and y for a 2-D array, and sometimes just because we felt like it we used i and j instead for temporary variables in subroutines. About 75% through, my partner and I realized that the code was just confusing, so all of our x's and y's in the for loops were changed to i and j, but we had to make sure that it wasn't an instance where we wanted the global variable x and y. Took such a long time, would not recommend, follow this guys advice and do ii jj etc. Definitely learned a lesson from that one.

1

u/[deleted] Aug 04 '14

I don't know about other ide's but in eclipse, the ide handles variable name changes

1

u/blaine64 Aug 18 '14

It's easier to do a search or search and replace on "ii" than on just "i".

Can you explain further? This doesn't seem justified. How would it be "easier" ?

0

u/[deleted] Aug 18 '14

[deleted] — view deleted comment

2

u/blaine64 Aug 19 '14

Typically, all the "i"s will be in a loop. That's the point everyone's trying to make.

→ More replies (1)
→ More replies (6)

5

u/MotherOfTheShizznit Jul 31 '14 edited Jul 31 '14

Follow your language's accepted style when it comes to CamelCase, snake_case, etc.

Holy crap, I've found the one other guy in the world who follows that!

In C++ [...] it's CamelCase for class names.

Wait, what? No, it isn't... e.g. unordered_map, not UnorderedMap.

Edit: Actually, what happened here is that I read "accepted" as "implicit" when you meant "popular". Well, I'm off to being alone in the universe again.

1

u/skeeto -9 8 Jul 31 '14

I pick an established style guide and follow it. In the case of C++ I default to Google's style guide. It specifies CamelCase for type names.

Type names start with a capital letter and have a capital letter for each new word, with no underscores: MyExcitingClass, MyExcitingEnum. The names of all types — classes, structs, typedefs, and enums — have the same naming convention. Type names should start with a capital letter and have a capital letter for each new word. No underscores.

This matches Stroustrup's own style. While it's unordered_map not UnorderedMap, the distinction is that this is a standard library class while the CamelCase convention is for your own defined types.

2

u/MotherOfTheShizznit Jul 31 '14

the distinction is that this is a standard library class while the CamelCase convention is for your own defined types.

But why? Why is C++ the only* programming language in the world where that distinction is made?

* or one the very few

1

u/skeeto -9 8 Jul 31 '14

When I'm writing C I stick to snake_case for type names because that how K&R did it, and it's also how I would prefer it anyway, even in C++. Most programming style conventions are arbitrary and are only important for the sake of consistency among many developers, so there's usually no real reason for any particular style decision. It's just what everyone else is doing.

→ More replies (1)

1

u/Rapptz 0 0 Aug 23 '14

I wouldn't use Google's Style Guide as a good example of a C++ style guide. Boost uses snake_case and the standard uses snake_case as well. It makes your code look consistent with the standard library. This is something that PEP8 encourages for Python, so I'm not sure why you're not encouraging it for C++.

1

u/Maping Jul 29 '14

Spot on for me. Java is the only language I know, and I use CamelCase (lowercase first letter, though). I try to make them descriptive but for small programs or short-use variables, I usually don't bother, going with a single letter, abbreviated name, or just a less descriptive one (num instead of numOfMiles).

2

u/valdus Jul 29 '14

In PHP we refer to that as "camelCase" (humps are in the middle), while having the first letter also capitalized is StudlyCaps.

1

u/Foxtrot56 Aug 01 '14

This is great, I have no idea why this is never mentioned in any CS class I took.

1

u/Coder_d00d 1 3 Aug 01 '14

Part of the origin of my reddit user name comes from a joke on myself that I use to always just snake_case all my variable names. Nice summary on variable names!