Is it possible to write good and understandable code without any comments?

前端 未结 20 1237
攒了一身酷
攒了一身酷 2020-12-28 18:35

Can any one suggest what is the best way to write good code that is understandable without a single line of comments?

相关标签:
20条回答
  • 2020-12-28 19:08

    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...

    0 讨论(0)
  • 2020-12-28 19:09

    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.

    0 讨论(0)
  • 2020-12-28 19:09

    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.

    0 讨论(0)
  • 2020-12-28 19:10

    I think that the concept of Fluent Interfaces is really a good example of this.

    var bob = DB.GetCustomers().FromCountry("USA").WithName("Bob")

    0 讨论(0)
  • 2020-12-28 19:12

    Clean Code by Robert C. Martin contains everything you need to write clean, understandable code.

    0 讨论(0)
  • 2020-12-28 19:12

    I don't really think comments are a good idea in most cases. Comments don't get checked by the compiler so they so often are misleading or wrong as the code changes over time. Instead, I prefer self documenting, concise methods that don't need comments. It can be done, and I have been doing it this way for years.

    Writing code without comments takes practice and discipline, but I find that the discipline pays off as the code evolves.

    0 讨论(0)
提交回复
热议问题