Re: do professional PHP programmers use error checking in their code?
- From: onembk <onembk@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 1 Sep 2006 23:59:24 -0600
On 2006-09-01 22:29:16 -0600, "lawrence k" <lkrubner@xxxxxxxxxxxxx> said:
I've made it habit to check all returns in my code, and usually, on
most projects, I'll have an error function that reports error messages
to some central location. I recently worked on a project where someone
suggested to me I was spending too much time writing error messages,
and that I was therefore missing the benefit of using a scripting
language. The idea, apparently, is that the PHP interpreter writes all
the error messages that are needed, and that I shouldn't write such
code myself. I was given the impression that if I needed extensive
error checking, or strict typing, then I should use a real language,
like Java, but if I'm going to use a scripting language like PHP or
Ruby, then I should leave errors to the interpreter, since the whole
point of using scripting languages is speed of development. Has anyone
else heard this argument, and do you agree with it? I'm wondering how
other PHP programmers handle error messages. Check everything or leave
it to the PHP interpreter to tell you when there is an error?
Which of these two functions is better, the one with error checking or
the one without?
function getWeblogEntries() {
$query = "SELECT * FROM weblogs";
$result = mysql_query($query);
$howManyWeblogEntries = mysql_num_rows($result);
for ($i=0; $i < $howManyWeblogEntries; $i++) {
$row = getRow($result);
extract($row);
echo "<h1>$headline</h1>";
echo "<h7>$date</h7>";
echo "<div class=\"mainContent\">$mainContent</div>";
}
}
function getWeblogEntries() {
$query = "SELECT * FROM weblogs";
$result = mysql_query($query);
if ($result) {
$howManyWeblogEntries = mysql_num_rows($result);
for ($i=0; $i < $howManyWeblogEntries; $i++) {
$row = getRow($result);
if (is_array($row)) {
extract($row);
echo "<h1>$headline</h1>";
echo "<h7>$date</h7>";
echo "<div class=\"mainContent\">$mainContent</div>";
} else {
reportError("In getWeblogEntries, the function getRow
failed to return an array on the $i iteration.");
}
}
} else {
reportError("In getWeblogEntries, the query to the database
failed.");
}
}
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.
I agree with you, although I think the extent you take it to depends on the needs project. Having quality error reporting is much different than interpreter errors. An error like:
"You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') VALUES ( 1, 2, 'asdf', 'asdf',' at line 8"
doesn't tell you much other than there is problem. A function like:
function myFunction($sql) {
$result = @mysql_query($sql, $connection);
if (!is_resource($result) && $result !== true) {
die(mysql_error()." (Full query: ".$sql.") in function myFunction');
}
return $result;
}
can save you lots of debugging, doesn't really take any extra time to write and gives the added benefits of showing both the interpreter's error, the full context of the error and the function used. But this just describes an error that would kill the script anyway. What if, as in your example, you needed program behavior to change based on external influences (I.E. user input, database results, etc.)? In any 'real' language (PHP is just as real as Java) you would be foolish not to have error checking and reporting. Why wouldn't that apply to an interpreted language? In the end, what's the difference other than the fact that it's not compiled into bytecode (you can compile PHP btw, http://www.roadsend.com/home/index.php?SMC=1&pageID=compiler)? Most interpreter errors are comparable to compiler errors, and data checking in otherwise 'compilable' code is a necessity in any language.
Granted, I think you should take advantage of PHP's abilities when you can and when it makes sense (better not to re-invent the wheel, a tired phrase I know) but that hardly covers everything.
.
- References:
- do professional PHP programmers use error checking in their code?
- From: lawrence k
- do professional PHP programmers use error checking in their code?
- Prev by Date: Re: this function never pads integers with leading zeros - help
- Next by Date: Re: do professional PHP programmers use error checking in their code?
- Previous by thread: Re: do professional PHP programmers use error checking in their code?
- Next by thread: Re: do professional PHP programmers use error checking in their code?
- Index(es):
Relevant Pages
|