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

Show parent comment

0

u/[deleted] Jul 29 '14

[deleted] — view deleted comment

0

u/[deleted] Jul 29 '14

[deleted] — view deleted comment

-10

u/StopThinkAct Jul 30 '14

99.99%? I guess I'm accounting for 100% of that .01% because I've never used "i" as a loop variable except in college intro courses.

If I see it I change it to be something that describes what it's iterating.

8

u/thestoicattack Jul 30 '14

What if I'm iterating over, say, vector elements? index? Something like v[i] is obvious and has been standard mathematically for a long time. (We even call it array "subscripting.")

→ More replies (13)

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 :)

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.

1

u/[deleted] Aug 21 '14

Jesus, just search and replace with that "full word only" thingamajing.

1

u/na85 Jul 29 '14

oh shit

how long have I been coding and never thought of that.

fuck.

7

u/Whadios Jul 29 '14

Have you ever in that time needed to search and replace the counter var in a loop? I've been coding for 20 years and don't think I've ever ran into a case where I wanted to do a search and replace like that.

For one loop code that uses the counter is rarely ever long enough that changing it would be hassle. Secondly most IDEs should support refactoring.

1

u/rlamacraft Jul 29 '14

Can you not just do a search and replace on "i ", rather than just "i" to replace the instances of i as it's variable name rather than its instances within other words?

3

u/Whadios Jul 29 '14

About the most dangerous thing you can ever do is start doing search and replaces trusting pulled out of your ass rules that you hope will isolate what you want. Do you really trust that nobody had a variable name or function that ended in i in that file?

Now you can say you'll use replace and step through each instance rather than replace all so you're safe but we both know you'll grow confident and laziness will take over and there will come a time when you start using replace all to 'save time'.

2

u/Decency Aug 07 '14

Fails for array[i], function(i), etc.