Live data from Hacker News

Useful and useless code comments

blog.jim-nielsen.com

21–30 of 181 posts

Re: Useful and useless code comments

#21
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?

You've heard the XTreme slogan "Where you would write a comment, we would write a method"?

Re: Useful and useless code comments

#22

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…

Yeah, I like the "visual boundaries" way of framing it. A line of whitespace is a visual boundary, of course, but I find the // comment above it acts as a heading. Like a bold heading above a couple of paragraphs of text in a document.

I wouldn't add a heading above every paragraph, but I would might above every few paragraphs. In code, this translates to every 5-15 lines of code. Here's some code I wrote recently that shows this (https://github.com/canonical/pebble/blob/b152ff448bbe7d08c39...):

    func writeFile(item writeFilesItem, source io.Reader) error {
        if !pathpkg.IsAbs(item.Path) {
            return nonAbsolutePathError(item.Path)
        }

        // Create parent directory if needed
        if item.MakeDirs {
            err := os.MkdirAll(pathpkg.Dir(item.Path), 0o755)
            if err != nil {
                return fmt.Errorf("cannot create directory: %w", err)
            }
        }

        // Atomically write file content to destination.
        perm, err := parsePermissions(item.Permissions, 0o644)
        if err != nil {
            return err
        }
        uid, gid, err := normalizeUidGid(item.UserID, item.GroupID, item.User, item.Group)
        if err != nil {
            return fmt.Errorf("cannot look up user and group: %w", err)
        }
        sysUid, sysGid := sys.UserID(osutil.NoChown), sys.GroupID(osutil.NoChown)
        if uid != nil && gid != nil {
            sysUid, sysGid = sys.UserID(*uid), sys.GroupID(*gid)
        }
        return atomicWriteChown(item.Path, source, perm, 0, sysUid, sysGid)
    }

Re: Useful and useless code comments

#23

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...

Yes, I'm probably just traumatized from trying to look at 3d engine code "back in the day" :)

Re: Useful and useless code comments

#24
The big problem with comments is that they need to be maintained. When the code changes, the comments need to change with it.

The never, ever happens consistently.

So soon you have the "man with two watches" problem. The code says one thing, and the comments say another. You could fix it, and sometimes you spend that time, but inevitably you also stop reading the comments, since they're not reliable.

I've never seen this not happen in comment heavy projects.

Re: Useful and useless code comments

#25
I always wondered if the "useless comment" comment was really a way of bragging employed by experienced programmers (or their wannabes). Why is it a burden on anyone? Most IDE clearly highlight the comments so you can tune it out. Also, even if it is obvious, at least when a bug presents itself, you don't have to spend much time figuring out the intention of the author and if it is indeed a bug or by design.

Re: Useful and useless code comments

#26
Article mentions my preferred take: stating my intention to help keep me honest on what I am trying to achieve.

Also: they function like an anchor tag. If you are spelunking a new code base for a specific functional area, a search in plain English would let you jump to the right spot more quickly.

Re: Useful and useless code comments

#27

The big problem with comments is that they need to be maintained. When the code changes, the comments need to change with it. The never, ever happens consistently. So soon you have the "man with two watches" problem. The code says one thing, and the comments say another. You could fix it, and sometimes you spend that time, but inevitably you also stop reading the comments, since they're not reliable. I've never seen…

This is something you can quickly see with Git. If the comment lines are older than the code lines you can at least start with the assumption that the comment might be outdated.

That’s more useful than dealing with stale documentation which can also be outdated. At least with code comments you have contextual proximity.

Re: Useful and useless code comments

#28

The big problem with comments is that they need to be maintained. When the code changes, the comments need to change with it. The never, ever happens consistently. So soon you have the "man with two watches" problem. The code says one thing, and the comments say another. You could fix it, and sometimes you spend that time, but inevitably you also stop reading the comments, since they're not reliable. I've never seen…

While that is true, it isn't a problem inherently with comments.

Re: Useful and useless code comments

#29

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 Δφ…

I imagine you will use this in more than one place in the code. Why does it need a comment? Naming the function should be enough for most of it. Other languages probably aren't better at specifying the "in meters" part either, are they?

Re: Useful and useless code comments

#30

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 Δφ…

I imagine you will use this in more than one place in the code. Why does it need a comment? Naming the function should be enough for most of it. Other languages probably aren't better at specifying the "in meters" part either, are they?

A function still seems appropriate even if you're not using this multiple times. Unless how you're computing the haversine distance is relevant to the surrounding code, it would be much more readable to extract it out.
Post reply on HN