Programming without comment

If it is good to spread your code with multiple comments, then having countless comments in your code would be great , right? But that is not true at all. Beyond the limit is a way for good comments to get bad:

Chúng ta phải viết comment trong code sao cho hiệu quả nhỉ? Do we have to write a comment in the code so that it works?

I always come across comments from programmers that seems they do not understand that the code that actually tells us how it worked then; We need to comment to talk about why it works like that. Comments in code are often misunderstood and abused so often that you sometimes wonder if it's worth using comments at all? But be careful with your wish. This is a code without any comment:

Do you have any thoughts on what the code does? It is completely readable, but what the hell is it doing?

Please add a comment line.

Is that what I'm looking for, right? It's a bit more pleasant, and has reached the midpoint of the compromise path between the two extremes, there's no comment at all and there is a negative comment after every 2 lines of code?

Not exactly that. Instead of adding a comment, I prefer to edit it like this:

I didn't add any comments, and now that cryptic code is as clear as day.

While comments are either good or bad, they are often used as a support. You should always write code as if the comment lines do not exist. This forces you to write your code so that it is the simplest, most obvious, and almost that code itself says what its function is.

When you've rewritten, refactored, and rearchitected your code dozens of times to make it easy for your fellow programmers to read and understand – when you can't imagine any way So your code can become clearer and easier to understand – well, and only then , will you feel reluctant to add a comment to explain what the code does.

As Steve points out, this is a key difference between junior and senior programmers:

In the past, having to look at too much code at a time often exceeded my threshold of complexity, and when I had to work with it, I often tried to rewrite it or at least I wrote a lot of comments. However, today I only work passionately through it without complaining (much). When I had a definite goal in mind and a complex piece of code to write, I spent my time doing it rather than just telling my story about it [with comments].

Junior programmers rely on comments to tell the story when they should rely on the code to tell the story. Narrative comments; important in their own way, but there is no way to replace the plot, features and settings.

Perhaps it is a slightly dirty bit of code comments: to write good comments, you must be a good writer. Comments are not code for compilers, they are words to convey ideas to others. While I (almost) really love my developer colleagues, I can't say that communicating effectively with others is our strength. I have seen emails up to three paragraphs from my team programmers that made me feel my brain melting. Are these the people we believe in writing clear and understandable comments in our code? I think maybe some of us must become more attached to our strengths – that is, write the compiler in the most obvious way we can, and only use comments when that is the last resort.

Writing good and meaningful comments is often difficult. It is as difficult as the art of writing code; even harder than the other. As Sammy Larbi said in the article Common Excuses Used To Comment Code : if you feel your code is too complex to understand without a comment, your code can be very bad. Write it down until you don't need any more comments. If you've finished that effort, but you still feel the comments are needed, add comments. But be careful.

ITZone via Vinacode

Share the news now