The question I asked might be closed, But i just want to know that is it necessary to write else part of every if condition. One of my senior programmer said me that \"you shoul
In imperative languages like Java and C, if - else
is a statement and does not return a value. So you can happily write only the if
part and go on. And I think that it is the better practice rather than adding empty else
s after every if
.
However in functional languages like Haskell and Clojure, if
is an expression and it must return a value. So it must be succeeded with an else
. However there are still cases where you may not need an else
section. Clojure, for such cases, has a when
macro which wraps if - else
to return nil
in the else
section and avoid writing it.
(when (met? somecondition)
(dosomething))