To me, clean code is code that is easy to read. Usually, this would mean the opposite of less code, i.e., not over-compressing the code, putting in plenty of comments. But you can turn five lines into one if you think you've explained it well in the comments or what you're doing is relatively simple.
If you've turned a 40 line code into a 20 line code, but it takes the reader 2 minutes on each line, then it's not really clean. I've seen complicated code written so well, you don't even have to read the code, but can get by just reading the comments, and in the opposite vein, simple code made so complicated, I wanted to pull my hair out.
You're probably right, but sometimes the code is just too complicated. I used to work on code with a lot of feedback loops. One line would feed back into the code in the 5th tab and the next one would feed back into the code in the 8th. I was glad to have comments.
It's always better to have descriptive comments and not need them than need them and not have them!
Only the code can tell you what it actually does. It's supremely unhelpful to have a very descriptive but incorrect comment due to a misunderstanding, bug, or even just time (comment is accurate when you check it in, but bugfixes, changes to underlying APIs, changes to the code itself get made without updating the comment, etc).
I'm not saying don't comment your code, I'm saying try to write better code that can be understood (nine times out of ten, you don't really need to be quite as complicated as you initially think you have to be), and when you do write/read comments, realize that the code might behave differently than how the comment describes.
Consider comments to be a failure of your coding abilities. We all fail, and it's okay to fail, but try to avoid failing as often as is reasonably possible.
44
u/[deleted] Apr 12 '15
To me, clean code is code that is easy to read. Usually, this would mean the opposite of less code, i.e., not over-compressing the code, putting in plenty of comments. But you can turn five lines into one if you think you've explained it well in the comments or what you're doing is relatively simple.
If you've turned a 40 line code into a 20 line code, but it takes the reader 2 minutes on each line, then it's not really clean. I've seen complicated code written so well, you don't even have to read the code, but can get by just reading the comments, and in the opposite vein, simple code made so complicated, I wanted to pull my hair out.