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

前端 未结 20 1384
攒了一身酷
攒了一身酷 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条回答
  •  旧时难觅i
    2020-12-28 18:58

    I think comments should express the why, perhaps the what, but as much as possible the code should define the how (the behavior).

    Someone should be able to read the code and understand what it does (the how) from the code. What may not be obvious is why you would want such behavior and what this behavior contributes to the overall requirements.

    The need to comment should give you pause, though. Maybe how you are doing it is too complicated and the need to write a comment shows that.

    There is a third alternative to documenting code - logging. A method that is well peppered with logging statements can do a lot to explain the why, can touch on the what and may give you a more useful artifact than well named methods and variables regarding the behavior.

提交回复
热议问题