Hacker News new | ask | show | jobs
by Cyberdog 2264 days ago
It reduces code duplication at the cost of increasing complexity and error-prone-ness, and there are ways to reduce the duplication. For example, given the following snippets:

    switch ($foo) {
      case 123: 
        // do 1
      case 23:
        // do 2
      default:
        // do 3
    }
    
    if ($foo == 123) {
      do1();
      do2();
      do3();
    }
    else if ($foo == 23) {
      do2();
      do3();
    }
    else {
      do3();
    }
Yes, the second one is more verbose and duplicates code, but I think it's also far more clear in terms of intention.
1 comments

You can even take it a step further:

    if ($foo == 123) {
      do1();
    }
    
    if ($foo == 123 || $foo == 23) {
      do2();
    }
    
    do3();