Joel Splosky once defined beautiful code as: code which does something in much less lines than what it seems like the task should take. I think these examples fit the definition exactly.
A long time back, browsing through a book, I came across code for strcpy function which is part of the standard library in C:
strcpy(char *s1,char *s2)
{
while (*s1++ = *s2++)
}
Just the beauty of the code made me fall in love with C and programming all over again. Then, I got busy with courses, jobs, projects and releases.
Recently, while programming with Ruby, I had to randomize an array of objects before it got displayed. I thought, I should consult Google’s brain before I whip up anything of my own. This is what I found:
(1..10).sort_by { rand }
Randomizes an array of numbers 1 to 10. Oh the beauty!
More details here if you want to learn about strcpy and here is where I spotted the Ruby code.
Leave a Reply