Page 1 of 1

Anyone ever use a null switch for data validation?

Posted: Wed Jun 21, 2006 12:12 pm
by Benjamin
Something like...

Code: Select all

switch (null) {
  default:

  if ($data == 'invalid') {
    $ErrorMessage = 'The Data was invalid';
    break;
  }

  // continue validation...

  // everything is ok...

  // insert data into db or something..
}  // end of switch

Posted: Wed Jun 21, 2006 12:15 pm
by feyd
what's the use? I don't see it being helpful..

Posted: Wed Jun 21, 2006 12:15 pm
by Weirdan
What is this for... goto emulation?

Posted: Wed Jun 21, 2006 12:21 pm
by Benjamin
Flow control, for example you have 20 fields to validate, but you want it to stop validation on the first error.

Posted: Wed Jun 21, 2006 12:30 pm
by patrikG
astions wrote:Flow control, for example you have 20 fields to validate, but you want it to stop validation on the first error.
in that case lose the "break;"

Posted: Wed Jun 21, 2006 12:34 pm
by Benjamin
Well it's sudo code. In this example $Data was invalid, so it populated the error message and stopped execution.

Posted: Wed Jun 21, 2006 12:59 pm
by Weirdan
in that case lose the "break;"
Here break acts as an emergency exit lever.

I can see some uses for this technique, yet I would use do{ }while(false); instead:

Code: Select all

do {
  // do something
   if($condition)
       break; // escape
  // do something
} while(false);
Btw, it's documented in PHP manual as well: http://us2.php.net/manual/en/control-st ... .while.php

Posted: Wed Jun 21, 2006 1:03 pm
by Benjamin
I wasn't aware of that, but I would hesitate to use a do while for something that I only want to execute once.

I really like the null switch method, but then again I thought of it myself. Not to say that no one has ever done it before, but I have never seen it done anywhere else at least.

Posted: Wed Jun 21, 2006 1:12 pm
by Weirdan
I wasn't aware of that, but I would hesitate to use a do while for something that I only want to execute once.
It's only a matter of habit. Once you know that switch is a special type of loop you don't see any substantial differences between your method and mine :)

Posted: Sun Jul 02, 2006 7:24 pm
by Skara
Helpful. I have a question, though.

I very basically have this:

Code: Select all

function err($msg) {
  print("error: {$msg}");
}
function dosomething() {
  if (fail) {
    err("message");
    return false;
  }
  return true;
}

switch(null) {
  default:
  if (!dosomething()) break;
  if (!dosomethingelse()) break;
  if (!dosomething3()) break;
}
Is there a way I could do something like this...?

Code: Select all

function err($msg) {
  print("error: {$msg}");
}
function dosomething() {
  if (fail) {
    err("message");
    //break switch here
  }
}

switch(null) {
  default:
  dosomething();
  dosomethingelse();
  dosomething3();
}
It would come in handy with:

Code: Select all

foreach (db_fetch_row()) {

Posted: Sun Jul 02, 2006 9:10 pm
by Benjamin
Not sure, it would probably work though. Might want to have a look at...

http://us2.php.net/manual/en/control-st ... .break.php

Posted: Mon Jul 03, 2006 1:53 pm
by Skara
No, just using break isn't going to work. I think the only solution is to throw an exception.