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

前端 未结 20 1442
攒了一身酷
攒了一身酷 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:07

    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 :

    if (starColour.red > 200 && starColour.blue > 200 && starColour.green > 200){
       doSomething();
    }
    

    look nice, but could be quite meaningless in the context of what the program is actually doing. I'd rather have it like this:

    // we do this according to the requirement #xxxx blah-blah..
    if (starColour.red > 200 && starColour.blue > 200 && starColour.green > 200){
       doSomething();
    }
    

提交回复
热议问题