Programming Without Braces

I was looking at this snippet of Javascript I wrote

	if (minutes) return minutes.minutes();
	else if (hours) return hours.hours();
	else if (days) return days.days();
	else if (weeks) return weeks.weeks();
	else return 0;

and noticed a certain beauty in this if-then-else statement in the fact that it doesn't contain any braces. Removing the braces de-clutters this code and makes it look...Zen.

The idea of using no braces was flashed out by Alan C Francis in his Redefining Simple article. So maybe I was subconciously affected by his article. What does this tell us about the use of braces though? Braces are not so nice. begin and end keywords and square brackets aren't so nice either...

I would also add that not using indentation at all also helps the beauty of the snippet. So this

	if (minutes)             return minutes.minutes();
	else if (hours)            return hours.hours();
	else if (days)             return days.days();
	else if (weeks)             return weeks.weeks();
	else             return 0;

does not look as good as the original above. In fact I would say this is approaching ugly, with the beginning if the lines jumping back and forth. So this tells us that indentation isn't necessarily the answer either.

Another thing I would say about the original snippet is that it looks almost like ML or LISP, which is probably why I like it.

blog comments powered by Disqus