Hacker News new | past | comments | ask | show | jobs | submit login

I've tackled some problems that would have ideally, from a performance perspective, been implemented with very deeply nested if()'s. I've found a truth table like layout of bool evaluations to be a nice compromise between maintainability and performance:

  void doit(const tststructptr* in) {
    assert(in);  /* Because we test our code, for safety */
    const tststructptr t = *in;
    if      ( t.t1 &&  t.t2 &&  t.t3) { f1(); }
    else if ( t.t1 && !t.t2 &&  t.t3) { f2(); }
    else if ( t.t1 &&  t.t2 && !t.t3) { f3(); }
    else         /* TODO: Fix, this should never happen */
      releasenerveagent(SLEEPINGQUARTERS);
  }
A long sequence of ternary operators allow for a prettier layout, but is more error prone.



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

Search: