CCoding Style - Break up complex expressions

From EdWiki

Revision as of 09:30, 23 June 2015 by Jshankar (Talk | contribs) (1 revision imported: EdWiki -- Emsys pages from Shukra)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Break-up complex expressions

  • C has a very rich expression syntax and operators, and it’s easy to get carried away by cramming everything into one construction.
  • An expression like the following is compact but it packs too many operations into a single statement:
*x += (*xp = (2*k < (n-m) ? c[k+1] : d[k--]) );

It’s easier to grasp when broken into several pieces:

if( 2*k < (n – m) )
    *xp = c[k+1];
    *xp = d[k-1];
*x += *xp;