|
|
|
Is it a bad practice to write (hopefully) unreachable code?
|
|
1 points
by WillowAndrew
2192 days ago
|
|
Let's say you want to union two intervals. The code could look like this: ``` def union(self, other): # Capture special cases
if self.is_empty():
return other
elif other.is_empty():
return self
# Standardize - after this step, the other.left is left of self.left
if other.left > self.left:
other, self = self, other
# Go through all cases
if other.right < self.left:
# Completely disjunct
return IntervalUnion([self, other])
elif other.right == self.left:
# next to each other
return Interval(other.left, self.right)
elif other.right <= self.right:
return Interval(other.left, self.right)
elif other.right > self.right:
# other is a superset of self
return other
else: # noqa
# This should never happen
raise NotImplementedError("Can't merge {} and {}".format(self, other))
```Note that at the very bottom, there is unreachable code. At least I hope it is. If it isn't, then there is a bug. Making an error throw early is good practice. Unreachable code is bad. So ... is this good or bad? |
|