I was wondering how one should write comments in code. I mean, should a comment be a descriptive of what is done in code, like ://we have got array, now we iterate over it.code to iterate arrayor should it be telling what we should do here. like an order, like ://we have got array, iterate over it.code to iterate array
Best way of writing comments in code
programming practices;comments
Overview what the code is about. It may be obvious when you write it, but not two years later, so write down when this code is used in the application, why it is used and so on. Like this code handles the situation where a user fills out a form complaining about returned packages lost in the post. Answers the question what the hell is this code for. If it isn't obvious, a high level overview how the code works. Comments in header files (or in places where they get automatically extracted) so that for every public function, a reader will know what it does and why it should be called without referring to source code. Especially for virtual functions: Comments in header files what this function, including overrides, is supposed to do. For non-virtual functions the comments are a convenience, saving me from reading source code. For virtual functions, there is no source code. Comments that tell other developers when you do something that looks weird. There are places where my code contains some lines that look like absolute nonsense, and then there's a comment Works around a bug in library XYZ. Comments explaining what the code does in terms of the problem that is solved. Like find all customers who purchased items worth less than $100 in the last year and made more than three returns - this might not be obvious from the code. We have got array, iterate over it as a comment for code iterating over an array is nonsense. I can see that you are iterating, I don't need a comment for that. Tell me what the iterating is actually doing. 