What can be the use of comment if it is not displayed ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What can be the use of comment if it is not displayed ?

31st May 2021, 3:47 AM
Harsh
Harsh - avatar
12 Answers
+ 4
You can also use it to spot errors
31st May 2021, 3:59 AM
Razan
Razan - avatar
+ 4
Jan Markus Pardon me, I think all what you have said signify wrong usage of comments. 1. Give variables descriptive names so that you don't need to explain their meaning. 2. A functions parameters are already listed when defining a function. Why list it again in the comments. 3. In a static typed languages. Your function's return type is declared explicitly in the code. For dynamic typing, even if you specify return type in comments - without static type checkers - someone might still need to confirm the type by reading your entire function. Comments don't really help here. 4. If you ever write such a complex code as described. The solution is not to write comments. It does not make it any less complex. The solution is to REFACTOR the code so that it is no longer complex. In short, spaghetti is spaghetti, with or without comments. Static type checkers, clean code and unit tests should be prioritised over writing comments IMO.
31st May 2021, 7:05 AM
Ore
Ore - avatar
+ 4
Jan Markus Yeah. Python is still a dynamic language. It is a design choice not a bug. If you need to enforce type checking there are some open source python libraries called static type checkers that scan your code for type annotations and raise a warning if there might be cases of data type mismatch. However even without static type checkers, I think type annotations are a better way to signify types (instead of comments) since they are part of the syntax. With comments, sometimes you forget to edit comments after the value changes. This will only confuse anybody that works on the code. In such a case it would have been better not to have used comments at all.
31st May 2021, 7:44 AM
Ore
Ore - avatar
+ 3
Jan Markus In later versions of Python you can add type annotations. def sum(a: int, b: int) -> int: return a + b You may also be interested in variable annotations and type comments. https://docs.python.org/3/library/typing.html Also you don't have to use long variable names if your program is well structured. For example, if there is function called get_account_details and has a parameter called account_owner. Your program will be more readable sum_of_interests = get_account_details('Mr. Smith')
31st May 2021, 7:32 AM
Ore
Ore - avatar
+ 3
P.M. I ran it multiple times. Some times typed was faster than untyped. But the difference is always about 1-3 milliseconds. I think it does not make a big difference in practical situations.
31st May 2021, 3:29 PM
Ore
Ore - avatar
+ 2
Comments will increase code readability. Just imagine thousands lines of codes without comment. It will be a problem to work on
31st May 2021, 8:34 AM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 2
Jan Markus i recently made a code that compares typed vs untyped function's speed, seems like the untyped function wins🤔 https://code.sololearn.com/c02VSEJg9Zi4/?ref=app
31st May 2021, 3:09 PM
Bot
Bot - avatar
+ 2
Jan Markus maybe that's because it doesn't matter in python as python isn't speed oriented. last time i checked the Rust language's github repo, i found an RFC where they celebrated over a 2% improvement😶
31st May 2021, 6:15 PM
Bot
Bot - avatar
+ 1
Thanx guys. I understood everything well.
2nd Jun 2021, 3:23 AM
Harsh
Harsh - avatar
0
annotations...
31st May 2021, 3:54 AM
visph
visph - avatar
0
Pls tell in detail......
31st May 2021, 3:55 AM
Harsh
Harsh - avatar