logoalt Hacker News

NoNameHaveItoday at 1:46 PM1 replyview on HN

I'm glad to see there is a "Beyond the Code" section that discusses comments. Here's what I typically told my students in Intro to Programming" Good comments lend insight into the code. Reading the code itself tells you the what. Comments should explain the why. Comments like "i+=1; /* Increment i */" are of little value. However comments such as "We increment i mid loop so that we can peek ahead at the next value for a possible swap" are more useful. Use a narrative voice when writing comments, like you are explaining the code to your grandparent. This make digestion easier. Remember, code spends most of its life, and most of its expense, in the maintenance phase. The easier you make your code to understand, the less it will cost and the longer it will live.


Replies

dec0dedab0detoday at 7:40 PM

I prefer comments like "I'm sorry about this, I know it's ugly but I'm in a rush and it's the quickest way to get it working"

show 1 reply