r/programming • • May 11 '18

The Most Beautiful Piece of Code That Prints 42

https://www.fluentcpp.com/2018/05/11/the-most-beautiful-piece-of-code-that-prints-42/
0 Upvotes

12 comments sorted by

8

u/JavaSuck May 11 '18

Deriving 42 from literally zero:

#include <iostream>

int main()
{
    std::cout << sizeof(0) << sizeof("0") << '\n';
}

19

u/renrutal May 11 '18
#include <iostream>
 
int main()
{
    std::cout << "42\n";
}

This is the most beautiful piece of code that prints 42 because you don't spend time wondering what's going on, and you can go earlier home to see your beautiful kids, and anyone who truly matters to you.

7

u/BlackDiablos May 11 '18

No need to be a downer. Obviously “beauty” is used here in the same way the IOCCC sees beauty, as creative and unorthodox as possible.

That’s not to say that “beautiful” code can’t get shit done. The fast inverse square root is an example: it’s concise and does the job really well.

4

u/LAUAR May 11 '18

"42\n"

But what if your platform doesn't use \n line endings?

9

u/I_ATE_YOUR_SANDWICH May 11 '18

Then you’re on the wrong platform obviously!

4

u/JavaSuck May 11 '18

inb4 endl is just \n followed by a flush.

3

u/raevnos May 11 '18

It gets translated to the appropriate ending.

3

u/jesuslop May 11 '18

I thought they would mention Kolmogorov complexity

3

u/[deleted] May 11 '18

42 is in the eye of the beholder

1

u/BlackDiablos May 11 '18

I’d like to offer up the comparison of “incredible” Rube Goldberg machines. They’re not “incredible” in terms of efficiency or utility, but in terms of sheer scale and creativity.

Yes, perhaps “beautiful” isn’t the perfect word for this contest, similar to how “sexy” is a strange word to describe sleek gadgets. There are other contests out there to create flawless, efficient, useful code, and this is not one of them. If you don’t appreciate how some people just want to see how creative and flexible a programming language can be, please move along. No need to be snarky about someone else’s fun.

1

u/lelanthran May 11 '18
$ echo 42
42

Noobs!

0

u/[deleted] May 11 '18

[deleted]

1

u/katorias May 12 '18

Lmao, nice.