Live data from Hacker News

The worst possible Hello World program

boredzo.org

11–20 of 23 posts

Re: The worst possible Hello World program

#11
post #7
post #5

Earlier quoted context omitted.

Ouch. Now the article isn't just advocating overengineering, it's just plain wrong - have you contacted the author to let him know? Probably best for him to fix this up asap.

mturmon hasn't (so far), but a friend just passed me the link to this thread. Thanks nonetheless. I'll fix it in a few minutes.

And I hope all this criticism isn't too down-heartening... at least from my part (though I strongly believe it is the same for others too) it's intended as friendly and genuine constructive criticism.

Re: The worst possible Hello World program

#12

We now use puts instead of printf. puts does not try to insert any values into the string, and it supplies the terminating newline (the \n you saw before) for us. I don't see how "does not try to insert any values into the string" and "supplies the terminating newline" are not contradictory.

It writes the whole string as passed, without inserting anything into it, and then writes the newline.

Re: The worst possible Hello World program

#13
post #2

I get the point, and it's important to point out to beginners the various subtleties of error checking, etc. - but to me the 'best possible' hello world is rather overengineered, I mean 4 includes to begin with... :-) And dude, don't go using the word 'worst' as lightly as that. It's dangerous - I am sure we could make it ohhh really quite worse... how about relying on some known compiler/os bugs to flip some bits so…

If we tried to pour the collective total of human stupidity (artificial or otherwise) into a single program, it would exceed the capacities of all the storage devices ever made, past, present, and future. :-)

Honestly, if I were writing this today, I'd make the “worst” example less bad. What you see here is way too fantastic. 2011 Me would make it realistically bad—the sort of thing a new or just plain apathetic programmer might write. I wonder, though, how much it would differ from the canonical one-liner.

Re: The worst possible Hello World program

#14
post #13
post #2

I get the point, and it's important to point out to beginners the various subtleties of error checking, etc. - but to me the 'best possible' hello world is rather overengineered, I mean 4 includes to begin with... :-) And dude, don't go using the word 'worst' as lightly as that. It's dangerous - I am sure we could make it ohhh really quite worse... how about relying on some known compiler/os bugs to flip some bits so…

If we tried to pour the collective total of human stupidity (artificial or otherwise) into a single program, it would exceed the capacities of all the storage devices ever made, past, present, and future. :-) Honestly, if I were writing this today, I'd make the “worst” example less bad. What you see here is way too fantastic. 2011 Me would make it realistically bad—the sort of thing a new or just plain apathetic prog…

I think the best kind of example would be one where the problems weren't at all obvious, yet were rather fatal.

Re: The worst possible Hello World program

#15

The "best" is way overengineered. I doubt this error-handling will even work. If the program fails to print "hello world" using puts then it is very likely to fail on fprintf to stderr.

Only if both stdout and stderr are unwritable. If stdout is unwritable but stderr is writable, then writing to stderr should work.

If they're both not writable, or stderr is writable but writing fails anyway, there's nothing we can do about it anyway, but that does not make it any less proper to try.

Re: The worst possible Hello World program

#16
The weirdest part, looking at it five years later, is that the page shows usage of and talks about puts, while the “Best Possible” code in the zip archive uses fputs. Unless anyone can think of a reason not to use puts, I'll change the zipped “Best” example over to use it after I get it both working and failing properly.

Re: The worst possible Hello World program

#17
post #7

Earlier quoted context omitted.

mturmon hasn't (so far), but a friend just passed me the link to this thread. Thanks nonetheless. I'll fix it in a few minutes.

And I hope all this criticism isn't too down-heartening... at least from my part (though I strongly believe it is the same for others too) it's intended as friendly and genuine constructive criticism.

Not at all. I always welcome constructive criticism; any opportunity to improve myself, I much appreciate. For my part, any criticism I offer, including the linked article itself, I likewise intend to be constructive.

Re: The worst possible Hello World program

#18
post #13

Earlier quoted context omitted.

If we tried to pour the collective total of human stupidity (artificial or otherwise) into a single program, it would exceed the capacities of all the storage devices ever made, past, present, and future. :-) Honestly, if I were writing this today, I'd make the “worst” example less bad. What you see here is way too fantastic. 2011 Me would make it realistically bad—the sort of thing a new or just plain apathetic prog…

I think the best kind of example would be one where the problems weren't at all obvious, yet were rather fatal.

Maybe so. Perhaps one example fault would be testing the result of fputs against 0 rather than EOF…

Re: The worst possible Hello World program

#19
post #9
post #4

This is actually quite a laugh. Compiling the author's recommended "best" hello world on Mac OS 10.6 (gcc 4.2.1) yields this: % a.out Hello World! ERROR: fputs did not succeed in writing our message string (error returned: Unknown error: 0)! A look at the man page for puts shows that the author bungled the test for error: fputs() now returns a non-negative number (as opposed to 0) on successful completion. As a resul…

Not so much bungled as got bitten by a change in behavior. Amusingly, “Return Values” still states the old behavior, to which standard the program is correct. I'll fix the program. By the way, the zip archives include a Makefile with correct compiler configuration (namely -std=c99). I tried downloading them after seeing this thread and it didn't work; I've re-uploaded them and now they download correctly, Makefiles a…

[deleted]

Re: The worst possible Hello World program

#20
post #16

The weirdest part, looking at it five years later, is that the page shows usage of and talks about puts, while the “Best Possible” code in the zip archive uses fputs. Unless anyone can think of a reason not to use puts, I'll change the zipped “Best” example over to use it after I get it both working and failing properly.

I find keeping the semantics of puts in my working memory to be harmful. The problem is that whereas (for instance) printf(...) is essentially an abbreviation for fprintf(stdout, ...), puts adds a newline whereas fputs does _not_. This is a headache to keep track of.

The result of this is that I always use fputs to write a plain C string to a stdio handle, specifying stdout explicitly and adding newlines if needed, and I ignore puts entirely, much like I ignore gets. The convenience is not worth the subtle error-causing asymmetry and associated mnemonic pain, especially if that code may ever have to write somewhere other than stdout.

Post reply on HN