• flashgnash@lemm.ee
    link
    fedilink
    arrow-up
    10
    ·
    edit-2
    3 hours 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)