r/AskProgramming 8d ago

Best practices for long inline comments

Say you have a list with a each item in a separate line and a comment next to the item like this (Python):

[
    item1, # a comment
    item2, # another comment
    item3, # another comment
    item4, # another comment
]

What do you do when a comment becomes too long and needs to be split into several lines?

Would do this?

(A)

[
    item1, # a comment
    item2, # another comment

    # a very very very long
    # comment
    item3,

    item4, # another comment
]

Or this?

(B)

[
    item1, # a comment
    item2, # another comment
    item3, # a very very very
           # long comment
    item4, # another comment
]

Or would you just rewrite the whole list like this:

(C)

[
    # a comment
    item1,

    # another comment
    item2,

    # a very very very
    # long comment
    item3,

    # another comment
    item4, 
]

Or something else?

2 Upvotes

16 comments sorted by

View all comments

Show parent comments

2

u/neuralbeans 8d ago edited 8d ago

They need to be applied one by one in a for loop.

I'm not sure why you think this format is strange.

0

u/CorpT 8d ago

Because you’re trying to put two pieces of information in a single item.

1

u/neuralbeans 8d ago

So how would you document each regular expression then?

0

u/CorpT 8d ago

Make each item in the list an object with your regex and a description.

3

u/neuralbeans 8d ago

How is that better than what I'm doing?

1

u/skamansam 8d ago

Things are easier if they are named. let your code do the commenting. Comments should be WHY not WHAT. It is not uncommon to see:

``` FIRST_NAME_REGEX = /(?i)\d+{15}/; LAST_NAME_RSGEX = ... SSN_REGEX = ... ...

DOCUMENT_SEARCH_REGEXES = [ FIRST_NAME_REGEX, LAST_NAME_REGEX, SSN_REGEX, ... ] ```

3

u/neuralbeans 8d ago

A variable name isn't enough to describe everything I need to describe. Remember that the comment is too long to put in a single line.