""" MakeAnIfElseLadder.cobra Consider possible alternatives to an if-else ladder: * Make a class hierarchy and send a message to the object. * Use the `branch` statement (see MakeABranchStatement.cobra) When using an if-else ladder, consider throwing a FallThroughException() at the bottom if you expect that it should never happen. See also: MakeABranchStatement.cobra, CheckInheritanceAndImplementation.cobra. """ class Program def main t = Thing() if t.isSmall print 'small' else if t.isMedium print 'medium' else if t.isLarge print 'large' else throw FallThroughException(t) # ^ As seen here, you can (optionally) include a value with the # fall-through exception which is the value that was being examined. class Thing """ A thing is always small, medium or large, and never more than one. """ get isSmall as bool return false get isMedium as bool return true get isLarge as bool return false |
How To Make An If Else Ladder