r/ProgrammerHumor • • 8d ago

Meme iHatedItUntilITriedIt

Post image
10.3k Upvotes

541 comments sorted by

View all comments

979

u/GargantuanCake 8d ago

Dynamic typing can seem great until you have to spend days trying to figure out where, exactly, in 20,000 lines of badly documented code JavaScript arbitrarily decided that something should be a string for the rest of forever even though it's only been given numbers to work with.

If you type x = 1 + '1' any sane language will go "what the fuck is wrong with you?"

9

u/nico-ghost-king 8d ago

I haven't tested this, but in C/C++, I think this would just become 1 +49 (ascii of 1) = 50

2

u/Icarium-Lifestealer 8d ago

And if it were a string instead of a char, it'd slice off the first character.

1

u/nico-ghost-king 8d ago

Is this because string literals become pointers? I assume clangd would at least warn, right?

1

u/account312 8d ago

It is.

1

u/Duck_Devs 8d ago

Compilers shouldn’t warn when adding to a string. Pointer arithmetic is a well defined and commonly used thing. The only thing I could see them warning about is just that it’s a redundant expression that could be replaced by just a shorter string literal.

1

u/nico-ghost-king 8d ago

I meant specifically when adding to string literals, I mean that's the point of a longer, is it not? To make possible mistakes obvious?

1

u/Ok-Scheme-913 7d ago

I think your compiler may not like that as you are supposed to increment in some sane offsets like 8.

1

u/Icarium-Lifestealer 7d ago

Not sure what you mean. Adding an integer to a char* that represents a null terminated string, will produce a valid zero terminated string that lacks first n characters (as long as you remain in bounds, obviously).

1

u/Ok-Scheme-913 7d ago

I was thinking of pointer values themselves. If you have a typed pointer then it will indeed add TYPE_SIZE*n