Which of these is better practice?

Posted by Fletcher Moore on Stack Overflow See other posts from Stack Overflow or by Fletcher Moore
Published on 2010-04-29T14:33:18Z Indexed on 2010/04/29 14:37 UTC
Read the original article Hit count: 319

You have a sequence of functions to execute. Case A: They do not depend on each other. Which of these is better?

function main() {
  a();
  b();
  c();
}

or

function main() {
  a();
}

function a() {
  ...
  b();
}

function b() {
  ...
  c();
}

Case B: They do depend on successful completion of the previous.

function main() {
  if (a())
    if (b())
      c();
}

or

function main() {
  if (!a()) return false;
  if (!b()) return false;
  c();
}

or

function main() {
  a();
}

function a() {
  ... // maybe return false
  b();
}

funtion b() {
  ... // maybe return false
  c();
}

Better, of course, means more maintainable and easier to follow.

© Stack Overflow or respective owner

Related posts about best-practices

Related posts about maintainability