Hi, I am Amit Mathur. Welcome to my blog. I write mostly about my experiences with Software development, Web apps, Rails, and occasionally some personal stuff.

Beautiful code

September 24th, 2009

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.



Bookmark and Share

Leave a Reply

If you can read this, you don't use a typical Web Browser that plays nice with CSS.
Please do not fill in anything here!

(Textile formatting is supported)

call
Web Statistics