Ask coding questions

← Back to all posts
Commenting
codefire23 (1)

Hello, can someone tell me if my commenting is okay?
I was wondering because somebody told me I should do it better.

Answered by Zavexeon (1070) [earned 5 cycles]
View Answer
Commentshotnewtop
Zavexeon (1070)

Basically, if there's ever a piece of code that doesn't look like it'd make outright sense when another developer reads it, give an explanation of why the code exists and how it works, and what it does. Providing an input/output for the code if possible is good as well.

Basically, make your code to where anybody reading it can understand what each line does.

CaptainAnon (138)

Rust has support for what's called a docstring.
For example, given this struct

struct Person {
  id: i32,
  name: String
  age: u32
}

You would document it like this.

/// Some documentation
struct Person {
  id: i32,
  name: String
  age: u32
}

Rust can automagically build you a website which makes it easy to browse the documentation for your crate.
Just type cargo doc to build the documentation.

Mendelevium (39)

So the comments are there for other people to see what’s going on without having to translate from code to English. You’re doing fine. If I were to be really critical, I would say that you should add more detail, but I’m not one to talk since I don’t really comment on my repls.