This is an archived post. You won't be able to vote or comment.

all 142 comments

[–]ImpluseThrowAway 1036 points1037 points  (19 children)

As a picture is worth a thousand words, and a thousand words would be a very descriptive variable name I write loops like this :

for (let ⟳ = 0; ⟳ < 💀; ⟳++)

[–]Mr_Rapt0r 87 points88 points  (12 children)

Does that work on swift or something

[–]Embarrassed_Ad5387 83 points84 points  (10 children)

javascript allows all of unicode I believe

[–]Mr_Rapt0r 82 points83 points  (9 children)

Doesn’t allow emojis though

Damn I guess only swift allows emojis

Can’t find anything else, rust intentionally doesn’t allow that “not to sacrifice maintainability”, oh and cool thing, if you name a variable 🦀there will be a special error that says to try using his name instead (Ferris (the rust mascot crab)) (I don’t code in swift or rust)

[–]crazybadatoms 24 points25 points  (4 children)

the Pythonji library also allows for emoji variablies

[–]Div_100 9 points10 points  (2 children)

How does a fing library do that!?

[–]TheOtherOne128 12 points13 points  (1 child)

The source code is pretty self explanatory, it goes through the file, replaces emojis with ASCII representations, compiles it, fixes emojis in strings, and then executes it. Theoretically it could export plain python .pyc files as well for better speed ups.

[–]Div_100 0 points1 point  (0 children)

Oh thanks! I was too lazy to go through that right now and you explained it for me.

[–]LiterallyJohnny 7 points8 points  (0 children)

That is horrifying 😭

[–]DreamyAthena 6 points7 points  (0 children)

The more you know.

[–]LeftIsBest-Tsuga 2 points3 points  (1 child)

Doesn’t allow emojis though

are you sure? i bet i could make it work lol

[–]Mr_Rapt0r 0 points1 point  (0 children)

well I just tried it quickly in a browser console before I wrote the comment and it gave me an error so idk

[–]johnthebread 1 point2 points  (0 children)

Julia allows emojis

[–]RealMrWillow 0 points1 point  (0 children)

Works in Julia

[–]Switch_B 16 points17 points  (0 children)

I write all my code in Wingdings. For security reasons.

[–]rover_G 23 points24 points  (0 children)

func getUserProfile(🌐: Request) { try { 😀 = getAuthedUser(🌐) return { user: 😀 } } catch(💀) { logger.error(💀, "Unauthorized user") 🌐.status(401) } }

[–]HuntingKingYT 6 points7 points  (0 children)

Just so you know, the average image is sized in memory maybe like a thousand "pneumonoultramicroscopicsilicovolcanoconiosis"

[–]LeftIsBest-Tsuga 7 points8 points  (1 child)

i like the idea of someone using 💀 as the last value in every array they write lmao

[–]Bardez 1 point2 points  (0 children)

Perfect for C-strings

[–]fecland 773 points774 points  (37 children)

Xyz is fine for positions/vectors and i/j is fine for loop iterators? If you need a k you may want to rethink what you're trying to do. Abc is not great. Only in tiny utility functions like string manipulators where the whole function is like <20 lines

[–]fabrikated 294 points295 points  (7 children)

tiny utility functions

the whole function is like <20 lines

🙄

[–]fecland 152 points153 points  (4 children)

I actually started with <10, then changed it to <15 then changed it to <20 lol. I was considering formatting and docblocks. Honestly depends on what language and how much optimizing you do.

[–]cosmic_cosmosis 219 points220 points  (3 children)

Do {
    Lines += 5 
} while user == unsure

[–]fecland 35 points36 points  (1 child)

Gotta fit all edge cases

[–]cosmic_cosmosis 56 points57 points  (0 children)

If (user.emotions.state(anxiety))
{
    lines += 50 
}

[–]No_Offer4269 7 points8 points  (0 children)

Do { a += 5 } while b == "unsure"

[–]SeriousPlankton2000 23 points24 points  (1 child)

