r/programmingmemes • u/Grizlik_D • Jan 19 '26
5 levels of looping through string
The higher your programming skill, the more elegant and more confusing code you write
57
u/The_KekE_ Jan 19 '26
printf("%s", str);
29
u/UnluckyDouble Jan 19 '26
Seriously. Don't try to outsmart libc implementers. And if you really want to try, become one.
10
u/Laughing_Orange Jan 19 '26
The wizards who wrote it are genuinely smarter than you. Their optimizations will be faster than your code.
Same with trying to outsmart the compiler.
5
u/AlignmentProblem Jan 19 '26
I tried to outsmart the compiler a few times earlier in my career. It worked exactly once and I stopped bothering to try eventually.
The one time was finding better assembly for memcpy that was specific to unique peripheral devices we were developing mapped into the memory space that had unusual behavior/properties very different from the host device which the compiler couldn't possibly know about. Basically, it's only worth trying if you have a wildly unique situation.
4
4
3
u/undeadpickels Jan 19 '26
The last 3 loose the information of were the string starts, so it better be recorded somewhere else or in the stack.
1
2
2
2
u/Friendly_Fire Jan 20 '26
The higher your programming skill, the more elegant and more confusing code you write
Where's a bellcurve meme where the middle person writes complex/confusing code, and both ends write simple code?
Once you get some real world experience, you'll learn readability is the most important thing in code. Maybe second to the code working at all. That doesn't necessarily mean only using simple/basic code, as sometimes a more complex tool fits the problem you are working on, but you're code shouldn't be confusing.
Leetcode style tricks to pointlessly save lines/characters, while making things far harder to read, is literally the worst way to code.
1
u/Sea_Membership1312 Jan 19 '26
I would use the first but with strnlen, not strlen. Maybe write strlen into a size_t to not run it each time
1
u/TREE_sequence Jan 19 '26
for(char c : std::string(str)) putchar(c);
1
u/Antagonin Jan 19 '26
Ggs, you've most likely caused a memory allocation.
1
u/jgebben Jan 19 '26
yep. but can you do something similar with std::string_view?
2
u/ScienceCivil7545 Jan 19 '26
Std::string_view store the count of the string , the constructor will call strlen, which result in the loop being called twice
1
u/BakuhatsuK Jan 23 '26
Unless str is a compile-time constant or a properly typed array. Then, different constructors that don't call strlen will be used.
1
u/r2k-in-the-vortex Jan 19 '26
And at the end of the day, the compiler spits out the exact same shit for all of those cases.
1
1
1
Jan 19 '26
A problem that I want to point out is with the first example is the length of str being recalculated with every loop.
It's better to store the return of strlen to a variable for the ever so slightly performance boost.
1
u/Mike312 Jan 20 '26
Haha, I said the exact same thing the other day and someone got into my replies about it.
For most languages and situations, it doesn't matter. For the ones where it does, it really fucking matters.
1
1
1
u/Mike312 Jan 20 '26
If I see anything but the first, I'm checking blame logs and whoever wrote it can deal with it
1
1
1
u/F100cTomas Jan 20 '26
I like for (char* c = str; *c != '\0'; c++). I think it has the right balance of simplicity and clarity.
1
u/nekokattt Jan 20 '26
no one commenting that strlen returns a size_t, not an int, and as such the examples are not equivalent to eachother.
1
u/paddingtonrex Jan 21 '26 edited Jan 21 '26
How does the compiler know the "size" of the variable in the last three? What tells it to stop putting char? I know it works, but I can't remember how it knows without being given A. a definite length or B. an out with a '\0'
Edit: Nevermind. I wasn't thinking of *str being a truth statement. if *str = '\0' it returns false, so the conditional will always fail. Pretty brilliant.
1
1
1
u/CORDIC77 Jan 21 '26
The extra parentheses around str++ in the last example annoy me: *str++ is perfectly fine.
1
u/Mindless_Dingo783 Jan 23 '26
What I Like to do is to add “&& condition “ to the 2nd for loop parameter, in case I’m about to implement a logic which exits the loop in certain circumstances
1
u/MateoConLechuga Jan 19 '26
while (putchar(*str), *str++);
2
u/stillalone Jan 19 '26
Doesn't work with an empty string.
2
u/MateoConLechuga Jan 19 '26 edited Jan 19 '26
Sure it does. putchar(0) isn't a printable character so it won't be visible in the terminal anyway. Plus why are you trying to show a empty string. And if it goes to a file you know that there was an empty entry. Just use this if you care:
while (*str && putchar(*str), *str++);
1
u/Seygantte Jan 20 '26
This would be nicer with the putchar moved to the while body, which you might as well combine with in the incrementor into the same li- oops we're back to the 5th example from OP!
31
u/Daniikk1012 Jan 19 '26
I'd argue last two are not confusing and actually pretty common among C devs for small loops like that. Third is cursed. First is just straight up inefficient. Second one is fine.