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
u/sephron_tanully Nov 12 '20
I also like to use l
8
54
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
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
→ More replies (36)2
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
15
10
u/prowness Nov 12 '20
It’s used at least in probability and number theory. I know, it was weird for me the first time I saw it too.
→ More replies (3)→ More replies (5)11
u/sawyerwelden Nov 12 '20
It gets used fairly often. It's pretty obvious when it's not the constant.
10
6
4
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
6
→ More replies (6)5
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 (1)3
u/kazneus Nov 12 '20
i,j,k, (l if desperate): indices for sums, products, and general collections of things. Capital I,J,K are often ideals.
this is the answer. it comes directly from math
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
u/Lame_Goblin Nov 12 '20
var 😍 = 🍆 + 💦
22
5
u/SilverDem0n Nov 12 '20
That's more of a const for me, but I take what I can get
I can't help but think the 🍆 should be changed to a pointer, but then it is a cold day out there
9
4
2
2
u/necheffa Nov 12 '20
Some say it is because of Fortran's implicit type system. Variable identifiers starting with i thru n are integers, the rest are floats. Mathematicians are why you end up needing to use ii or iii as loop control variables instead of having sensibility designed code
2
u/R3D3-1 Nov 12 '20
Different problem domain. Good luck reading large equations, if the variables have descriptive names, and even the end-result suddenly spans half a page. Short-hands become a necessity, though they generally should be either well-established or documented in the publication.
Of course, this to some degree carries over to implementing those equations. Have fun trying to check if a version with descriptive names matches what was written on paper... At that point, reducing the difference between paper and implementation really helps.
→ More replies (3)2
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
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)→ More replies (2)3
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.
3
u/saschaleib Nov 12 '20
Absolutely. If there is just one "File" object, there's usually no reason why it can't simply be called "f". If there's two of them, better use e.g. "sourceFile" and "targetFile" instead.
3
u/trezenx Nov 12 '20
that's actually pretty clever maybe it will help me with my loop-struggles, thank you
2
Nov 12 '20
Kotlin went as far as to include the keyword
itas the implicit name of a single parameter, often times for use in higher order functions, like this:
objects.filter { it.hasProperty() }2
2
u/matrix-doge Nov 12 '20
me: fOr eAcH obj iN oBjeCtARraY
Edit: seriously tho, I actually do that sometimes. Generally noun in singular form, and plural form for arrays
2
u/Victorino__ Nov 12 '20
Any variable that's supposed to count the number of elements in something: n. For example:
for i in array:
if array[i] >= 0:
n += 12
2
u/Orlando-- Nov 12 '20
I couldn't do that, because I mostly work in kotlin and "it" actually means something in some contexts. Even in other languages it bothers me using "it" as a variable
→ More replies (1)2
u/Fufu-le-fu Nov 12 '20
When I have nested for loops and already used i, I use o. Using j just leads to me confusing it with i.
2
u/Sexual_Congressman Nov 12 '20
itis for iterables which are containers ofobas indef flatten(it): return [ ob for sq in it for ob in (sq if is_iterable(ob) and not is_string(ob) else [ob])]fs/file for file strings, fp for abstract path objects, and fd for the actual thingamabob you read and write to:
with (fp:=Path(fs)).open() as fd: txt = fd.read()
srcanddsthave great symmetry as inos.symlink(src, dst)When min and max are already in use, lo and hi: lo, hi = (b, a) if (b<a) else (a,b)
lists are li or sq if a generic sequence happens to be a list; dicts/mappings are dk/mp; stack types are sp or staq and deques in particular are dq; anything related to raw memory manipulation is a buf or mem; classes are tp/cls/typ/; numbers are nb sometimes
→ More replies (8)4
u/BadFurDay Nov 12 '20 edited Nov 12 '20
Oldschool coder / don't code too often anymore, but still using:
$i,$jand rarely$kas indexes for iterating through loops and nested loops
$tempfor intermediate operations that don't need to be stored
$returnwhen preparing a function/method/subroutine's returned data
$file,$data, etc. when working with temporary i/o stuff
$queryfor assembling a query's content before executing it
$idwhen passing an id to a clearly named function/method/subroutine, eg. user_get($id)Everything else gets a name that properly and clearly identifies them as what they are, even randomly generated numbers (
$random_user), element ids ($user_id), etc.This "name things clearly" rule includes your example, I'd use a descriptive name instead of
it, since the iteration is on a specific object and not on a throwaway temporary index. There's a major difference betweenindex 7anduser 7, I don't mind the first one being$ibut the second one has to be$useror$userdataor$userstateor whatever it actually contains.→ More replies (1)
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
u/TheGreatSalvador Nov 12 '20
Thanks for the new vocab word!
12
3
u/Nall-ohki Nov 12 '20
Each new cromulent word you learn embiggens your perspicacity!
(3 Simpsons references in one sentence? Yes!)
→ 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.
9
u/devils_advocaat Nov 12 '20
Planning on the snip before Kay, Elle and Em arrive?
→ More replies (1)0
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/
3
u/siraajgudu Nov 12 '20
Yes bro OP shamelessly copied my post and reposted without even crediting me :(
→ More replies (1)
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.
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)→ More replies (1)12
u/idekvro Nov 12 '20
Can't you specify that you need whitespace around i in your search?
→ 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
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
0
0
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.
•
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.