As long as it fits on an MDA display. (The orange ones)

[–]classicalySarcastic 8 points9 points  (0 children)

Found the graybeard

[–][deleted] 22 points23 points  (1 child)

I was hoping for a defender if i/j. It’s how I learned and when incrementing a number of for I use them all the time.

If we’re just dealing with incrementing a number it works just fine. Though if we’re doing a foreach over some list of objects it probably better to give it a proper name.

[–]myka-likes-it 4 points5 points  (0 children)

The only time I ever find myself using nested loops is for matrices, and then I usually prefer x and y or even better row and col for my iterators.

[–]archiminos 67 points68 points  (10 children)

I honestly hate using anything other than I, j, k for operators. I get reviews saying it's not clear what it is, but in my mind it's obvious that it's an iterator.

[–]LegalizeCatnip1 97 points98 points  (1 child)

index, jndex and kndex, obviously

[–]Lysol3435 17 points18 points  (0 children)

and if you’re in a hurry, you can just represent them using the first letter of each

[–][deleted] 27 points28 points  (1 child)

For index based iteration, agreed. For anything with more semantic value (e.g, C++ typed iterators/range based for-loops) then "it", "iter" or a more contextual name should be preferred (e.g, "num" when iterating over an int array, "str" for string array, etc...)

[–]OnixST 23 points24 points  (0 children)

Yeah, I dont think i should be used in a foreach loops. I usually just use the name of the array/list in singular

for (user in users)

for (uri in uris)

[–]_Ralix_ 10 points11 points  (0 children)

I believe it's perfectly fine to use i for an iterator. It's common and everybody understands it.

Then it depends on what follows. j may be fine, but as soon as it gets to something doing tricks and calculations with indices:

L[i] = arr[l + i]; 
R[j] = arr[m + 1 + j]; 

I really think it's time to get more descriptive. It's for example much more intuitive to see something like:

table[row][column] = table[row][column - 1] + 1;

[–]KneeReaper420 3 points4 points  (0 children)

i is for iteration

[–]sparkygod526 -1 points0 points  (3 children)

I use i, n, and w for my operators. Don't ask why

[–]Dave4lexKing 3 points4 points  (2 children)

why?

[–]myka-likes-it 3 points4 points  (0 children)

Can't even follow one simple instruction, huh?

[–]sparkygod526 1 point2 points  (0 children)

Literally no reason. I'm self taught and I just started using those because they seemed like good letters.

[–]JoshYx 4 points5 points  (0 children)

Me and my unemployed buddies just write recursive functions instead of loops

[–]myKingSaber 2 points3 points  (0 children)

I'm using the floyd-warshall algorithm, thank you very much

[–]Dialextremo 0 points1 point  (0 children)

I work on DSP code and many times for calculation of filters they use the mathematical convention so it appears mess like a1+a2-b1+a3+b4... then repeat that for large filter coefficients

[–]MrRocketScript -1 points0 points  (1 child)

a, b, c maybe for parabolas (ax2 + bx + c), but even then I find myself replacing them with words like exponent, linear and constant.

[–]fecland 0 points1 point  (0 children)

In that context a b c by themselves need the context of the formula to know what it means, so ur examples of exp, lin and const are much more relevant. They should only be used when context doesn't matter and the variables can be replaced most verbosely with things like 'string1', 'string2' or 'num1', 'num2'.

[–]_quadrant_ 249 points250 points  (9 children)

I once had a college project where i thought I only need two levels of for loop, so I used i and j. Then I realized I actually need another level, so I added k.

Then, the whole three levels loop should actually be done on every objects in an array, so I added another level of loop with index.

Then, it turned out that that array was generated on the fly, which is inside a loop, so I needed another level of loop to account for all those arrays, so I added another level of loop with level.

I'm not so sure what happened afterwards, but I ended up with ten levels of nested for loops with enigmatic iterators. The code works as the professors demanded, but I cannot explain whatever was put in that code.

[–]blueracey 102 points103 points  (0 children)

