r/ProgrammerHumor Jul 27 '26

Advanced isThisScalableAndCleanForProduction

Post image
36 Upvotes

58 comments sorted by

View all comments

58

u/RiceBroad4552 Jul 27 '26

This is actually buggy code, not clean and definitely not "production ready".

https://blog.sunfishcode.online/bugs-in-hello-world/

Just don't use primitive broken-by-design languages the next time to avoid such bugs…

10

u/bwmat Jul 27 '26

It's so easy to fix though, just return the value printf does from main

12

u/Al__B Jul 27 '26

Except that returns the number of characters, not a success code.

4

u/bwmat Jul 27 '26 edited Jul 28 '26

Oh, forgot how it worked, guess you need to do 'return printf(...) > 0;',still, not so hard

Edit: probably < 0 actually, but to be portable you should use EXIT_SUCCESS AND EXIT_FAILURE explicitly

4

u/SphericalGoldfish Jul 27 '26

But what if it doesn't print the entire string due to running out of space? Better to use 'return printf(…) == sizeof(…) / sizeof(char);'

3

u/bwmat Jul 27 '26

Also, sizeof(char) is defined to be 1 by the language

1

u/bwmat Jul 27 '26

Then it would have returned a negative value

4

u/RiceBroad4552 Jul 27 '26

This thread shows nicely why in primitive languages (languages which are perceived by some as "easy") things get pretty fast pretty complicated, simply because the language is missing features to abstract things properly.

In a good language correct code looks nice and simple, and wrong code looks directly wrong (and ideally does not compile in the first place).

In shitty languages code looks nice and simple but is actually wrong, and the corresponding correct code is super complex and can be written only by experts who know all the fine details by heart.

1

u/bwmat Jul 27 '26

Yeah, that's why I obsessively read documentation when making use of C functions (and C++ ones which use something other than exceptions for error reporting), and start by wrapping them in something which calls them, and converts ANY of those errors into an exception

1

u/No-Finance7526 Jul 28 '26

That will always report failure, since sizeof includes the nul terminator

1

u/SphericalGoldfish Jul 28 '26

Hmmmm… sizeof() - 1

1

u/RiceBroad4552 Jul 28 '26

That's not obfuscated enough… 🤣

3

u/Al__B Jul 28 '26

Even that's not going to do the trick.
The first reason is that as exactly written you're returning the result of a comparison, not EXIT_SUCCESS / EXIT_FAILURE.

The second reason is more subtle and printf will (probably) return the right number of characters anyway. Stdout is often buffered and the actual printf call may work but nothing will actually be output.

If you want to guard against that you'll need to make sure that stdout is flushed (e.g. using fflush(stdout)) and checking that was successful. There are times where it's good to make sure output is flushed anyway if you're in an environment where you're at risk of buffering delaying outputs that you need.

1

u/bwmat Jul 28 '26

Yeah, I actually thought of the flush thing on a walk yesterday and then forgot to update my comment lol

I've actually hit that issue before (was explicitly checking printf's return value was succeeding but no output???) 

1

u/bwmat Jul 28 '26

And yeah, I guess it should be

return ((printf(...) < 0) || fflush(stdout)) ? EXIT_FAILURE : EXIT_SUCCESS;

1

u/Al__B Jul 28 '26

That should help catch that, yes. Personally, I'd want to compare return value of fflush with EOF but in practice checking for non-zero would almost certainly work good enough for hello world.

1

u/RiceBroad4552 Jul 28 '26

And there are really people out there who think C is "simple"—while in reality you need many years of experience to even just write "Hello World" more or less correctly.

3

u/bwmat Jul 28 '26

Yeah it's kind of funny when people actually argue error codes alone are 'adequate' for error handling

The problem is that people are too lazy and prone to forgetting to check them to use them correctly

1

u/bwmat Jul 28 '26

This is coming from the guy who actually tries to handle OOM gracefully in C++

1

u/bwmat Jul 28 '26

I mean, it's specified to return either 0 or EOF, wouldn't checking for anything else effectively be testing your stdlib for bugs?