News for hackers. Stuff that matters.
Re:The Shapes of Code by classified on Tuesday January 28, 2020 @05:13AM (#22167996)
> ... not so good code: if it was, we wouldn’t need a comment...

How much longer will this rumor persist that "good code" doesn't need commenting? Good comments explain things that aren't immediately obvious from the code.

Re:The Shapes of Code by Izkata on Tuesday January 28, 2020 @08:06AM (#22168964)
You left out the most important part of this criticism:

> ...to explain each line of code.

Commenting every line is a hint to a more pervasive problem. This section has nothing to do with "comments are bad".

Re:The Shapes of Code by clarry on Tuesday January 28, 2020 @05:59AM (#22168213)
Most code should be boring and immediately obvious. I don't run into lots of real world code that needs much explaining. Sometimes you do something weird or not-obvious for good reason, but I see plenty of comments explaining things that only need to be explained due to poor design. Also poor comments that don't really explain anything that isn't obvious by looking.
Re:The Shapes of Code by nerdponx on Tuesday January 28, 2020 @08:15AM (#22169019)

    def clean_start_year(x):
        # Account for the 7 year offset in the database
        return x + 7

    def process_record(record):
        record['start year'] = clean_start_year(record['record'])
It's perfectly obvious what this code does, but without the comment it's totally unclear why it does what it does.
Re:The Shapes of Code by clarry on Tuesday January 28, 2020 @09:33AM (#22169666)

   const database_year_offset = 7;

   fix_start_year(year):
       return year + database_year_offset;

   process_record(record):
       record['start year'] = fix_start_year(record['record']);
In this version with one less magic constant (and renamed function), the comment would look very redundant.

DougBTX had the same idea, at the same time. I don't think their version needs the comment either. EDIT: Dang you removed a perfectly fine post :)

Re:The Shapes of Code by nerdponx on Tuesday January 28, 2020 @09:50AM (#22169838)
I guess, but I think it's more that my example fails to illustrate the point well.

Informative comments cannot always be "factored out" like this. Do you at least agree that a ticket or bug #, or a link to an issue in an issue tracker, would be appropriate?

Re:The Shapes of Code by clarry on Tuesday January 28, 2020 @10:08AM (#22170041)
I think a comment is appropriate if the problem cannot be factored out. But in my experience such things can be factored out most of the time, hence the argument that most code should be boring and immediately obvious.
Re:The Shapes of Code by Firadeoclus on Tuesday January 28, 2020 @10:37AM (#22170320)
Factoring out a comment into code sometimes loses information. I would argue that database_year_offset is not as informative as the original comment. Free form text can say things better than identifier names.
Re:The Shapes of Code by nerdponx on Tuesday January 28, 2020 @07:21PM (#22175751)
Most code, sure. But it's not an ideal to be striven for, or a metric to be used when evaluating code.
Re:The Shapes of Code by taneq on Tuesday January 28, 2020 @08:33AM (#22169169)
Ah, the inline equivalent of Doxygen-for-the-sake-of-Doxygen documentation.

    int frob(const int x, void *context)
    Performs a frob on x in the given context.
    Parameters:
      x - int param
      context - a pointer to context
    Returns an id of the frob performed.
Re:The Shapes of Code by clarry on Tuesday January 28, 2020 @09:55AM (#22169902)
I see a lot of this at work, sigh.

The better a variable / function is named (and the better its scope or purpose is limited to one thing), the less it needs comments. So there are a lot of self explanatory variables or functions with doxygen comments that state exactly the same thing the name already does, without adding anything besides noise. And then there are some poorly named things where the exact same thing happens: comment re-states what the bad identifier states. Sigh. Genuinely useful comments are rather rare.

Re:The Shapes of Code by nerdponx on Tuesday January 28, 2020 @12:51PM (#22171932)
How is what I posted an example of this, in any way?

Use your imagination. The "cleaning" routine might not be a one-liner. Maybe it depends on multiple functions from another file/package/library.

Re:The Shapes of Code by AnanasAttack on Wednesday January 29, 2020 @12:41AM (#22177802)
The comment doesn't explain why there even is an offset in the first place. Who put it there and why? I think this should be explained as well
Re:The Shapes of Code by AstralStorm on Tuesday January 28, 2020 @07:46AM (#22168843)
The code might be obvious, but if you don't include the reason why it exists it's a target for removal. And having too many of those makes either for garbage or hard decisions when refactoring.

Writing the rationale is the most important comment you must not skip.

And there would be links to design documentation so that it can be kept up to date. (For non-programmers.)

Re:The Shapes of Code by swish_bob on Tuesday January 28, 2020 @08:25AM (#22169095)
Why it exists ought to be explained by the existence of a test.

This isn't always possible, but it's far more possible than many developers seem to think.

Re:The Shapes of Code by minaa-chan on Tuesday January 28, 2020 @09:24AM (#22169591)
That just makes two things that need to be commented doesn't it?

When I read code I care about what the code is supposed to do and why it's supposed to do that.

There is code that passes the current tests and is logically sound but no longer fits the business requirements. Knowing that it was implemented to solve a certain use case helps the reader / reviewer see whether the code and the test still fit or if they should be updated.

The best comments I've come across comment the intention and any context that isn't immediately obvious from the function.

Re:The Shapes of Code by swish_bob on Tuesday January 28, 2020 @11:39AM (#22171002)
Your test should explain what it's testing. Via it's name or some other mechanism. Otherwise how do you know what's gone wrong when it fails?
Re:The Shapes of Code by minaa-chan on Tuesday January 28, 2020 @05:03PM (#22174582)
That other mechanism is comments.
Re:The Shapes of Code by SketchySeaBeast on Tuesday January 28, 2020 @10:23AM (#22170180)
Does that explain why, though? It explains that it's there, and someone obviously wanted it to be, but it still doesn't get at the why. I've found that most of the logic I'm left scratching my head at is due to some business rules that there's no better way to deal with - even if a test exists for it I still can't figure out what the tests purpose was, other than to just verify that it's working.
Re:The Shapes of Code by thfuran on Tuesday January 28, 2020 @11:51AM (#22171138)
Just yesterday I wrote some code that was pretty boring, pretty readily understandable, and also pretty apparently a good target for an easy simplification. However that obvious simplification would also non-obviously introduce a bug that might not be noticed immediately. So I left a comment to the effect of "yeah these two lines look kinda dumb but we need it because X"

Could I have instead massively restructured the feature to prevent the possibility of the bug the comment was explaining the workaround for? Probably. But that would have increased the number of lines of code to make the change by a factor of something like 20x-200x and would have required far more testing. It also probably would have resulted in something overall more complicated.

Re:The Shapes of Code by classified on Tuesday January 28, 2020 @07:05AM (#22168589)
I'm not sure whether I should envy or pity you. Writing code that is that obvious and boring cannot be very fulfilling. How long until a machine does that for you (after all, automating boring and obvious stuff is what we programmers do)? OTOH, on days where I feel lazy I can only wish my world were that simple.
Re:The Shapes of Code by gobayesgo on Thursday January 30, 2020 @06:13AM (#22190573)
Any monkey can puke thousands of lines of code. Producing code so boring that fellow humans can understand it immediately is the real challenge in programming. It requires much more effort and care than blaming everyone else for not understanding a sophisticated pile of mess.
Re:The Shapes of Code by goto11 on Tuesday January 28, 2020 @06:26AM (#22168370)
Maybe because some people have learnt they should comment but haven't learnt how to comment properly. And as often the case in this industry, the fact that some people do some thing badly leads other people to forbid the thing altogether.
Re:The Shapes of Code by BitwiseFool on Tuesday January 28, 2020 @11:18AM (#22170751)
We typically use comments to describe our client's business process. Even though the code itself looks straightforward what the client wants certainly isn't.
Re:The Shapes of Code by artsyca on Tuesday January 28, 2020 @06:06AM (#22168257)
A rule of thumb I follow is that code is twice as hard to read as it is to write so comments may of course help but in the other case comments that have to overly explain what the code is doing or why it's doing it also indicate that the code ought to be revised

My process of writing code is to of course code a proof of concept in order to get a feel for the problem domain this could be considered a spike in agile parlance

Then the primary and alternate flows can be coded against a basic test suite

followed by a refactor and then a comments pass

Depending on what comments arise perhaps another refactor may be in order

I typically try to follow the spirit of TRUE software

Re:The Shapes of Code by blakehaswell on Wednesday January 29, 2020 @12:22AM (#22177711)
> How much longer will this rumor persist that "good code" doesn't need commenting?

Indeed. I recently came across an article[0] which does a good job of debunking this idea, and describing different types of comments and when they can be valuable. Have a read if you're skeptical about comments.

[0] http://antirez.com/news/124

Re:The Shapes of Code by CraigJPerry on Tuesday January 28, 2020 @05:52AM (#22168172)
Like what? It can’t be the “how” - the code should be able to express that clearly. It can’t be the “what”, again that should be apparent from some combination of code and configuration / metadata.

So that leaves the “why”. To my mind, code comments are the worst place to record why a particular implementation exists. The why often needs collaboration with non-coders.

Re:The Shapes of Code by artsyca on Tuesday January 28, 2020 @06:08AM (#22168273)
It's definitely the why for any particular unorthodox bits like shims hacks workarounds and the like

The worst is finding some snippet from stack overflow or GitHub without a reference to the issue to back up why this thing is here or perhaps a TODO: with an improvement

Re:The Shapes of Code by minaa-chan on Tuesday January 28, 2020 @09:26AM (#22169605)
I disagree. The code is the best place to comment the why. Regardless of what is documented or what is written up on jira or w/e, the code is what is true.

Commenting the purpose of the code and maybe a link to some documentation allows a future reader to see if the code still fulfills the requirements.

Re:The Shapes of Code by CraigJPerry on Tuesday January 28, 2020 @04:59PM (#22174546)
The comments are very often not true, that's why there's so much push back
Re:The Shapes of Code by minaa-chan on Wednesday January 29, 2020 @11:27AM (#22182288)
So they should be updated the same as any other documentation. They're there to help. They don't need to be 100% infallible to be useful.