Ronald Rowe

Posted on

# #Commenting Code... Before, During, or After?

I'm not here to give a lesson on commenting code, rather I'm hoping to learn a thing or two. Comment at will!

Commenting code, oh all the things we have heard about this topic. No matter how many different thoughts or ideas there are on this subject, we can agree on a few main points.

• Comments should explain what and/or why, not how.

• Comments need to be brief but informative.

Utilizing the During method of commenting code must be for the experienced developer. The ability to maintain concentration effortlessly while breaking for only a moment to efficiently comment where needed is quite impressive. I get so focused on the task at hand, I'd worry breaking my concentration would cause me to lose my train of thought. This would be ideal to complete projects with as little backtracking as possible. The things I have to work on and look forward to!

Commenting After getting the function working is what I find works best. This makes me take that second look at my work before moving on. As being a newer programmer it is advantageous to self-review your work. You have to backtrack through your code, but you more than makeup for what you lose in efficiency. You often catch easy mistakes, redundant code, or errors that will lead to issues in the future.

The other day I decided to create a little program in Python to find prime numbers in a range with a user inputting the maximum. After getting it working I decided I better practice commenting as well. Redundant Code! Well, that was just the first thing I noticed and fixed. I also had forgotten to check the input value, negative numbers cannot be prime. This was accomplished by just commenting on my code using the After method.

``````primeList = []
numerator = 2
#loop through numerators to be checked
while numerator <= maxNum:
z = 0
#Redundant Code!
#    if numerator % primeList[z] == 0:
#       numerator+=1
#    else:
#         z+=1
#loop through prime factors
#should have iterated using for loop
while len(primeList) > z:
if numerator % primeList[z] == 0:
numerator+=1
break
else:
z+=1