top | item 17410079

(no title)

dwc | 7 years ago

I like and use early exits when they fit well, which is often. And in this case I'd probably do as you suggest. But it's worth mentioning that even if you stick with if/else it's often worth it to put the simple case first to reduce mental load:

    if cache.contains(key) {
      return cache.get(key)
    } else {    
      v = db.query("..")
      expiry = calcexpiry(v)
      cache.put(k, v, expiry)
      return v;
    }

discuss

order

muxator|7 years ago

This example makes me whish an early return even more. Wouldn't it be sweet?