r/ProgrammerHumor • u/khanshahvarem • Nov 12 '20
Removed: Repost It's the law!
[removed] — view removed post
1.1k
u/boboprobo Nov 12 '20
i is iterator, j is jiterator, k is kiterator
75
25
16
u/murtaza64 Nov 12 '20
index and jndex
9
u/calcopiritus Nov 12 '20
for index, i in enumerate(list): for jndex, j in enumerate(ljst): for kndex, k in enumerate (lkst):3
3
→ More replies (37)10
u/progrography Nov 12 '20
o is offender, p is predator, q is queer, r is [removed], s is seducer, t is transformer.
→ More replies (3)5
436
Nov 12 '20 edited Nov 12 '20
It's just like mathematicians being obsessed with x,y,z i,j,k a,b,c f,g,h t,u,v,w m,n p,q,r
232
Nov 12 '20
α,β,γ ε,δ π
161
26
u/tod315 Nov 12 '20
Excuse me what kind of deranged mathematician would use π as a variable
→ More replies (11)10
→ More replies (1)6
36
u/YOBlob Nov 12 '20
When in doubt, use phi
41
u/SilverDem0n Nov 12 '20
I like to assign any huge constants as epsilon to annoy colleagues
→ More replies (10)5
18
u/chepas_moi Nov 12 '20
No, it's more than that. i, j, k are for integers where x, y, z are for floats. Knuth said so.
16
u/Cyberwolf33 Nov 12 '20
A small table of common uses for those interested (source: math graduate student)
x,y,z: Real variables, generally in a function.
i,j,k, (l if desperate): indices for sums, products, and general collections of things. Capital I,J,K are often ideals.
a,b,c (and others): simple variables (like in a small system of equations ) or geometric objects/angles (angles uppercase, sides lowercase)
f, g, h: functions, or more generally, morphisms. Capital G can also be a group, and H a subgroup.
u,v,w: first use, linear algebra vectors. Capital U, V, W for the vector spaces themselves. Also work as desperation variables.
m,n,p,q,r: Integers. m,n often the upper index of a sum or product, and used when referring to addition of integers. p, q often used for multiplication. r for radius of a sphere too
Bonus Greek letters! (Lowercase unless specified)
(Var)phi, psi: A morphism
alpha, beta, gamma: Elements of something, often when the above letters are exhausted elsewhere or certain lead to confusion.
epsilon, delta: smöl bois.
pi: pi.
Pi (uppercase): Products
sigma, tau: a permutation (a bijective function from a set with some amount of elements to itself)
Sigma (uppercase): sums
theta: an angle
lambda: Eigenvalues of a matrix
zeta, xi: “Jesus I have so many variables floating around what the hell do I even call this?!? UGH FINE XI IT IS”
Edit: And a variety of others that I have not thought to include. Feel free to add or contest, these are just what I’ve regularly seen
→ More replies (2)29
u/SilverDem0n Nov 12 '20
You can use emoji for variable names in a bunch of languages. You can pick an emoji that has a different semantic meaning than the purpose of the variable if you want to be particularly awkward, and I am sure you do.
48
9
→ More replies (9)4
181
u/WowSuchEmptyBluh Nov 12 '20
I could do k. Don't you dare to ask for any more.
78
u/Snykeurs Nov 12 '20 edited Nov 12 '20
Also use k, v for dictionary
47
u/nubenugget Nov 12 '20
If it's a Map (or dictionary in Python) I use "key" and "value" or something based off of what is stored as key/value
16
Nov 12 '20
Nested loops are the future. I’m sure Intel’s next processor will be able to take a billion functions pushed onto the stack for each onClick event handler!
6
4
u/mostlyBadChoices Nov 12 '20
while(k < MAX_VALUE) { for(int k1=k; k1 >= k; k1++) { int k2 = k1*k; for(int k3=0; k3<k1 && k3>k2; k3++) { ... } ... } }
81
u/saschaleib Nov 12 '20
Actually an interesting point: what other "default" variable names are you guys using?
Like, if I'm iterating over objects, I always call them "it", as in (pseudocode):
for each it in objectarray
if it is null ...57
u/DoktorMerlin Nov 12 '20
i mostly use "item" if there is no other obvious name, but mostly it is like
foreach(phoneState: PhoneState in phoneStates) if(phoneState != null)16
u/Khaare Nov 12 '20
That kind of naming quickly gives me semantic satiation and I get dizzy.
→ More replies (2)13
u/Eolu Nov 12 '20
iis the only default variable name I use that I can think of. For the kind of iterator in your example I always give a meaningful name.
for each balloon on balloon_array
iseems reasonable to me because an index is an int counter no matter where you use it, whereas theitis a different thing depending on what’s being iterated through.→ More replies (1)39
u/nubenugget Nov 12 '20
Iterator > iter
Random > rand
Anything that I'm using to store an object/value in temporarily > temp
→ More replies (3)8
u/Embr-Core Nov 12 '20
Wait are you intending me to read this as “iterator is better than iter” or “use iter for the iterator variable”
→ More replies (1)6
u/n0tKamui Nov 12 '20
funny thing, because in Kotlin, the default single parameter name of a lambda is
it(0..10).forEach { println(it) }
is strictly equivalent to
(0..10).forEach { x -> println(x) }
→ More replies (5)3
u/Superbead Nov 12 '20
I sometimes use f (file), o (generic object), s (string), r (return value) and v (value) to keep verbosity down, especially in short functions or as the only obvious parameter to a clearly-named function, but only where it's unambiguous.
If I had two files in the same routine, I'd give them clearer names.
→ More replies (1)→ More replies (19)3
u/trezenx Nov 12 '20
that's actually pretty clever maybe it will help me with my loop-struggles, thank you
73
u/Nall-ohki Nov 12 '20
i is fine as an index variable, not as a value.
One letter variable names are appropriate to sub-function scopes such as loops where their context is entirely grokable at a glance.
7
→ More replies (4)7
u/dpash Nov 12 '20
The other acceptable use is if the domain uses those names. Coordinates and scientific equations spring to mind.
But then where possible, coordinates should be in a point class so there's extra context attached. And as you say, scope should be limited.
→ More replies (1)
24
u/drdrero Nov 12 '20
I rarely use for loops, but when I do, it is related to the amount of iterations and not directly the iterated object. Therefore, i is fine.
16
20
Nov 12 '20
In my next life, will name my first daughter Eye, and the second Jay.
→ More replies (2)9
u/devils_advocaat Nov 12 '20
Planning on the snip before Kay, Elle and Em arrive?
→ More replies (1)
49
u/ManuelRav Nov 12 '20
I use i, ii, iii etc. I used to use k, kk, but the third one didn’t look to good when reviewed...
17
5
6
11
u/Nuroyun Nov 12 '20
reposted something didnt even change the title u/khanshahvarem
https://www.reddit.com/r/ProgrammerHumor/comments/gxm3af/its_the_law/
→ More replies (2)
81
u/olafurp Nov 12 '20
I use index because I hate single letter variables
186
Nov 12 '20
Usually I'd agree with you but in
for (int i = 0; i < limit; i++)writing "index" or "iterator" instead of "i" doesn't add any useful information.
It's such a universal snippet that everyone will immediately know what
iis.But if someone writes
Class A
{
private int i;
}then fuck that person.
9
→ More replies (2)27
u/TSPhoenix Nov 12 '20
It isn't about adding information, it is so when I CTRL+F my loop variable I get only my loop variable. Yes I know a better IDE solves this, but we don't always get to choose.
→ More replies (3)57
u/nasaboy007 Nov 12 '20
If your loops are long/complicated enough that you need to ctrlF your index variable, that sounds like a code smell itself.
→ More replies (1)14
3
u/renrutal Nov 12 '20
The length of a variable name is usually proportional to its scope.
I'd use i in small scopes and index in slightly wider ones.
→ More replies (2)3
u/dpash Nov 12 '20
iis so ingrained for loop indexes, in okay with using it in just those situations. Also acceptable if the domain typically uses that letter (xandyfor coordinates or in scientific equations).But list iteration (
foreachetc) is preferable to indexes if your language supports them.
25
u/wunderbraten Nov 12 '20
m and n are also some nice variables, especially for looping inside of a loop
→ More replies (4)3
u/ShlomoPoco Nov 12 '20
I like x and y for 2d arrays, because my brain is cursed and turns matrices to coordinates system.
a[y][x] is like the point (x,y).
I might be abit weird tho
7
6
u/_Screw_The_Rules_ Nov 12 '20
I like to use x, y, n and i the most
2
u/MadMax2230 Nov 12 '20
x y n and i can be rearranged to spell n i x y
nixy is a misaddressed or illegibly addressed piece of mail
motherfuckers have alleged voter fraud with mailed ballots
twitter is a social media platform
reddit is a social news aggregation website
your username is screw the rules
trump also likes to screw the rules
and the working class
your comment is a subliminal message
by the bourgeoisie
trying to sway my modus operandi
and i will not take
the mind control medicine
the doctor prescribed me
begone thot, and may we be rid of your devilish variables
6
u/Cley_Faye Nov 12 '20
Me: "Ok enough of this, now we'll properly name loop variable for what they mean to improve readability"
Me, a bit later: "Screw that, I'm not coming up for another name for that, everyone knows 'i' anyway"
17
u/checkeredfire Nov 12 '20 edited Nov 12 '20
Others: why do you always use i,j variabes in loops? Programmers:
[Photo of Mr Moseby from Zach and Cody, speaking to London Tipton, with his mouth open looking passionately frustrated.]
ITS THE LAW
I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!
→ More replies (2)3
5
u/Pixelmod Nov 12 '20
At best, I find i acceptable solely for self-contained loops with no nesting and no use of the variable outside the loop. And then again I'd advise using index at the very least.
If you have nested indexes, please give them proper descriptive names. Pseudocode:
for (let categoryIndex = 0; categoryIndex < categories.length; ++categoryIndex) {
const category = categories[categoryIndex]
for (let itemIndex = 0; itemIndex < category.items.length; ++itemIndex) {
// Do your thing
}
}This isn't math class, readable code is more important than your keystroke-saving shenanigans anyways. If you're too lazy to type your variable names completely, get an autocomplete. We have plenty of those, and some even have AI that can guess your var names for you.
2
u/TropicalAudio Nov 12 '20
This does depend on the type of nesting. In image analysis, you sometimes want to iterate over different dimensions, but those might not always correspond to rigidly oriented x/y/z/t data. If you use x/y/z in a loop that gets fed rotated data, debugging can get really confusing, as your x might correspond to t, y to z and z to x, or any other combination. If your input can be an arbitrary dimensional slice in an arbitrary orientation, i/j/k iterators will be just as clear as dim1/dim2/dim3.
→ More replies (1)
9
u/wrossi81 Nov 12 '20
I learned C++ in high school and Java in college, and wasn’t a fan of the latter. When I had to write code in Java I would use c as my iterator, so the for statement said c++.
4
4
u/JuanGee1G Nov 12 '20
I always thought of them representing the vector components i, j, k, etc. because of how they can index arrays/vectors/matrices/lists.
3
3
Nov 12 '20
We also don't use A or B as drive letters anymore. But that's more to honor our fallen drive comrades.
→ More replies (1)
3
u/hessenic Nov 12 '20
Afaict its a hold over from the days where storage was EXPENSIVE. There's a lot of older code that has abbreviations because the cost of learning the abbreviations and contractions was much less than the cost of storing the source code.
→ More replies (1)2
u/SilverDem0n Nov 12 '20
Some early language implementations only used the first couple characters in identifiers and ignored the rest - for space/speed like you say. Some of the 80s BASICs just used the first two characters of any variable name at most, and ignored the rest.
And small terminals don't have much space to waste on long names.
→ More replies (2)
3
2
2
2
2
2
u/RaghadR Nov 12 '20
If it’s a single loop, then I use ‘n’. If it is 3 or more level nested, then I use ‘i1’, ‘i2’, ‘i3’,...
But yes, for 2 level nesting, i j is all time favourite.
2
u/ribbonsofeuphoria Nov 12 '20
Why was Dijkstra so good at algorithms?
His name has i, j, and k in it.
You will never misspell Dijkstra again. You're welcome.
→ More replies (1)
2
2
2
2
u/SilverDem0n Nov 12 '20
CS came from mathematics, and mathematicians use these single letters for indices as they have to write them on blackboards.
Also, early languages and implementations ignored anything beyond the first few letters of identifiers, so longer identifier names may have actually clashed to the surprise of programmers.
1
u/injuomatic Nov 12 '20
Because in Fortran you've to add 'implicit none' to use other letters as intenger
1
1
1
1
1
u/braytag Nov 12 '20
it's the law, unless I'm doing cartesian for my game (ex.: grid), then it's x,y.
1
1
u/BlackTorr Nov 12 '20
When i started coding i would use i, n, or x, in an inconsistent mess, now i use ndx short for index, witch has (almost) all 3.
1
1
1
1
u/GeneMayo Nov 12 '20
ijk might come from BASIC if not earlier. They are used as iterator named for more than three decades, IIRC.
→ More replies (1)
1
1
1
1
u/Sexual_tomato Nov 12 '20
I always thought it was because I, j, and k usually denote the basis vectors in 3d space when doing matrix math, and most early computing was done by mathematicians who would visualize a 3d array as a cube.
1
1
u/erickweil Nov 12 '20
I don't use j, just i and k.
If is 3 nested loops, x,y and z.
And if more than 3 nested loops, I quit.
1
1
1
u/Saihardin Nov 12 '20
i, j, k
And if one still requires more then for some reason we go back to a, b, c
1
u/MarryUhna Nov 12 '20
Except for when working with coordinates, then you obviously use
x, y, z. Anyone else is a maniac
1
1
u/beardMoseElkDerBabon Nov 12 '20
I always break the law. r for rows, c for columns. x,y,z,w... iWhatever rWhatever cWhatever xWhatever ...
1
u/MyHamburgerLovesMe Nov 12 '20
It comes from Mathematics where "i" is used in Summation notation and also used that way for collections of things, like if you have a bunch of variables x1, x2, ... xn, then an arbitrary one will be known as xi.
1
u/jm434 Nov 12 '20
So if anyone has taken physics, they know that i,j are popular variables as well.
If anyone has done Dynamics, they know that i,j are used A LOT and confusing them leads to bad things and hours of headache.
Now simple reader, try to imagine the lecturer you have is not a very good one, they're poor at explaining and don't provide adequate supporting material.
My lecturers i and j were indistinguishable.
Sigh. That was a stressful module.
1
1
u/qroamer Nov 12 '20
I use x, y, and z for number placeholders. “i” is saved for when I run out of the first three.
1
u/notsohipsterithink Nov 12 '20
I used to do that until I hit n bugs, then I was like screw it, use descriptive iterator names.
1
u/Radfordhound Nov 12 '20
Am I the only one who uses i/i2/i3...?
I personally think it just makes more sense but I’ve had multiple programmer friends freak out about it lol
1
1
1
u/gdavtor Nov 12 '20
I think the reason those are commonly used is that back in the FORTRAN days, the variables I through N were by default integers (maybe because they are the first letters of 'integer').
1
1
1
u/javellin Nov 12 '20
I used to use Star Wars characters as variables in my programs. My teacher didn’t like it.
1
1
u/ElCerebroDeLaBestia Nov 12 '20
Can you believe the linter we use at work will complain about variables with just 1 char?
1
u/peanut_peanutbutter Nov 12 '20
It's convention, which is not the law, but makes it easier for anyone who looks at your code to reason through it.
1
Nov 12 '20
i and j being the imaginary number in MatLab is the real reason why I hate that language.
1
1
u/Julio974 Nov 12 '20
I use in order i, j, k, l, then I start wondering why the fuck I have so many nested loops
1
1
u/LavaSquid Nov 12 '20
n = "number" for me, when just iterating a count. f = "float" when I need that.
1
1
u/R3D3-1 Nov 12 '20
It can make sense for abstract algorithms. Which is more readable?
A(i,j) = SUM([( B(i,k)*C(k,j), k = 1, N )])
outputMatrix(row, column) = SUM([( &
leftMatrix(row, sumIndex)*rightMatrix(sumIndex, column), &
sumIndex = 1, numberOfColumsFirstMatrix )])It is also quite acceptable, when the value has no specific meaning.
DO i=1, numberOfItems
item => itemArray(i)
doSomethingWith(item)
END DOThough in the latter case I'd use iItem over i at least.
1
1
u/puppy_sprinkles Nov 12 '20
That’s the law according to the rules.
Edit: Initially misquoted Dwight.
1
u/MoreNMoreLikelyTrans Nov 12 '20
Its cause the J is a modified I. With a little choopchick on the end.
1
1
u/mcbergstedt Nov 12 '20
Lol for some of my fortran code I would use i, j, and k then i1, j1, j1, and so on.
1
1
1
u/noise256 Nov 12 '20
It is from index. In the sigma notation for summing, i, is the index of summation.
1
1
1
1
u/siraajgudu Nov 12 '20 edited Nov 12 '20
Dude wtf that's my post ,you clearly reposted without even crediting me
1
1
1
1
u/teknobable Nov 12 '20
When I started at a new place after college, we had to do a little development in the role. I turned in something early on and was asked to change my iterator variables to something more clear, like "count"
1
1
1
Nov 12 '20
To be honest, modern Code rarely uses it anyway since you rarely need the current index of your iteration. You either want to iterate over every element (no index needed) or you are doing a lookup for a specific element in your collection. In both cases no index is necessary since most languages provide the element of your iteration.
1
1
u/sonotrev Nov 12 '20
I'm not sure how related but almost all mathematics books dealing with tensors or matrices always use i j k for element positions. I have a copy of a tensor analysis book which was translated from Russian after the wall fell and even that uses i j k. I don't know whether math has influenced programming or vice versa.
1
1
u/DrRocketWizard Nov 12 '20
1
u/RepostSleuthBot Nov 12 '20
Looks like a repost. I've seen this image 1 time.
First seen Here on 2020-06-06 100.0% match.
Searched Images: 169,714,524 | Indexed Posts: 647,522,542 | Search Time: 0.72282s
Feedback? Hate? Visit r/repostsleuthbot - I'm not perfect, but you can help. Report [ False Positive ]
1
1
1
u/anujfr Nov 12 '20
Whoa whoa! Isn't x, y, & z the king of variable name including in loops? Who the fuck uses i, j, & k?
1
u/JaCraig Nov 12 '20
I start with x because if I have a nested loop that needs to go past z, I've done something horribly wrong and need to refactor.
1
1
1
u/killzdesu Nov 12 '20
I'm the one who use a, b, c for iteration for 11 years. Of course, my friends hate me and rejected my pull requests
1
u/auchnureinmensch Nov 12 '20
I use ii and jj, because I feel weird to overwrite the imaginary number. I use Matlab, though, so I'm obviously no programmer.
1
1
1
-1
u/Sudhanva_Kote Nov 12 '20
When I was in 2nd year of engineering (it was introduction to C) our teacher asked " can we use anything else other than i in (for i=0; i<n;i++). One girl who is also topper of the class said no. So everyone else collectively said no. Me and my friend knew we can use any variable. We still sometimes tell her "I'm going to use k in a for loop".
→ More replies (2)1
0
u/DanteMiw Nov 12 '20
I know "i" stands for "index", but "j"? It's because is the subsequent letter on alphabet?
3
3
-6
Nov 12 '20
actually using single letters is a bad practice, you should get used to using proper names.
0
Nov 12 '20
[deleted]
2
Nov 12 '20 edited Nov 12 '20
Exactly. Not only is it harder to read, it becomes more prone to mistakes and after you will come back to some advanced code after several months where you were using i,j,k etc you will need to waste time trying to understand wtf is going on. Seems like people downvoting me know better lol. Pretty much every decent company will tell you to use proper naming conventions which obviously includes iterators.
0
Nov 12 '20
[deleted]
14
2
u/-_entity_- Nov 12 '20
Your teacher is probably just trying to make the concept as concrete as possible.
2
•
u/dejaydev Nov 14 '20
Hi there! Unfortunately, your submission has been removed.
Violation of Rule #2 - Reposts:
All posts that have been on the first 2 pages of trending posts within the last month, is part of the top of all time, or is part of common posts is considered repost and will be removed on sight.
If you feel that it has been removed in error, please message us so that we may review it.