r/learnprogramming • • Mar 22 '22

Don't be that guy.

I just took over a project for a guy who left. Don't be this guy:

  • ZERO comments in your codebase.

  • One and two character variable names with no discernable pattern.

  • Comment characters used to comment out big chunks of code for no discernable reason.

  • No error checking.

  • Reliance on data files that you forgot to include or explain, going in to meaningless data frame names.

  • Making outrageous claims about how much your code improves things with zero baseline performance.

People like this think you're an idiot because you can't make their code work, and get to be all smug. We will track you down and find you. You will need references someday....oh my.

2.6k Upvotes

427 comments sorted by

View all comments

1

u/IdempodentFlux Mar 22 '22

Do you think comments are needed if the function names are super clear/read like a sentence?

3

u/[deleted] Mar 22 '22

Overcommenting is a beginner's mistake that can reduce code legibility. I often see beginners write x = x + n; // add n to x. Comments should be used sparingly to explain how code should be interpreted and what it needs, and the code should itself be legible. Add as many comments as it takes for someone who can read code to understand your code, and not more. So, yes, if the function is short and tidy and does not have secret side-effects or byzantine demands, you can do without comments.

1

u/BullCityPicker Mar 22 '22

I suppose I could be convinced by a good counter argument. Adding in a good example is a nice bit of documentation. For example, if you're passing in a column of data, are you literally passing in the column reference, or the name of the column as a string to be de-referenced later? If there are flags controlling behavior, do you list what the legal options are? Are there conditions, like null values, that would cause the function to fail if the data passed in isn't 'clean' enough?

0

u/vi_sucks Mar 22 '22

The problem is that if the function name is actually a sentence, it's painful to read and super easy to mess up. And usually it's less descriptive anyway then using a name that describes what it's used for rather than what it does.

I'd rather see:

/* This method takes two digits, adds them together, multiplies the result and then gets the square root. This is used as the main equation in calculating the factor to use in pricing. */

calcPricingFactor(int digit1, int digit2)

Than:

addTwoDigitsMultiplyTheResultThenSquareRoot(int digit1, digit2)

2

u/IdempodentFlux Mar 23 '22

Personally, I feel like the first one without the comment should be fine. If anything; I'd have a comment that links to the documentation about the product/companies policies about pricing. I shouldn't need a comment to tell me we're multiplying and getting square roots. That seems strange. I also don't think comments should duplicate the companies policy documentation; if needed it should point, but that's sort of tangential I guess.

I think it's fine to have a comment if you're explaining domain via the comment though.

0

u/vi_sucks Mar 23 '22

Personally, I feel like the first one without the comment should be fine.

This is how you become that guy.