This is a question mostly geared towards helping my own programming style (and of course maybe yours, by example), so if you can please reply to give me some input, that would be great.
I'm writing a Javascript calendar sort of like Google Calendar's functionality but with more collaboration features. For the events that actually show on the calendar, I wrote this function that converts positions on the calendar (left and right CSS offsets) to UNIX timestamps (and another function that does vice-versa). In the code below, the parameter "el" is a jQuery encapsulation of the DIV that holds the event, the "isEndTime" parameter is whether to look at the beginning of the DIV or the end (i.e., start or end of the event), the this.parent.timeStart variable holds a Date that begins at the very top left of the calendar (midnight at first Sunday of the week), the this.parent.timeslice variable is a shortcut/cache for the number of seconds that one column in the calendar represents (i.e., # of seconds in a day), the this.hSpacing variable holds the CSS "left" offsets of the 7 columns as an array (60, 210, 360, etc., for a 1024-wide window resolution), this.vSpacing is the vertical # of pixels in one half-hour block (each day is partitioned into 48 rows of half-hour blocks), and this.borderWidth is just the width of the border between half-hour blocks.
Within a minute or two, would you be able to comprehend the following? (the whitespacing is a little ugly, but I can't go over 80 characters horizontally)
_dom2time : function(el, isEndTime) {
// The function px2num below takes a DIV element and CSS attribute like "left", and
// returns the number of px that CSS attribute was assigned.
// For example, a DIV with left:50px called with attr equal to 'left'
// would return 50.
var px2num = function(el, attr) { return
parseInt(j(el).css(attr).substr(0,el.css(attr).indexOf('px'))); };
return
(this.parent.timeStart.getTime() / 1000) +
parseInt(this.parent.timeslice * (
(function(n){for(var i in this.hSpacing) if (n <= this.hSpacing[i])
return i;}).apply(this, [px2num(el, 'left')]) +
((px2num(el,'top') + (!isEndTime ? 0 :
px2num(el,'height') + this.borderWidth)) /
(this.parent.rows*this.vSpacing))));
}
If you can't understand it, let me just take a second to explain. The UNIX timestamp at a given position is given by the time at the start of the week, plus the days before that event, plus the seconds up until the event on the day of the event. So, I take the start-of-the-week timestamp, and then take the number of seconds in a day times the "fraction" of a day the event occurs at (it would be 5.5 for an event occuring on Friday at noon). Traditionally, this code would've been done very different by starting maybe with some variable which accumulated the time up until the event (starting with the beginning of the week, then adding the days up until, then the seconds in that day up until), but instead it's all compressed into one return statement. When I had written this, it came naturally, but when I stopped to look, I realized "Wow...this might be kind of hard to follow". And yet it's much fewer lines of code than a "traditional" approach would've used. What would you prefer?
EDIT: Am I writing Lisp in Javascript?
If it's something that you'll be in charge of forever, get as clever as you can :)
Remember the words of Kernighan: "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."