Live data from Hacker News

Block Comments Are a Bad Idea

futhark-lang.org

61–70 of 84 posts

Re: Block Comments Are a Bad Idea

#61

Commenting out whole sections of code incredibly useful for debugging and rapid prototyping.

Many editors allow you to select the section and line-comment it as a whole.

Equally importantly, block commenting fucks with the editor's ability to comment/uncomment code.

Re: Block Comments Are a Bad Idea

#62
post #29

"I don't like something, therefore it is BAD " I've never had an issue with a block comment that couldn't be fixed in a couple seconds. The usefulness and readability of a well formatted block comment, especially for documentation generation, far outweighs any inconveniences in fixing a broken implementation.

usefulness and readability of a well formatted block comment, especially for documentation generation it's indeed a bit strange the OP doesn't mention that as an advantage, accoording to the OP the first main purpose is just that it is "perceived that line comments are impractical for long comments"

Rust supports block comments and line comments; the recommended style is “don’t use block comments”. I have never felt any inclination to use block comments in Rust. These examples of doc comments (using slash-star-star or triple slash instead of slash-star or double slash which are plain comments) are all equivalent, and I know which one I find to be nicest (the third, with line comments):

  /**
   * This is documentation.
   *
   * It spans multiple paragraphs.
   */
  pub fn foo() {}

  /**
  This is documentation.
  
  It spans multiple paragraphs.
  */
  pub fn foo() {}

  /// This is documentation.
  ///
  /// It spans multiple paragraphs.
  pub fn foo() {}

  #[doc = "This is documentation."]
  #[doc = ""]
  #[doc = "It spans multiple paragraphs."]
  pub fn foo() {}
(You can mix-and-match, too; I’ve combined doc attributes with cfg_attr before, to add doc comments if a certain feature is enabled, see the bonus section in https://chrismorgan.info/blog/rust-cfg_attr.html.)

Re: Block Comments Are a Bad Idea

#63
post #38
post #29

Earlier quoted context omitted.

usefulness and readability of a well formatted block comment, especially for documentation generation it's indeed a bit strange the OP doesn't mention that as an advantage, accoording to the OP the first main purpose is just that it is "perceived that line comments are impractical for long comments"

Line comments are no hindrance to documentation generation. Extracting the contents from a paragraph line comment is a rather simple regular expression[0]. Haskell almost exclusively uses line comments for documentation, and it works fine. You write it like this (the '|' is to indicate that it's a doc-comment): -- | Compute this and that. -- Parameter @x@ is the good one, and -- parameter @y@ is the bad one. -- -- Th…

Actually my point was mainly about the readability aspect, should have made that more clear: for me there usually still is a (slight) advantage in readability if every line is not prefixed. E.g. the code you show here looks rather cluttered to me. It doesn't mean it does to you of course, if you are used to it your brain just ignores those dashes. But mine doesn't, since I am not used to it. Though it would probably take me only a couple of days.

Re: Block Comments Are a Bad Idea

#64
post #59
post #46

Rust had an RFC at one point that considered removing (some) block comments, which generated a good argument in their favour (see also the longer comment 5 replies later): https://github.com/rust-lang/rfcs/pull/1373#issuecomment-205... Essentially it's very annoying (slash slash) for blind programmers (slash slash) if their screen readers (slash slash) keep reading out (slash slash) line comment delimiters (slash sla…

This feels more like a deficiency in the screen reader than one in line comments.

I'd argue the editor rather than the reader. Readers can't be too specialised, they have to work with a large variety of programs, so it's not going to know it's looking at code, let alone parsing it, unless the editor tells it. Instead the editor should have an accessible interface that only passes up the comment text to the reader, perhaps annotated with a "comment" role or something.

Re: Block Comments Are a Bad Idea

#65
post #7

This is my personal preference, but from experience I prefer languages that don't shun concepts because they're bad programming practice. I can understand if something is difficult to implement or prevents other features from being added, but if a feature is possible, I say add it---it might come in handy sometimes. There's a reason languages like C, C++, Ruby, and Javascript are immensely popular. It's because progr…

Incidentally, Ruby has block comments, but almost nobody knows about them, because they're inherited from Perl. Ruby comments are line comments with #, but you can also use =begin and =end for block comments.

Re: Block Comments Are a Bad Idea

#66
post #49

One can call it BAD too, but I’ve done this several times: if (self-describing) { } else if (self-describing) { } else /* description */ { } and void myfunc(int a, char *b /* optional */, ...) { Block comments are not only block, but also inline. You cannot end line comment on the same line. (Though it is easy to work around). As of good/bad: if you don’t like it personally, then don’t use it. If in group, then put t…

Put the comments at the end of the line, introducing line breaks if necessary: if (…) { } else if (…) { } else { // description } and void myfunc(int a, char *b, // optional ...) I don’t program in C or C++, but when working with the Windows API I appreciate Microsoft’s source-code annotation language which leads to being able to indicate input and output parameters, and optional ones, so that you’d wind up with some…

Sure, I’m aware of these methods; these are obvious. But my preference is to put else-comment that way, because its beauty makes me peaceful and calm when I look at it. At work I’ll do what PM expects, i.e. her/his [motivated] preferences, not mine (if you ask people what they’d like to do, work will be hardly a popular option at all, so no problem, it is consensus between being “busy” and having profit).

Re: Block Comments Are a Bad Idea

#67

"I don't like something, therefore it is BAD " I've never had an issue with a block comment that couldn't be fixed in a couple seconds. The usefulness and readability of a well formatted block comment, especially for documentation generation, far outweighs any inconveniences in fixing a broken implementation.

"I don't have these problems, therefore they are not real problems"

The author makes almost exactly your point regarding the (non)importance of these issues in practice.

What would the author have to write in order to be able to discuss the consequences of this language design choice, and be spared the barb of your wit.

Re: Block Comments Are a Bad Idea

#68

I really appreciate class-level and method/function-level comments, in languages that have a special format for them to assist with generating documentation (e.g. Javadoc, Scaladoc, JSDoc, etc). A couple of years back, the company that I work for had a series of lunch-n-learns where each presenter did a chapter of Robert Martin's "Clean Code". One of the chapters deals with comments. Not using them as a crutch, and f…

It's kind of surprising to me that anyone past junior-level development doesn't already have it hammered into them that "code is how you did something, code is why you did it". Also, if you can't possibly be any less clever in your code, then leave a comment explaining the "how", and apologize that you couldn't make it any simpler.

s/code is why/comment is why/, I think?

Re: Block Comments Are a Bad Idea

#69
post #9

The D Programming language has a neat trick to block commenting out code for debugging purposes. Wrapping code in a version(none) {} block will exclude the block from compilation. The code inside the block must still be syntactically valid so it avoids the problem mentioned in the linked post. The D Programming Language - Conditional Compilation: https://dlang.org/spec/version.html

In what ways is that better than "if(false) { }"?

From linked examples it is clear that version() is allowed outside of functions. Since version() already serves a feature, there is no need to overload the meaning of if().

Re: Block Comments Are a Bad Idea

#70

CSS only has block comments, and they’re eager-closing rather than nested. This makes it approximately impossible to comment out code if you add any explanations for why things are how they are or have any other comments inline, which makes me sad.

It is wonderful how 2 of top-3 formats of the web (css, json) went wrong on developers from the start.

Edit: oh, wait, html comments also suck.

Post reply on HN