1204
Comenting code (lemmy.ml)
you are viewing a single comment's thread
view the rest of the comments
[-] flashgnash@lemm.ee 18 points 1 month ago* (last edited 1 month ago)

"tells the user the current time" would be an excellent comment for a clock

I'm not the best at commenting my code, but generally I just try to think of what information I'd want to know if looking at this 10 years from now

Imo comments are best used sparingly, don't bother commenting something that anyone with a basic understanding of programming would understand straight away by reading the code

Functions should generally be commented with what parameters are and what they're for, plus what they output

use reqwest::Client;

// create a http client class that all other files can import 
// so as to only create one instance globally 

pub struct HttpClient {
    client: Client,

}
impl HttpClient {
        pub fn new() -> Self {
            HttpClient {
                client: Client::new(),
            }
        }

        pub fn client(&self) -> &Client {
            &self.client

        }

}

Here's an example where if I were to stumble onto this file 10 years from now, I might think wtf is this looking at it out of context, the comment explains why it exists and what it's used for

(we'll ignore the fact I totally didn't just add this comment because I suck at commenting personal projects)

this post was submitted on 28 Sep 2024
1204 points (99.1% liked)

Programmer Humor

32476 readers
770 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS