Hacker News new | past | comments | ask | show | jobs | submit login

Fewer lines of clearer code: I reject the dichotomy.

So first you have four lines of comment explaining a completely unnecessary helper function--px2num(el,'top') doesn't save a lot over parseInt(el.css('top')), and the later is clearer. Then you have all packed into your return something that's technically one line but actually takes seven lines to display in a way that it can actually be read.

So could this be written clearer in less than ~12 lines? Oh yes. First, why are we working in seconds when JavaScript uses milliseconds natively? Assuming that's really necessary, you must do myDate.getTime()/1000 an awful lot. So let's assume you added a getTimestamp method to Date.prototype. And I'm going to assume that el.style.left actually has to equal one of the members of hSpacing--it doesn't make sense that you'd allow dropping without snapping to the grid and that you'd only want the right result if they happened to drop left of the day column but not if they drop one px right.

And it looks like you've got jQuery mapped to j instead of $, which is weird but ok.

    function domStartTime(el) {
      var week = this.parent;
      return week.timeStart.getTimestamp() +
        week.timeslice * j.inArray(parseInt(el.css('left')),this.hSpacing) +
        (parseInt(el.css('top')) + this.borderWidth) / week.rows / this.vSpacing;
    }

    function domEndTime(el) {
      return domStartTime(el) +
        parseInt(el.css('height')) / this.parent.rows / this.vSpacing;
    }



Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: