this post was submitted on 09 Jul 2023
631 points (94.0% liked)

Programmer Humor

32568 readers
695 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
top 50 comments
sorted by: hot top controversial new old
[–] Sam@sh.itjust.works 84 points 1 year ago (1 children)

When I’m doing coding interviews I always like to start off and say I’m a big fan of very long variable names. “As descriptive as you can be” I say. Then I get to my first for loop. Instead of i I use “iterator” and then when I start a nested loop I use “jiterator” and it always gets a laugh.

[–] dandroid@dandroid.app 21 points 1 year ago (3 children)

I used to conduct coding interviews at my old job. If someone came in and had some humor like that, it would be big bonus points in my book. Being someone I would like to be on a team with is very important. Plus, I think it shows confidence and being comfortable in situations that make most people nervous.

[–] Hazama@lemmy.world 16 points 1 year ago (1 children)

I've been at two start ups and they had me interview people. Honestly this is what I looked for. I'd ask basic questions to prove you had an idea about coding, but I can teach someone to code, I can't teach someone to be someone I like working with.

[–] KairuByte@lemmy.world 13 points 1 year ago

You can teach them to code if there is an underlying level of logic to build off. I’ve met a few people in life who I know for a fact will never code, no matter how smart they generally are.

[–] Sam@sh.itjust.works 5 points 1 year ago

And even if it didn’t help my chances directly like that, even getting a small chuckle would help me be more comfortable and confident.

[–] tdawg@lemmy.world 4 points 1 year ago

Honestly finding someone who can relax and intergrate into your team culture is arguably more important that anything

[–] Poob@lemmy.ca 57 points 1 year ago* (last edited 1 year ago) (5 children)

i is for index. j is simply the next letter and we're too lazy to think up something meaningful

[–] PM_ME_VINTAGE_30S@lemmy.sdf.org 20 points 1 year ago (1 children)
[–] Gork@beehaw.org 5 points 1 year ago (1 children)
[–] CoffeeDev@lemmy.studio 3 points 1 year ago* (last edited 1 year ago)
[–] DraughtGlobe@feddit.nl 17 points 1 year ago (1 children)

I always thought it stood for iterator

[–] indepndnt@lemmy.world 7 points 1 year ago

I sometimes use it for "item", knowing full well its established meaning as index or iterator, because I'm a rebel.

load more comments (3 replies)
[–] barsoap@lemm.ee 32 points 1 year ago* (last edited 1 year ago) (1 children)

It depends. x and y are either elements or coordinates, a and b usually elements though in e.g. Haskell reserved (by convention) for type variables.

The i j k l series is reserved for indices. n m etc. are the counts of something, as such you'll see i counting up to n. Both are due to mathematical sum notation and general mathematical convention. Random google result:

Let x~1~, x~2~, x~3~, …x~n~ denote a set of n numbers. x~1~ is the first number in the set. x~i~ represents the ith number in the set.

...if you're using a language in which you use i often chances are you should stop coding in C and get yourself a language with iterators. Manual loops are a bug magnet.

[–] PreachHard@lemmy.world 5 points 1 year ago (1 children)

It's a shame iterators in JS are trash for memory if you have a giant array

[–] StudioLE@programming.dev 30 points 1 year ago (5 children)

A useful tip I picked up was to use ii instead of j for an inner loop. It's far more distinct than j.

If for some terrible reason you have even more inner loops you can easily continue the trend i, ii, iii, iiii, iiiii - or iv, v if you're feeling roman

[–] hstde@lemmy.fmhy.ml 35 points 1 year ago

If you have the need to nest 5 levels of for-loops, I suggest taking a step back and rethinking your approach, my friend.

Even if that other approach is just refactoring it into separate methods.

[–] biscuit@lemdro.id 3 points 1 year ago (1 children)
load more comments (1 replies)
load more comments (3 replies)
[–] dark_stang@beehaw.org 29 points 1 year ago (2 children)

x is used for map, filter, etc. a and b are used for sorts, comparisons and merges. y might be used if I'm doing multiple lambda expressions (but that means I'm in a bad place already). I have no idea why, but these are firm rules in my brain.

[–] DogMuffins@discuss.tchncs.de 12 points 1 year ago (1 children)

I've gotten used to using the singular form as in...

records.filter((record) => ...)

Not saying this way is better but it works for me.

load more comments (1 replies)
load more comments (1 replies)
[–] Dirk@lemmy.ml 19 points 1 year ago (2 children)

People who name iterators with one letter have no soul.

[–] lowleveldata@programming.dev 9 points 1 year ago

two letters it is then

[–] catfish@lemmy.ml 4 points 1 year ago (1 children)

And people who iterate over 3D space using firstDimensionIndex, secondDimensionIndex, and thirdDimensionIndex instead of x, y, z have no sense 😜

load more comments (1 replies)
[–] Cowabunghole@lemmy.ml 17 points 1 year ago (2 children)

It's my understanding that i,j are conventionally used in mathematics which carried over into programming, but specifically it comes from Fortran in which all integer variables start with "I" through "N" based on said mathematical convention

[–] LuckyFeathers@lemmy.world 6 points 1 year ago

Yep, this is the answer. In Fortran, all variables are assumed to be floats, unless the variable starts with I, J, K, L, M or N. I’m sure they had a good reason, but it sounds so bizarre today!

load more comments (1 replies)
[–] TheWoozy@lemmy.world 15 points 1 year ago (3 children)

In old FORTAN variable starting with I...N are integers. This is how the practice began.

load more comments (3 replies)
[–] throwsbooks@lemmy.world 14 points 1 year ago (1 children)

I always thought i for index when iterating through an array. Then you can't use i again in a nested loop so j follows.

Tho sometimes x, y if the array represents coordinates.

Only a maniac would use a, b.

load more comments (1 replies)
[–] Luvon@beehaw.org 14 points 1 year ago* (last edited 1 year ago)

I generally use a for each type loop or a map because I am usually applying some function across a collection, and in both cases I use the singular name from the collections plural.

’Cities.map(city -> …)’

For (val city in cities)

If I actually need the index for some reason I still prefer loop structures that give me the index and the item together

*note syntax pulled out of my head and not necessarily belonging to any specific language.

For ( city, index in cities)

cities.map((city, index) -> … )

If I need to double loop a matrix array I would use rowIndex and ColIndex for the indexes.

[–] Spzi@lemm.ee 11 points 1 year ago (2 children)

I find it hard to read when these are together:

  • i, j, l
  • n, m, u, v, w

From all the possible character combinations, somehow the lookalike combinations are among the most popular. Yes, probably comes from math. I hated it even more when my math prof's i and j on the board were indistinguishable.

load more comments (2 replies)
[–] kionay@lemmy.world 9 points 1 year ago (1 children)

I prefer index variable names that are two words. The second word is always 'index' and the first word describes the enumerable objects. carIndex, productIndex, thingIndex

I'm not paid by the character count. Longer and more descriptive is better. Long lines that go past your 1080p monitor are probably not long because of variable names but because you insist on doing many things in one line (quit doin' that). For small functions this isn't necessary, but too often I'm shunted to the middle of a big function with two or three indecies doing acrobatics over one another and while working on it I have to constantly remind myself that this i and j mean particular things.

[–] GhostlyPixel@lemmy.world 4 points 1 year ago* (last edited 1 year ago) (1 children)

I have had too many times where I have been confused trying to figure out a giant nested loop because the writer used i/j/k or x/y/z. It’s even worse when they confused when a particular bug is because they confused what their single letter variables were and used j somewhere instead of i and no one caught it because it is so easy to brush over. Name your stuff what it is, make your life easier, make others lives easier.

[–] LetThereBeDwight@lemmy.world 3 points 1 year ago

If you need three iterators, it's time for at least one function encapsulating that inner loop.

[–] csm10495@sh.itjust.works 7 points 1 year ago (1 children)

If you use I,j,k.. what do you do if you need another? I hate seeing lowercase l as a var.

[–] hh93@lemm.ee 11 points 1 year ago (2 children)

If i is lowercase too then it doesn't make a difference

But tbh if your function is having mit than 3 levels of nested loops you should probably rethink that function

[–] faerbit@feddit.de 4 points 1 year ago (1 children)

... or you just work in 3D ....

[–] fangleone2526@lemmy.fmhy.ml 4 points 1 year ago

Wouldn't that be x y z or something similar then though ? You wouldn't even think to use i j k for that

load more comments (1 replies)
[–] atimholt@lemmy.world 7 points 1 year ago (2 children)

I like range-based for loops. You can just name the iterator after the object that it actually is. Have to be a little careful though, if the container is named a plural noun, and the natural name is the same word minus the easy-to-miss 's'.

[–] LeFrog@discuss.tchncs.de 4 points 1 year ago

a plural noun

sheep.each do |sheep|
  sheep.baaah
end

Oh shit :D

load more comments (1 replies)
[–] 80085@lemmy.world 6 points 1 year ago (1 children)

I think it's a convention taken from math notation conventions.

[–] soloner@lemmy.world 7 points 1 year ago (5 children)

I is short for "index" for a traditional for loop for mapping over an array and looking up by index. J comes after I and is used for nested loops so it doesn't shadow the outer I.

load more comments (5 replies)
[–] Dohnakun@lemmy.fmhy.ml 6 points 1 year ago* (last edited 1 year ago)

for <thing> in <amount> do...

[–] DScratch@sh.itjust.works 4 points 1 year ago

Int index = 0 But you shorten the name to Int I = 0

[–] TeoTwawki@lemmy.world 4 points 1 year ago* (last edited 1 year ago) (1 children)

I used starcraft references in mine till the project lead demanded I knock it off.

The protoss quotes were perfect.

load more comments (1 replies)
[–] got2best@lemmy.world 3 points 1 year ago

OMG the PRNDL

load more comments
view more: next ›