Is it bad style to redundantly check a condition?
- by mcwise
I often get to positions in my code where I find myself checking a specific condition over and over again.
I want to give you a small example: suppose there is a text file which contains lines starting with "a", lines starting with "b" and other lines and I actually only want to work with the first two sort of lines. My code would look something like this (using python, but read it as pseudocode):
# ...
clear_lines() # removes every other line than those starting with "a" or "b"
for line in lines:
if (line.startsWith("a")):
# do stuff
if (line.startsWith("b")):
# magic
else:
# this else is redundant, I already made sure there is no else-case
# by using clear_lines()
# ...
You can imagine I won't only check this condition here, but maybe also in other functions and so on.
Do you think of it as noise or does it add some value to my code?