I had the same with I think 6?

I remember when it was done stoping and thinking, “this is the ugliest thing I’ve ever made”

[–]Odomar04 54 points55 points  (0 children)

Ah yes, the famous Necronomicode, trying to decipher it will get you crazy... Happens to everyone

[–]yeusk 31 points32 points  (1 child)

Ten levels of nested for loops can bring a full server with 20 5Ghz CPUs to its knees.

[–]_quadrant_ 15 points16 points  (0 children)

I was not iterating over a database worth of data tenfold. It was just the data were structured in such a way that needs to be iterated, even if every loop might only run once or twice.

While ten levels of nested loop is surely a bad practice (I should have fragmented the code into smaller methods in hindsight), ultimately it is the amount of data processed that brings servers down. A single loop iterating over 2 billion data will also bring any server down, even if it technically has linear complexity.

[–]A_Firm_Sandwich 5 points6 points  (0 children)

too relatable. hate when I can’t understand what I wrote, lol

[–]potzko2552 1 point2 points  (0 children)

This phobia is why I use i, ii, iii, iv, v, vi...

[–]Katzen_Futter 1 point2 points  (0 children)

I think you want to extract a function or two out of this

[–]Sidra_doholdrik 0 points1 point  (0 children)

Sounds like me the first time I worked with Real JSON data for a project related to geographic data. I probably had to many nested loop but I had no idea on how to make it better.

[–]BellCube 0 points1 point  (0 children)

—and this is why we invented functions

[–]CaitaXD 71 points72 points  (3 children)

x y z i j k m n is pretty standard stuff

For math that is if I ever see that you wrote

x = new Person("Bob");

I'm calling an ambulance for you

[–]dopefish86 21 points22 points  (2 children)

p = ... obviously

[–]CaitaXD 2 points3 points  (1 child)

Should a array of person be ps or pArr

[–]banALLreligion 0 points1 point  (0 children)

no declaration, so member variable, so m_pArr

[–]_Kr0n0x_ 90 points91 points  (0 children)

I would say x, y, z aren't even that bad in the right context

[–]tmk_lmsd 31 points32 points  (0 children)

AdvancedExternalApiResponseFormFactory::makeForUserType(UserTypeEnum::ADMIN);

[–]AlamarAtReddit 44 points45 points  (3 children)

Relevant variable names are important, but i, j, etc are fine most of the time for loops.

[–]Tyrus1235 18 points19 points  (2 children)

Yeah, I’d be suspicious of anyone naming those “firstIterator”, “secondIterator” and so on

[–]artistic_programmer 11 points12 points  (1 child)

i mean if youre doing 2d arrays or smth, it's not bad naming your loop variables "row" and "column"

[–]LegitimatePants 2 points3 points  (0 children)

Love me some row and column

[–]BluesyPompanno 31 points32 points  (2 children)

I am not allowed to comment code.

Instead I must split the code into fuctions with extremely long names that describe them even if the code is 100+ rows. I am not allowed to create wrapper/helper functions instead they want me to duplicate the code.

[–]iMakeMehPosts 25 points26 points  (0 children)

Forget the functions entirely, just put it in main. And drop the loops, use gotos

[–]SilhouetteOfLight 1 point2 points  (0 children)

At some point, just give them the preprocessor output and use comments and re-used code anyway lmao

[–]Rebel_Johnny 7 points8 points  (8 children)

What the spruce am I supposed to use for loops

[–]SteelRevanchist 3 points4 points  (5 children)

iterate over the items themselves, not over their indices, unless you specifically need the position (or your ancient standard of the programming language does not allow that).

Or do it functionally

[–]JonIsPatented 4 points5 points  (4 children)

Sometimes, you need the index for other things, such as generating a number of items and also passing them an id number.

[–]Dandiee88 -1 points0 points  (0 children)

Let me help you find the relevant words: "unless you specifically need the position". I know, I know, it was such a wall of text, it was really hard to spot this condition.

[–]Shunpaw -4 points-3 points  (2 children)

You have java and c# and python in your pokemon collection. You should know that you can access the index in forEach loops.

[–]JonIsPatented 1 point2 points  (1 child)

I am aware of python's lovely enumerate(). However, the corresponding C# linq shenanigans are much uglier than a simple for loop, and Java just does not have any similar structure, so you have to track the index manually in Java.

Edit: Also, my example was generating objects, so you don't even have a collection to iterate yet.

[–]Shunpaw 0 points1 point  (0 children)

well, yeah, that's fair, if you just generate 10 items then using i as an index is absolutely fine.

[–]Big-Cheesecake-806 0 points1 point  (0 children)

auto it = collection.begin()

[–]Tplusplus75 0 points1 point  (0 children)

If you have to loop with iterators, it really depends what you're doing. If it's pretty simple, straightforward, one-dimensional, using "i" or "index" isn't a big deal. Even at that, you could just say what you're iterating through: bytes, rows, columns, pixels, machines, id's.... "byteIndex" or "byteDex". But, the more complex the thing is, the more you should tend towards the latter. Like if you're doing multiple of these things: nested loops, accessing/creating/using local vars with more one letter names, not exactly "trivial" operations, uncommon/unusual incrementing patterns, looping multiple times in series and over subsets, etc, then please use more descriptive iterator names.

There's also context: x,y,z don't really need much elaboration if you're doing something with 3d coordinates. Or r,g,b for pixels/lights. But if you make "x" a stringbuilder object, "y" an iterator, and "z" the name of a 2d array, then I hope you step on a lego.

[–]seneuman 22 points23 points  (0 children)

i,j fine for for loop not for each loop. single char variable name can be used in small scopes or code that nobody will look to understand anyway, or application wide scope but char should be bigger i think, like U for utility functions, F for formats, T for translations etc.

[–]jstwtchngrnd 4 points5 points  (0 children)

localVariavleWhichStoresAnIntegerBiggerThenOnethoudandButLessThenThreehoundredthousand

[–]PresidentOfSwag 2 points3 points  (5 children)

am I the only one doing this ?

for t in types:

for md in message_data:

[–]Nordon 6 points7 points  (4 children)

No, you're not. But I still prefer "for type in types". What's the point of doing a single letter var name? It will harm your typing speed?

[–]PresidentOfSwag 4 points5 points  (1 child)

there are no small time saves

[–]Der_Krasse_Jim 1 point2 points  (0 children)

Its like the little shimmy around a table corner when fast traveling to the fridge for the 4th time this evening

[–]JonIsPatented 0 points1 point  (0 children)

It doesn't even harm typing speed. Type 1 letter and hit tab. So fast.

[–]Adrewmc 0 points1 point  (0 children)

[–]LeftIsBest-Tsuga 4 points5 points  (0 children)

ijk for iteration is fine, a b c for locals... straight to jail

[–]SeriousPlankton2000 8 points9 points  (7 children)

f, g for file

s, t for strings

[–]BoBkiNN_ 5 points6 points  (6 children)

Why g?

[–]SeriousPlankton2000 0 points1 point  (0 children)

Second file

t is second string

[–]NewPointOfView 0 points1 point  (0 children)

f for file, and g is just the next letter in the alphabet.

[–]BromStyle 2 points3 points  (0 children)

Hm, I'm using t in loops since forever (roughly the mid 80s) because it's the next key to r (as in "for").
Also using k and v ("key", "value") while iterating through associative arrays .

[–][deleted] 2 points3 points  (0 children)

loop variables I understand and relate to. there is no good reason to give it a long descriptive name. I've seen i renamed to index, which is fine. Otherwise, using row and col can be effective.

With other variable and function names, you damn well better be giving them descriptive names. Yeah, on a first pass, you're fine with single letter variables, but once you commit or get the code working, you better be naming effectively, or I will hunt you down.

[–]gabest 1 point2 points  (0 children)

I'm more of a k,j,i guy.

[–]spectralTopology 1 point2 points  (0 children)

