this post was submitted on 05 Sep 2023
635 points (97.7% liked)

Programmer Humor

19564 readers
1010 users here now

Welcome to Programmer Humor!

This is a place where you can post jokes, memes, humor, etc. related to programming!

For sharing awful code theres also Programming Horror.

Rules

founded 1 year ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[โ€“] [email protected] 5 points 1 year ago (1 children)

What? My intuition is there's always gotta be some equivalent nicer refactor that could do away with such an awkward construct.

In what kind of situation would that be totally unavoidable?

[โ€“] [email protected] 1 points 1 year ago

I could tell you my recent cenario, but it wouldn't get us anywhere. because I know that it's avoidable, but it'd take for me to run a different logic for only first element of my array. which is doable, but it'd make the code like 5 extra lines longer, harder to read/follow. But I just simply choose to put -1 and boom it's fixed, just works.

another solution would be (without context) is to add one more variable and one more check to my foreach, but that takes more memory and cpu, I usually choose the i = -1, it's ugly but not as ugly as other solutions would be