|
|
|
|
|
by Jtsummers
1432 days ago
|
|
You shouldn't need to raise exceptions for early returns. You can usually restructure your program to provide for the proper return without needing to use a return or comparable form when using an expression oriented language. If you had this C snippet, for instance: int foo(...) {
if (condition) return an_expression;
...
return another_expression;
}
You'd put everything after that if into the else part instead of dropping it (like you would in C): (defun foo (...)
(if condition
an-expression
(progn
...
another-expression))) ;; and this /progn/ form would possibly be extracted to a new function if it was too long
Or more likely a cond if using Common Lisp and there were more than two cases: (defun foo (...)
(cond (condition an-expression)
...
(t
...
another-expression)))
And in Common Lisp (and some others) you can still get a return form, using return-from in particular, from functions if you want to keep something more like the C-ish form: (defun foo (...)
(when condition (return-from foo an-expression))
...
another-expression))
|
|
The first few examples are what I was more familiar with, and it is precisely your note “and this /progn/ form would possibly be extracted to a new function if it was too long” which makes me want to just return early in some cases. Sometimes a case is common enough that it warrants a separate function, but having to define functions for incredibly specific cases just to avoid excessive indentation is not ideal for me.