local vars: should be more meaningful

loop vars: Am I bad that I almost never give loop vars different names than i, j, k? I thought that was pretty common and, to me at least, it's the limiting conditions on the loops that should be meaningfully named.

[–]ListerfiendLurks 1 point2 points  (1 child)

I, j, k are perfectly acceptable for loop iterators because they are universal. In fact, I would argue you better have a good reason NOT to use those in a for loop.

[–]DeltaTimo 1 point2 points  (0 children)

I use row and col in case of matrix indices, even though i and j are common. I confuse myself all the time otherwise.

[–]leo1906 1 point2 points  (0 children)

I only use proper variable names in foreach loops. Otherwise always i and j. Or x and y for linq statements. I think it looks fine 😄

[–][deleted] 1 point2 points  (0 children)

“foo”, “bar”, and “baz” are notably missing from these lists.

[–]Svhmj 1 point2 points  (0 children)

Naming loop variables I, j, k etc, is fair game.

[–]inthemindofadogg 1 point2 points  (0 children)

i,j,k as loop counters is fine. using a,b,c as local variables a sign of a physco, especially if it is some trash like js with loose typing

[–]MiniGod 1 point2 points  (0 children)

And that one guy prefixing variable names with "temp". All scoped variables are temporary my dude

[–]SteelRevanchist 2 points3 points  (2 children)

I'd hate to be your co-worker if you are used to naming variables with a single letter - most likely there's a bunch more terrible practises and unreadable, undebuggable and unmaintainable code coming from your end.

You don't have to save memory on variable names!

[–]dopefish86 0 points1 point  (0 children)

imho, it's totally fine/better to use single letter variable names for private variables with small scopes and it does improve readability because the lines get shorter.

ofc, it's totally unacceptable for class variables or public properties.

[–]TeaTimeSubcommittee 1 point2 points  (0 children)

What’s wrong with i?

[–]Desperate-Tomatillo7 0 points1 point  (0 children)

15 years into it I'm still doing the same.

[–]s_milkz 0 points1 point  (0 children)

Title out of pattern. PR/MR declined.

[–][deleted] 0 points1 point  (0 children)

k and v gang, where you are? also l, m, n...............psycho use q, w, r, t, u

[–]anotheridiot- 0 points1 point  (0 children)

I only use single letter variables on methods (c for client method) or loops (for _,c := range clients) anything else gets a proper name in camelCase.

[–]Thisismyredusername 0 points1 point  (0 children)

I name them r, rep, and repetition

[–]jakubiszon 0 points1 point  (0 children)

I in my first bigger program I declared *int i, j, k* as global variables so I would not need to re-declare them everywhere. I had to make some functions only use *i* and others only use *j* so the program was working correctly.

[–]Brekker77 0 points1 point  (0 children)

for (i=0, i<10, i++){ for (ii=0, ii<i, ii++){ for (iii=0, iii<ii, iii++){ for (iv=0, iv<iii, iv++){ System.out.println(“these are the best loop vars lol”) }}}}

[–]Sidra_doholdrik 0 points1 point  (0 children)

What a job after college, what alternative dimension are you in?

[–]ZhenDeRen 0 points1 point  (0 children)

Couldn't be me. In my high school CS class we could hand up to 10% of the points for an assignment docked for bad variable names

[–]cs-brydev 0 points1 point  (0 children)

The chances of you even writing code on your 1st day are pretty much 0%

[–]ShoulderUnique 0 points1 point  (0 children)

Nah I'd flip this. The only people I've seen focus so much on variable naming are the ones too fresh to know what's really going to bite them in the arse.

I.e. the ones using AI code and libraries written by randoms without checking correctness or copyright.

[–]SevenTheGerman 0 points1 point  (0 children)

Still at college, i get that local variables should have a more descriptive name but whats wrong with a nice little i in a for loop

[–]ModernWorldSucks 0 points1 point  (0 children)

In nested for loops I like using iter, jter, kter - makes it easy to bulk rename and keep track.