Hacker News new | ask | show | jobs
by dhimes 2492 days ago
OT, but check out the comments in the code. It doesn't instill confidence:

    // Adjusts the position of the given element
    WebDeveloper.Common.adjustElementPosition =      
       function(element, xPosition, yPosition, offset)
    {
      // If the element is set
      if(element)
      {
        var contentWindow = WebDeveloper.Common.getContentWindow();
        var innerHeight   = contentWindow.innerHeight;
        var innerWidth    = contentWindow.innerWidth;
        var offsetHeight  = element.offsetHeight;
        var offsetWidth   = element.offsetWidth;
        var offsetX       = contentWindow.pageXOffset;
        var offsetY       = contentWindow.pageYOffset;

        // If the x position is less than 0
        if(xPosition < 0)
        {
          xPosition = 0;
        }

        // If the y position is less than 0
        if(yPosition < 0)
        {
          yPosition = 0;
        }

        // If the element will fit at the x position
        if(xPosition + offsetWidth + offset + 5 < innerWidth + offsetX)
        {
          element.style.left = xPosition + offset + "px";
        }
        else
        {
          element.style.left = innerWidth + offsetX - offsetWidth - offset + "px";
        }
2 comments

one may snicker at the redundancy of the "is less than 0" pair and so son, but I'm grateful for "if the element will fit at the x position." It puts into words what isn't obvious from the code.
Fair enough. But if they are going to do that, wouldn't a comment about what the positions are in each case (especially the case where it doesn't fit) be helpful?

    // If it fits, put it there.  If not, align to the right side
or whatever.

        // If the x position is less than 0
        if(xPosition < 0)
Looks like they have some kind of policy of at least 10% of code lines must be comments or something
I write such comments sometimes, the reason is simple: when a codebase has a lot of comments in general comments often act as separators of "groups of code" (and often read like short prose), so seeing a bit of code not having them feels unbalanced and i add some generic comment. The comment acts more as a separator or header, in a "this is the part where we do XYZ", than anything else.

Also this has to be taken into account with syntax highlighting where comments are made to stand out. It doesn't work that much when comments use a faded out color (like some schemes use) or without syntax highlighting at all (well, except perhaps if you use ====== ALL CAPS COMMENTS ====== :-P).

But even then, "Set negative x values to 0" would be a more informative header than repetition of just one part of the statement