Of course everyone thinks they always write good clean code and therefore don't need comments to elaborate on what the heck is going on. Unfortunately, having been doing this trade for 30+ years, I've found most people write crappy code in a hurry to try to hit some deadline based on incomplete requirements and confusing business rules. A few precious comments stuck in there can help the next guy, months or years lat…
How can you write clean, simple comments if you cannot write clean, simple code.
I've never seen a language's style guide recommend avoiding comments before
131–140 of 198 posts
Re: I've never seen a language's style guide recommend avoiding comments before
#132Similar thoughts were seen in the SICP book. "In this book we don't use many comments; we try to make our programs self-documenting by using descriptive names." http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-15.html...
In modern Lisp, though, it's still considered good form to include both a docstring, and internal comments explaining anything particularly tricky.
Re: I've never seen a language's style guide recommend avoiding comments before
#133I've seen this many times: ...thus they (comments) tend to diverge from actual implementation. It happens, you update/refactor code, and forget to update the comments. Thus the comments are outdated or worse not applicable anymore. Common mistake by less-detailed oriented developers. Begs the question, in this case is is better to have confusing/incorrect comments, or no comments at all?
" ...thus they (comments) tend to diverge from actual implementation." In my experience comments are no more likely to diverge than tests are. With tests you have the advantage that they must compile. With comments you have the advantage that they are inline/interspersed with your code and so get read every time the code is read. "It happens, you update/refactor code, and forget to update the comments." So then the n…
Just as a heads up, there are lots of testing paradigms that have the tests inline with the code it is testing. That they aren't the default in most languages is most disappointing.
Re: I've never seen a language's style guide recommend avoiding comments before
#134Comments say what your code does, your code says how you do it. The swap example is trivial, but for most functions it is good to add an API comment, because how you use the function shouldn't depend on how it's implemented, but what it should do, described in the comment. That way you can change your implementation as long as you don't change the contract. In other words, changing how your code does something should…
Well, this is Haskell we're talking about. Between the name, type signature and the fact that most functions are pure (so, no side-effects to describe), it's often obvious what they do.
Re: I've never seen a language's style guide recommend avoiding comments before
#135Of course everyone thinks they always write good clean code and therefore don't need comments to elaborate on what the heck is going on. Unfortunately, having been doing this trade for 30+ years, I've found most people write crappy code in a hurry to try to hit some deadline based on incomplete requirements and confusing business rules. A few precious comments stuck in there can help the next guy, months or years lat…
> Comments cost essentially nothing to add to your code and can save it from an early death and complete refactoring by the next guy who comes along. Whatever their benefits are, this is not true at all. Comments are expensive to write and maintain. They are VERY VERY expensive to maintain because there is no automated way to test them.
Re: I've never seen a language's style guide recommend avoiding comments before
#136There are times when you absolutely want to comment something like an "Add" or "Swap" function. eg. // The reason we use a custom swap function instead of // the one that is shipped with the framework is because // of an edge-case that occurs quite frequently in our // scenario // Refer to Change Request 345. void Swap (Foo a, Foo b) ....
I think you are missing the point there. Sure, I completely agree with you that those comments are crucial, but they are not informing about the code doing a swap, they are explaining the need of the applied workaround.
Re: I've never seen a language's style guide recommend avoiding comments before
#137Earlier quoted context omitted.
Isn't that fairly easy to do during a code review?
Sure, code review is a very manual approach.
Re: I've never seen a language's style guide recommend avoiding comments before
#138Of course everyone thinks they always write good clean code and therefore don't need comments to elaborate on what the heck is going on. Unfortunately, having been doing this trade for 30+ years, I've found most people write crappy code in a hurry to try to hit some deadline based on incomplete requirements and confusing business rules. A few precious comments stuck in there can help the next guy, months or years lat…
Re: I've never seen a language's style guide recommend avoiding comments before
#139Earlier quoted context omitted.
There is no such complex function that can't be decomposed to bunch of simple(r) functions.
Often only with a deep understanding of the complex problem to understand its simplicity. But you have to ship in 4 weeks, so why not just get something working first?
Of course, this sometimes happens, but it shouldn't be encouraged. This way you are creating technical debt, which will be very expensive to pay off.
Re: I've never seen a language's style guide recommend avoiding comments before
#140Earlier quoted context omitted.
Completely agree :) In some cases I prefer to wrap it in an aptly named function, but in other cases I prefer the whole algorithm to be in a single function, making comments a very useful tool for "naming".
in other cases I prefer the whole algorithm to be in a single function A modern compiler can inline most of your function calls if you like. That way you can factor the code appropriately for both concerns.
Please note that in many cases putting an algorithm in a single function is the wrong choice but there is also a cost in splitting it.