Can any one suggest what is the best way to write good code that is understandable without a single line of comments?
相关问题
- Using XPath to access comments a flat hierachy
- should I write more descriptive function names or
- Python Encoding Comment Format
- How to increase mysql table comments length?
- How to ignore unassigned new in ReSharper?
You usually can turn your comment into a function name something like:
In most cases, yes, you can write code that is clear enough that comments become unnecessary noise.
The biggest problem with comments is there is no way to check their accuracy. I tend to agree with Uncle Bob Martin in chapter 4 of his book, Clean Code:
Most comments are either needless redundancy, outright fallacy or a crutch used to explain poorly written code. I say most because there are certain scenarios where the lack of expressiveness lies with the language rather than the programmer.
For instance the copyright and license information typically found at the beginning of a source file. As far as I'm aware no known construct exists for this in any of the popular languages. Since a simple one or two line comment suffices, its unlikely that such a construct will be added.
The original need for most comments has been replaced over time by better technology or practices. Using a change journal or commenting out code has been supplanted with source control systems. Explanatory comments in long functions can be mitigated by simply writing shorter functions. etc.
Well written code might eliminate the need for comments to explain what you're doing, but you'll still want comments to explain the why.
Use descriptive variable names and descriptive method names. Use whitespace.
Make your code read like normal conversation.
Contrast the use of Matchers in Junit:
with the traditional style of assertEquals:
When I look at the
assertEquals
statement, it is not clear which parameter is "expected" and which is "actual".When I look at
assertThat(x, is(3))
I can read that in English as "Assert that x is 3" which is very clear to me.Another key to writing self-documenting code is to wrap any bit of logic that is not clear in a method call with a clear name.
becomes
In some cases - yes, but in many cases no. The Yes part is already answered by others - keep it simple, write it nicely, give it readable names, etc. The No part goes to when the problem you solve in code is not a code problem at all but rather domain specific problem or business logic problem. I've got no problem reading lousy code even if it doesn't have comments. It's annoying, but doable. But it's practically impossible to read some code without understanding why is it like this and what is it trying to solve. So things like :
look nice, but could be quite meaningless in the context of what the program is actually doing. I'd rather have it like this:
I believe it's possible, if you consider the fact that not everybody likes the same style. So in order to minimize comments, knowing your "readers" is the most important thing.
In "information systems" kind-of software, try using declarative sentence, try to approximate the code line to a line in english, and avoid "mathematical programming" (with the i,j and k for index, and the one-liners-to-do-a-lot) at all costs.