News for hackers. Stuff that matters.
Re:AI to name your Java Classes by manmal on Sunday March 05, 2023 @10:09AM (#35030212)
Everytime I see i,j,k as variable names, I wonder if the original author will be able to understand their own code in a few weeks.
Re:AI to name your Java Classes by dtech on Sunday March 05, 2023 @10:34AM (#35030470)
Probably the worst example since those are pretty canonical as loop counters. Try a,c,w,z.
Re:AI to name your Java Classes by ajross on Sunday March 05, 2023 @10:41AM (#35030544)
Come on, folks. It's a culture thing. If your loop body is 20 lines, fits trivially on a single editor window, and starts with a declaration of a counter named "w" (for "worker", or "width" or whatever obvious thing it's referencing), that's Just Fine. No reasonable reader is going to have trouble with that, and you know it. Decades and decades of extremely maintainable[1] code has been written in that style. Aesthetics are just aesthetics. Write to the style of the project you're maintaining, and just let the argumentation go.

[1] And more to the point: extremely maintained. You're really going to put the code quality of your Java WhateverFactoryListenerRepository class, that will probably be rewritten by some new hire in 2025, down against a Linux kernel file that has been under continuous improvement since 1997? Because of variable names?

Re:AI to name your Java Classes by manmal on Sunday March 05, 2023 @12:05PM (#35031497)
I agree that one letter variable names sometimes make sense. I’d use w for width if the function is short, has a very limited number of variables, w is limited to the scope of the function and not an input parameter, and w is used multiple times.
Re:AI to name your Java Classes by twic on Sunday March 05, 2023 @10:56AM (#35030698)
I had an annoying bug a couple of weeks ago. I had an array of ongoing events, and an array of time windows (roughly, quarters or years). I wanted to generate the cross-product of events and windows, evaluate some metric for each combination, and total the metric per time window. So, i wrote a pair of nested loops over the two arrays, calculated the metric in the inner loop, and accumulated it into a third array with a slot per time window.

Except i had called the loop variables i and j, forgot which way round i had nested the loops, and used the index into the event array to select a slot in the accumulation array. I was baffled as to why my results were completely wrong, but totalled to the right amount!

Once i realised what i had done, i renamed the loop variables to eventIdx and windowIdx. This is far better.

Re:AI to name your Java Classes by manmal on Sunday March 05, 2023 @11:47AM (#35031275)
Why would it be such a good idea to obfuscate the semantics of three different loops (i,j,k) instead of writing “userIndex”, “addressIndex”, and “charIndex”? With “j”, I have to lookup its meaning constantly. If I need to scroll to understand the meaning of a variable, then there is room for improvement in my opinion.
Re:AI to name your Java Classes by MikeTheGreat on Sunday March 05, 2023 @12:08PM (#35031526)
You're right that they're terrible names, and that giving them names that describe how the counter is being used would be better

and

Since they get used as loop counters so much you at least know that they're being used in a loop. It's not much info, but at least it's something. (The grandparent's suggestion of 'a, c, w' (or whatever) tells me _nothing_ :) )

Re:AI to name your Java Classes by ilyt on Sunday March 05, 2023 @10:24AM (#35030360)
outer, middle, inner loop variable, not hard,
Re:AI to name your Java Classes by xxs on Sunday March 05, 2023 @12:20PM (#35031639)
i can be an iterator as well, if 3 used together - definitely 3 nested loops... or 2, and k is key in a Map. The gp jab was unjustified.
Re:AI to name your Java Classes by furyofantares on Sunday March 05, 2023 @10:27AM (#35030401)
In over 25 years of doing that I've yet to encounter a problem so idunno, maybe your model of something here is drastically off.
Re:AI to name your Java Classes by manmal on Sunday March 05, 2023 @11:53AM (#35031338)
I’m not sure what you are implying. This is not a problem you’d notice if you use this pattern yourself, but something that makes code harder to read for people that don’t.
Re:AI to name your Java Classes by furyofantares on Sunday March 05, 2023 @12:40PM (#35031903)
I directly replied to you post saying:

> I wonder if the original author will be able to understand their own code in a few weeks

As the original author of plenty of this code, I'm reporting no problems 1300 weeks in.

Re:AI to name your Java Classes by manmal on Sunday March 05, 2023 @02:25PM (#35032993)
Thanks for replying, but it seems you missed the second part of my comment.
Re:AI to name your Java Classes by furyofantares on Sunday March 05, 2023 @05:16PM (#35034692)
A fine criticism, but surely you see that you first made a comment about the author themselves not being able to read their own code, I replied about that, then you said it wasn't something the author themselves but about other people.
Re:AI to name your Java Classes by marginalia_nu on Sunday March 05, 2023 @11:58AM (#35031406)
To be fair, I find every code style that isn't my own harder to read than the style I prefer myself.
Re:AI to name your Java Classes by hdjjhhvvhga on Sunday March 05, 2023 @10:11AM (#35030230)
I don't remember using anything else in a for loop.
Re:AI to name your Java Classes by manmal on Sunday March 05, 2023 @12:37PM (#35031857)
It's an old convention that comes either from Fortran or math (sums/matrices). Depending on how long a for loop is, more descriptive names can make sense.