Where should comments be added in code?

Question

In the context of this exercise that introduces comments, where should comments be added to the code? Can I just add comments to every line of code to be safe?

Answer

Generally, comments should only be added to pieces of code that would not be obvious to others who are reading it. Adding comments to every line of code is usually not necessary, and having too much text in a file can overcrowd it, ultimately making it harder to work with or understand.

Comments help provide context for why a particular piece of code was written in that way, and also help others understand the code itself faster. Comments are typically added above the code they are describing.

# When hitpoints are below 5% and shields are gone, self-destruct.
if hp < 5 and shields == False:
  self_destruct()
23 Likes

this is a great web site

30 Likes

this website is dope and is easy to understand

14 Likes

this website is dope and is easy to understand

3 Likes

This is my first question on Codecademy :sweat_smile:
how does the computer know that the 5 represents a percentage and not something else?
Kind regards

5 Likes

This will be set somewhere else in the code. The computer knows it’s a number because it is preceded by a ‘<’ (less than) symbol.

2 Likes

That’s where the comment is useful. The computer just sees 5 as an integer. From the comment we can see that 5% is the threshold for the potential invocation of self_destruct(), so it would be logical to assume that the maximum possible value of hp is 100. Therefore, 5 is equivalent to 5%. At least that’s my impression.

23 Likes

Cool website! Can’t wait to learn how to start coding

9 Likes

Normally, comments in Python are only added whenever and wherever you need them. They can be used for different purposes, and yes, you can add comments to every line, or above a set of lines to know what it happening.

I hope this helped, thanks!

  • Java
2 Likes

#i love coding and I will like to do get better at doing it

3 Likes

#I would say before every block of code is where comments should be added to help others follow the application with ease.

2 Likes

`this code will be used to change the languge many times

code_changing_language

`

1 Like

#i will do it really seriously

1 Like

#Want to learn something I can use to make more than 11$ an hour =D

4 Likes

Nice, but the problem with the step two( 2.comments), is that I cant to write anything…
Only black screen… sorry for bad English.

1 Like

Do you mean that when you have this in the middle panel:

capture

… and then press “Run”, there is nothing but a black screen in the right-hand panel?

That is the expected behavior. The purpose of the # tag is to tell Python to ignore everything after it. The next line, persnickety_count = 0, initializes a variable, but does not call for anything to be printed or otherwise sent to the screen.

So when the script runs, Python ignores the comment (# and after), then does the variable assignment, then halts, just as requested.

If you’d like to see something on the screen, type on a new line (where the cursor is shown above):

print(persnickety_count)

… and then press “Run”.

14 Likes

Comments typically belong at the top of a functions, class, or file. If it becomes obvious the programmer did not completely understand the problem you end up with “Shotgun Surgery”. Comments when and whereever, rewrite the code for your own sake. If you are given that much time. :slight_smile:

This code will be the best

simple_code_calculation()

Peaks my interest in 2 pages this is GREAT!

Reading you answer gave me an idea for another question.

Do comments slow down the code when it is executed?
To say the code is less streamlined?