Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2002
    Posts
    32

    Unanswered: Validating Logic

    I have been working on a script that is just about finished. One question I have is validating the logic.

    The script has become bloated and hard to follow. If I wanted to add some validation then what is the best way?

    I have a table I created and then imported records from a csv file. The csv is from a testing scantron that has exam grades. I want to test the following:

    Check if already imported
    Validate UID and compare to Student_ID in the imported table.
    Check if all Students are assigned to a group.
    Check if any students are missing from import.

    Each part of the script works but the logic does not flow.

    I was thinking about something like this:


    *Just a rough draft*
    Code:
    $Session_ID = $_POST['Session_ID'];
    //Check if already imported
    $checked = checkSession($Session_ID);
    if $checked = 1 {
    die();
    }else{
    //Validate UID and compare to Student_ID in the imported table.
    $checked = checkUID();
    if $checked = 1 {
    die();
    }else{
    //Check if all Students are assigned to a group.
    $checked = checkGroup();
    if $checked = 1 {
    die();
    }else{
    //Check if any students are missing from import.
    $checked = checkMissingStudents(); 
    if $checked = 1 {
    die();
    }
    //No errors detected so import grades to grades table.
    }
    What is the proper way to trap errors and/or return from a function so that it does not continue until all logic errors are fixed?

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    There is no reason why you cannot short circuit the logic
    I'd consider making your function return a true / false
    Code:
    $HasError=FALSE; //don't like settign a flag to OK.. normally Id trip it to true
    $Session_ID = $_POST['Session_ID'];
    //Check if already imported
    if (checkSession($Session_ID) == TRUE)
    { if (checkUID()==TRUE) //Validate UID and compare to Student_ID in the imported table.
      { if (checkGroup()==TRUE) //Check if all Students are assigned to a group.
        { if (checkMissingStudents()==TRUE) //Check if any students are missing 
    from import.
          } else
          { $HasError=TRUE; //we have missing students
          }
        } else
        { $HasError=TRUE; //we have group problems
        }
      } else
      { $HasError=TRUE; //we have userid problems
    } else
    { $HasError=TRUE; //we Session problems
    }
    If ($HasError==FALSE) //No errors detected so import grades to grades table.
    } else
    { //insert error handler / message / whatever
    }
    arguably you could make each statement a single if
    Code:
    $HasError=FALSE; //don't like settign a flag to OK.. normally Id trip it to true
    $Session_ID = $_POST['Session_ID'];
    //Check if already imported
    $HasError = NOT ($Session_ID);
    if ($HasError==FALSE) {$HasError= NOT checkUID();)  //Validate UID and compare to Student_ID in the imported table.
    if ($HasError==FALSE) {$HasError= NOT checkGroup();) //Check if all Students are assigned to a group.
    if ($HasError==FALSE) {$HasError= NOT (checkMissingStudents();) //Check if any students are missing from import.
    If ($HasError==FALSE) //No errors detected so import grades to grades table.
    } else
    { //inseret error handler / message / whatever
    }
    I forget if PHP supports the NOT word, but I'm sure you get the picture
    Although you have commented the code, which is good, there is another black art which is to make the code indented and easy to read, aswell as commented.

    Although its against the PHP convention I prefer my IF statemrnts to be
    Code:
    If (condition comparator value)
    { code block on true;
    } else
    { code block on false;
    }
    as opposed to
    Code:
    If (condition comparator value) {
      code block on true;
    } else
    { code block on false;
    }
    ....which I find harder to read and therefore to debug, but thats just me, and Probably you should stick to the conventional (C++) way of doing it.

    I don't mind using
    Code:
    If (condition comparator value) {statement on true;} else { statement on false;}
    which some purists regard as heresy, but I'd only do it if there is a single statement, and usually only when there is a single outcome
    but for me it makes it easier to understand when reading through the code
    ie
    Code:
    If (condition comparator value) {statement on true;}
    but in essence PHP is not a strictly controlled language and to a certain extent it reflects its origins in the chaos that was early web development.

    I think if the code is easy on the eye its easier to spot errors
    I'd generally avoid using die() on its own, I prefer sending soem message back to the calling script / screen whatever so that I know where, or why it went
    wrong. That way round you know your script didn't do something because the data was banjaxed, as opposed to your script didn't do soemthing because it had some unspecified problem. it may be a message 'Session ID:$SessionID invalid" written to a logfile or screen.

    or you could send the result to a generalised error handler which depending on what environemtn you are in (development, test or live) you could decide where to send the error message, or how much infromation you wish to divulge.

    HTH

  3. #3
    Join Date
    Mar 2007
    Location
    636f6d7075746572
    Posts
    770
    Basically avoid using die() firstly. It's bad practise.

    What you need to do is:
    1) validate (filter) each entry that the user is supplying
    2) track which entries are invalid
    3) if there are invalid entries, go back to the previous section and present the errors.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •