r/ProgrammerHumor • • Nov 12 '20

Removed: Repost It's the law!

Post image

[removed] — view removed post

10.9k Upvotes

378 comments sorted by

View all comments

Show parent comments

186

u/[deleted] 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 i is.

But if someone writes

Class A

{

private int i;

}

then fuck that person.

9

u/AceSLS Nov 12 '20

Class A

{

private int i;

}

I'm waiting fingers crossed

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.

1

u/TSPhoenix Nov 13 '20

Sure, but if I can't pick my own IDE I'm probably not talking about a loop I wrote.

12

u/idekvro Nov 12 '20

Can't you specify that you need whitespace around i in your search?

2

u/sometimes_insightful Nov 12 '20

Wouldn’t that miss something like array[i] or i-1

1

u/olafurp Nov 13 '20

Then don't make long loops

1

u/olafurp Nov 13 '20

For this case I agree, it's convention after all. I'm a js guy now so I write these nowadays

[...elements].forEach((element, index) => {})

Variable name should convey the whole meaning, no more, no less.

1

u/[deleted] Nov 13 '20

That's assuming you want to iterate to the end, though.