do professional PHP programmers use error checking in their code?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • B.r.K.o.N.j.A

    #16
    Re: do professional PHP programmers use error checking in their code?

    My own feeling, obviously, is that it is better to error check
    everything, and to write extensive comments everywhere. I've taken over
    PHP projects, started by other programmers, that had no error checking
    and no comments, and such projects are always a big pain in the neck. I
    lose time playing Sherlock Holmes, trying to track down where a
    function's parameter first originates and why it's in use. I'd rather
    have a comment on it, and error message for when the wrong thing is
    passed in. Obviously this slows development. Is there any concensus
    among developers about what is the best approach? I think whatever is
    cheapest for the client should be considered the best approach, but it
    seems to me cheapest-in-the-short-term is quite different from
    cheapest-in-the-long-term.
    In my personal opinion and experience, if I were a project manager and
    I've heard the words "php will handle errors for you, why bother?" from
    any of the programmers I work with, I would fire that person on spot.
    It's HORRIBLE practice and it leads to lots of crap php code out there
    and also the "not serious programmer" stereotype about php developers
    (enforced mostly by java and c# folks - who (you've guessed it :) handle
    their errors and exceptions). So my advice is handle all the errors, USE
    the exceptions as much as you can, log errors for after analysis but
    don't reinvent the wheel (e.g. don't reinvent the text of DB error
    messages, just handle them and give them context).

    --

    B.r.K.o.N.j.A = Bionic Robotic Knight Optimized for Nocturnal Judo and
    Assasination

    Comment

    • Chung Leong

      #17
      Re: do professional PHP programmers use error checking in their code?

      Peter Fox wrote:
      * Distinguish between
      - Errors and exceptions (Exceptions are where something doesn't work
      as normal. Errors are where an incorrect result/action is obtained.)
      Making that distinction is important. That's where the colleague of the
      OP erred. Error handling/reporting in PHP is different not because it's
      a scripting language. It's different because it's designed for web
      application. Unlike desktop apps, the operating environment for web
      apps is usually controlled by the programmer. That allows PHP to make
      the assumption that any error encountered is a programming error and
      accordingly, throwing up a message to inform the programmer. Take
      fopen() for instance. In a desktop app it could fail in normal
      circumstances (end-user mistake etc.) In a PHP app, when it fails it
      usually means the programmer did something wrong, as the file being
      opened can either be a file that's part of the app or a file uploaded.
      The guy is correct is saying that it's pointless to do what PHP does
      for you already.
      For example if (in the UK) I ask for a date then I want it in
      day-month-year order. But you might be used to m-d-y and input to my
      form like that.
      That's not a terribly good example, I must say. User errors should
      always be expected. Handling them is part of a application's basic
      functionality.
      What does the function getRow() return? Here I'll assume it is an alias
      for mysql_fetch_arr ay() which returns FALSE if there's a problem. So if
      the result is to be tested it is good practice to test according to the
      signals in the documentation.
      if(FALSE===getR ow($result)){
      I didn't mention it since it's just an example, but there is really no
      way for that condition to arise. If mysql_num_rows( ) succeeds, then
      mysql_fetch_arr ay() cannot fail, since the rows are in memory already.
      So spend less time on fancyfying the bits that you think might go wrong
      and more on discovering what the important failure modes are; and
      understanding the context in which the information is going to be used.
      The only real failure modes are (a) the DB connection is bad, or (b)
      the table weblogs is missing or don't have the right column names.
      PHP's error reporting mechanism would inform you of both. If one wants
      to be helpful, then say something that's actually informative like "the
      database schema is incorrect, run create_tables.p hp to create the
      database." Otherwise, if all you're writing are messages in the vein of
      "an error happened," then you might as well leave it to PHP.

      Comment

      Working...