Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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;
    }


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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: