How do I deal with many levels of indentation?

In many cases, these deeply-nested indentations are the result of deeply-nested error checking. If that’s so for you, you should look into MaybeT and its big brother ExceptT. These offer a clean way to separate the “what do we do when something went wrong” code from the “what do we do assuming everything goes right” code. In your example, I might write:

data CustomError = IfCheckFailed | MaybeCheckFailed

main = handleErrors <=< runExceptT $ do
    inFH  <- liftIO $ openFile ...
    outFH <- liftIO $ openFile ...
    forM myList $ \item -> do
        when (...) (throwError IfCheckFailed)
        ...
        x <- liftMaybe MaybeCheckFailed ...
        ...

liftMaybe :: MonadError e m => e -> Maybe a -> m a
liftMaybe err = maybe (throwError err) return

handleErrors :: Either CustomError a -> IO a
handleErrors (Left err) = case err of
    IfCheckFailed    -> ...
    MaybeCheckFailed -> ...
handleErrors (Right success) = return success

Notice that we still increase indentation at the forM loop; but the other checks are done “in-line” in main, and are handled all at the same indentation level in handleErrors.

Leave a Comment