The sins of Adam Bard

(written by lawrence krubner, however indented passages are often quotes). You can contact lawrence at: lawrence@krubner.com, or follow me on Twitter.

This last year I find that everything Adam Bard writes is worth reading. I have not been checking his blog very often but I should. I just saw this post from a year ago, which is really fantastic:

Any time you put a let in a let or an if in an if, that’s a strong code smell. It means you’re still writing code in a way that requires you to short-circuit your functions with returns.

There is a fascinating debate to be had regarding what to do with these initial conditionals for this function:

(defn register [{{:keys [username
                         password
                         email
                         email_repeat
                         gender
                         dob_year
                         dob_month
                         dob_day]} :params :as request}]
(cond

   (or (empty? username) (empty? password) (empty? email) (empty? email_repeat) (empty? gender))
       (fail-registration "Please fill in all fields")

   (not (= email email_repeat))
       (fail-registration "Whoops! You entered the wrong email address.")

   :else
   (let [dob [(Integer/parseInt dob_year) (Integer/parseInt dob_month) (Integer/parseInt dob_day)]
         [usr err] (user/create-user! {
                                      :username username
                                      :password password
                                      :email email})]

Possibly these should be caught with preconditions, perhaps using Dire, with higher level functions responding to the Exceptions, or with middleware responding to the Exceptions, or possibly this should be handled as many separate functions, or perhaps some of these conditions should be checked in middleware, with an appropriate error message returned.

Post external references

  1. 1
    http://adambard.com/blog/five-mistakes-clojure-newbies-make/
Source