Is it bad style to redundantly check a condition?

Posted by mcwise on Programmers See other posts from Programmers or by mcwise
Published on 2012-11-05T01:54:04Z Indexed on 2012/11/05 5:13 UTC
Read the original article Hit count: 253

Filed under:
|

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?

© Programmers or respective owner

Related posts about coding-style

Related posts about clean-code