Live data from Hacker News

Useful and useless code comments

blog.jim-nielsen.com

11–20 of 181 posts

Re: Useful and useless code comments

#11
post #9

IMO, if I feel the need to add comments to explain code blocks like this: > // Add a horizontal scroll bar > hScrollBar = new JScrollBar(scrollBar, HORIZONTAL); > add(hScrollBar, BorderLayout.SOUTH); It's a sign I should be writing code that reads more like: > addHorizontalScrollBar()

Do you then document the new function, though?

I think the function name is pretty self documenting, I would go a step further and make a function named addScrollBars() clear on its intention here

Re: Useful and useless code comments

#12
This is also somewhat about language choice. Languages more oriented towards natural language need less commenting. If I were writing C++ or assembler on a regular basis, I would probably be writing a lot of "what" comments.

My favorite comments are actually "what" comments, but that clarify something opaque about the code, e.g.

    const R = 6371e3;
    const φ1 = lat1 * Math.PI/180;
    const φ2 = lat2 * Math.PI/180;
    const Δφ = (lat2-lat1) * Math.PI/180;
    const Δλ = (lon2-lon1) * Math.PI/180;
    
    const a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
              Math.cos(φ1) * Math.cos(φ2) *
              Math.sin(Δλ/2) * Math.sin(Δλ/2);
    const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    
    const d = R * c;
It's extremely difficult to analyze that and say oh, obviously, that's the haversine distance formula, and the result is the distance in meters, of course. It would be slightly easier in a language with type annotations, but not a bunch easier. You add a simple

    // haversine distance formula, result is d in meters
and now you can google the wikipedia page to understand the context and history.

Re: Useful and useless code comments

#13

I’m on the same page. Just look at the given example. Does it look wasteful to say that you’re adding a vertical bar when you have the following line of code?: // Add a vertical scroll bar vScrollBar = new JScrollBar(JScrollBar.VERTICAL); add(vScrollBar, BorderLayout.EAST); Perhaps. But the comment is making a lot more than simply describing what’s below of it. The comment is doing the following things: - It’s creati…

Make it clear, add structure, add visual boundaries.

And push back hard against linter/prettifier rules that remove all developer discretion from layout and structure.

Re: Useful and useless code comments

#14

IMO, if I feel the need to add comments to explain code blocks like this: > // Add a horizontal scroll bar > hScrollBar = new JScrollBar(scrollBar, HORIZONTAL); > add(hScrollBar, BorderLayout.SOUTH); It's a sign I should be writing code that reads more like: > addHorizontalScrollBar()

I think my problem with this approach and why I lean to favouring the more obvious and verbose comment is, if I'm reading the code it's probably because something is broken (or I need to change something in the area).

With the function I think, "is it and if it is what else does it change?". Is that function definition actually doing what it says it is? So now I have to add another stack frame to my mental model, go to definition on the function, confirm that is indeed what it claims to do, pop the frame and resume my reading of the parent method.

With the comment I get the statement of intent and can see if the code matches inline. Now it's fair to say a good code review culture (and immutability) might create code where there's more room to trust named functions, but I've not seen it yet.

Re: Useful and useless code comments

#15

IMO, if I feel the need to add comments to explain code blocks like this: > // Add a horizontal scroll bar > hScrollBar = new JScrollBar(scrollBar, HORIZONTAL); > add(hScrollBar, BorderLayout.SOUTH); It's a sign I should be writing code that reads more like: > addHorizontalScrollBar()

Meh. If you're only doing it once, then I'd argue that the main practical difference between

  // Add a horizontal scroll bar
  hScrollBar = new JScrollBar(scrollBar, HORIZONTAL);
  add(hScrollBar, BorderLayout.SOUTH);
and (assuming scrollBar is not a local variable)

  void addHorizontalScrollBar() {
      hScrollBar = new JScrollBar(scrollBar, HORIZONTAL);
      add(hScrollBar, BorderLayout.SOUTH);
  }

  ...

  addHorizontalScrollBar();
is whether you want to do it in 3 lines or 5.

(edit: disclaimer -- Snark aside, I should say that I personally tend toward the latter option. But I am absolutely not prepared to try and convince anyone that it is, in any objective sense, better. It's just an aesthetic preference. Maybe even a nervous habit.)

Re: Useful and useless code comments

#16
I generally save comments for WTF code, or to put links that document what's going on. I try to abstract things as a lot of folks are mentioning here, but that just pushes things to another place. Eventually someone is going to need to debug that code. Here's a Puppeteer example, where text is being split into nodes to extract based on line breaks:

    /* https://medium.com/@roxeteer/javascript-one-liner-to-get-elements-text-content-without-its-child-nodes-8e59269d1e71 */
    return await this.page.$eval(selector, e => {
      const s = [].reduce.call(e.childNodes, function(a, b) { return a + (b.nodeType === 3 ? b.textContent : ''); }, '');
      return s.replace(/\s+/g,' ').trim();
    });

Re: Useful and useless code comments

#17

This is also somewhat about language choice. Languages more oriented towards natural language need less commenting. If I were writing C++ or assembler on a regular basis, I would probably be writing a lot of "what" comments. My favorite comments are actually "what" comments, but that clarify something opaque about the code, e.g. const R = 6371e3; const φ1 = lat1 * Math.PI/180; const φ2 = lat2 * Math.PI/180; const Δφ…

Modern C++ is actually quite readable now imo - fairly close to C#. Has come a long way in the last 20 years.

That being said most code out there is legacy of course...

Re: Useful and useless code comments

#18
The more I see people opine on commenting style, the more I want to just spam the "you do you" button and get on with my life.

I have a way that I like to do it. Other people on my team have their own ways. My way is not hurting them. Their way is not hurting me. Either of us trying to impose our way on the other would hurt both of us.

Re: Useful and useless code comments

#19
post #9

Earlier quoted context omitted.

Do you then document the new function, though?

I think the function name is pretty self documenting, I would go a step further and make a function named addScrollBars() clear on its intention here

Sure. Agreed. But I still docblock every function I write so this wouldn’t change much with respect to usefulness of the documentation.
Post reply on HN