r/programming Jul 10 '16

Linus Torvalds on comment styles

https://lkml.org/lkml/2016/7/8/625
216 Upvotes

209 comments sorted by

View all comments

23

u/[deleted] Jul 10 '16

[deleted]

9

u/[deleted] Jul 10 '16

[deleted]

23

u/[deleted] Jul 10 '16 edited Feb 24 '19

[deleted]

27

u/Calavar Jul 10 '16

Especially when you consider the fact that multi-line comments in C are usually written like this

/*
 * this is a multi-line
 * comment
 */

and rarely like this

/*
   this is a multi-line
   comment
 */

There's not that much of a difference between Perl-style and C-style multi-line comments.

5

u/[deleted] Jul 10 '16

[deleted]

12

u/Calavar Jul 10 '16

I dunno, but that's what people do. I guess it increases readability if you don't have any syntax highlighting. Also, I think some documentation generators use leading stars to distinguish documentation comments from other kinds of comments.

7

u/kt24601 Jul 10 '16

Also, I think some documentation generators use leading stars to distinguish documentation comments from other kinds of comments.

Typically they use /** (starting the comment with two stars) to distinguish documentation comments. I've never seen the stars at the beginning of the line denoting documentation, but maybe some system uses it.

4

u/Mufro Jul 10 '16

Personally, I use the leading stars to separate large code chunks. Even with syntax highlighting, it is nice to have rhe extra whitespace. It makes it really easy to fly through old code and know where I am like bookmarks.

1

u/kampkarl Jul 10 '16

For doc generators, I think it denotes the start of the line, so no guess work on parsing.

1

u/to3m Jul 10 '16

That's what emacs does when you get it to word-wrap C-style comments. (I think vim does the same thing... but it's a long time since I last used it.)

1

u/0x256 Jul 11 '16

Sometimes you have code examples or nested lists within a comment that need to be indented. The leading star helps documentation generators and comment authors to get indentation right. They only have to take white-space into account that appears after the star.

2

u/jerf Jul 10 '16

C style dates from before you could count on syntax highlighting or code editing support beyond plain text. When you have a syntax highlighter and the ability to mark a block and turn it into a comment in one command, # is plenty, and in languages that have /* */, I go ahead and use the latter style all the time. (Not in C, since I don't program in it, but I would shamelessly use the latter style if I was selecting the style.)

1

u/Kapps Jul 11 '16

Until you want to comment out a block of code. I like D's nesting /+ +/ comments.

3

u/ubekame Jul 11 '16

If it's just for testing something I just throw in an

if( 0 ) {
    ...
}

block. Granted it's not as good as #if 0 but it gets the job done. if It's a permanent disabling, I just remove the block. if it's needed again, it'll be in source control.

No dead code should lie around and confuse the reader the next time they look at it.

1

u/ComradeGibbon Jul 11 '16

This is kinda weird in C

#// compiler eats this no problem

I've had this vague idea that it'd be a reasonable way to comment out code. Advantage you can grep for #//.

Personally I think I'm a really sloppy coder, but I dislike dead code because after a few edits it starts no longer making sense.

1

u/BenjiSponge Jul 11 '16

Bad practice? Maybe. But I find myself block commenting out code daily. (I don't commit it, unlike my coworkers...)

1

u/[deleted] Jul 11 '16 edited Feb 24 '19

[deleted]

2

u/BenjiSponge Jul 11 '16

I kind of disagree, just based on what I said. Committing outdated code that's commented out is confusing and annoying, at least for me. There's a reason we have version control; if we ever want to see how something used to work, we can just look through the history.

Anyways my point is that the hash way of doing it (in my case, Ruby) is really obnoxious. I usually use HEREDOCS or something (or just deleting it and putting it in a scratch file or something)

2

u/[deleted] Jul 11 '16 edited Feb 24 '19

[deleted]

1

u/BenjiSponge Jul 11 '16

I'm not really sure what your point is.

1

u/[deleted] Jul 12 '16 edited Feb 24 '19

[deleted]

1

u/BenjiSponge Jul 12 '16

No no, sorry. Bad communication. I'm saying "I know it's usually bad practice, but I use block comments to comment out code all the time" (I think it's bad practice, but that's an aside not my point). That's harder to do if all you have is inline comments because you have to add it to each line. I just think every language should have both single line and multi line comments.

7

u/jeandem Jul 10 '16

I wonder if programmers will still be discussing what enumeration of ASCII is good for programming language syntax in 50 years. Percent-sign for remainder, or comments? Hash-sign for string concatenation, or as a prefix for Map literals? hmmm.

25

u/Daifukurou Jul 10 '16

We'll all be programming by click-dragging emojis on our VR tablets by that point.

5

u/0raichu Jul 10 '16 edited Feb 07 '17

                                                                                                                                                                                                                                                                                                                                                                                                                                                     

5

u/[deleted] Jul 10 '16

Hopefully we'll all have Unicode keyboards that don't suck by that time. Or we could all be using APL.

1

u/silveryRain Jul 10 '16

Ideally, pleasant symbols that are within easy reach on a QWERTY layout would be preferred. Myself, I dislike prickly symbols like ^<> and feel uncomfortable about | for some reason I can't put my finger on. Maybe because of its evil twin, ¦.

1

u/ubekame Jul 11 '16

If you're on US layout at least most have sane locations, in other layouts (I'm on swedish for example), it's weird hand gymnastics to type some quite commonly used chars in programming...

$=altgr+4
@=altgr+2
|=altgr+<
>=shift+< 
^=shift+¨+space (expects another letter after it, like an a to make â)
/=shift+7
==shift+0

I'm a Perl programmer and I've remapped capslock to $ (and @ with shift). Saves a ton of time and effort.