Here's an example of my question in python. Notice there's only a very subtle difference: changing an if
to an elif
. There's no difference in behavior; if the first if
statement is executed, the second will never be reached. Is it better practice to use an if
or an elif
in this situation? (Which is better: Option 1 or Option 2?)
Option 1:
def add_two_numbers(a, b):
if not isinstance(a, int):
raise TypeError('a must be an int')
if not isinstance(b, int):
raise TypeError('b must be an int')
return a + b
Option 2:
def add_two_numbers(a, b):
if not isinstance(a, int):
raise TypeError('a must be an int')
elif not isinstance(b, int):
raise TypeError('b must be an int')
return a + b