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?
If you really want to then you would need to be very detailed in your variable names and methods names.
But in my opinion, there is no good way to do this. Comments serve a serious purpose in coding, even if you are the only one coding you still sometimes need to be reminded what part of the code you're looking at.
If you want to code entirely without comments and still have your code be followable, then you'll have to write a larger number of shorter methods. Methods will have to have descriptive names. Variables will also have to have descriptive names. One common method of doing this is to give variables the name of nouns and to give methods the names of verbal phrases. For example:
Use
enum
s liberally. With anenum
, you can replace mostboolean
s and integral constants. For example:vs
I like to 'humanise' code, so instead of:
I'll do this:
I once had a professor when I was in college tell me that any good code should never need any comments.
Her approach was a combination of very precise logic split out into small functions with very descriptive method/property/variable names. The majority of what she presented was, in fact, extremely readable with no comments. I try to do the same with everything I write...
I think that the concept of Fluent Interfaces is really a good example of this.
var bob = DB.GetCustomers().FromCountry("USA").WithName("Bob")
Clean Code by Robert C. Martin contains everything you need to write clean, understandable code.