Avoiding Bad Comments - JetBrains Academy - Learn Programming by Building Your Own Apps
Avoiding Bad Comments - JetBrains Academy - Learn Programming by Building Your Own Apps
Table of contents:
As you already know, a Python feature (beloved by all Python programmers) is its
well-readable syntax. However, apart from the syntax itself, there are other ↑ Avoiding bad comments
important things that contribute to the readability of your program. We assume
§1. When not to write comments?
that you are already familiar with comments and how they help in learning a new
language. §2. How to write good comments?
In real programs, comments become especially important as the program gets §3. Conclusion
bigger and more complicated. Without them, things may get confusing even for
you within a few months after writing the program, not to mention other Discussion
developers who see your code for the first time. However, there is also a downside
of making comments, meaning that more is not necessarily better, and we will
discuss it below as well.
1 n = 0 # number of participants
2 participants_num = 0
Avoid writing obvious comments, like the one below. They make your code
redundant and even harder to read. You should always seek to follow the
D.R.Y. (don't repeat yourself) principle, not W.E.T. ("wrote everything twice" or
"wasted everyone's time" for more cynical ones).
1 age = 20 # age is 20
2 greeting = 'hello' # assign 'hello' to greeting
If you see a way to alter your code so that comments would become
unnecessary - you should do that.
That is, if you can avoid commenting — you'd better do. Then your code would be
clean, wouldn't be overloaded with unnecessary details and wouldn't become
more complicated rather than clearer for readers to understand.
Make sure that your comments do not contradict the code (that happens
more often than you can imagine!). Wrong comments are worse than no
comments at all.
Do not forget to update the comments if you modify the code. It will only
confuse the reader instead of helping them. In the example below, the
variable "counter" used to be designated as "i"; the programmer changed its
name in the code but not in the comment.
Following these pieces of advice, you can write code that is clean, organized, easy
to understand, and pleasant to read.
§3. Conclusion
When annotating the code, it's important to know where to draw the line. Both
over- and undercommented programs can be difficult to understand, which results
in wasted and unpleasant time spent working with such pieces of code. So, you
should always try to write comments carefully and only when it's necessary.
The simplest way to learn to do so is just by doing it. It's a good idea to start
practicing when you only start coding because you will get used to it, and by the
time some more complex problems should be solved, you will know how to write
comments properly. From now on, try to include simple comments in your code to
explain difficult moments that took you a while to understand. It is also useful to
get back to review your older programs and see how they (including comments)
could be enhanced.
Report a typo
1603 users liked this piece of theory. 22 didn't like it. What about you?