Re: Return or not to return, that is the question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



At 5/30/2007 05:41 AM, Richard Davey wrote:
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

If that was wrapped in a function, sticking 'return false' within the
connect_error check is useful why exactly? Equally the fact the
function didn't 'exit' implies it 'returned true' anyway, so why check
it again in whatever called the function in the first place? it has
performed its task, it didn't raise an error.

(I know most of us would never use 'exit' in production code like the
above, so replace it with whatever error handling mechanism you have,
the question above remains the same.)


I demur at your final point: If we don't use exit() and the function performs non-aborting error handling, it's going to return to the calling function which in most cases will need to know whether its child function succeeded or failed.

In most of the applications I write, an SQL error (not merely an empty result set) indicates more often than not that the parent code should gracefully withdraw from the process it was attempting to perform. SQL errors are going to indicate a syntactical error in the query, a missing table or field, a connection failure, or another problem serious enough that the developer's attention should be drawn to it. It's certainly possible in a thoughtfully-written application for a parent function not to care whether a child SQL query was successful on this fundamental level, but in most apps we'll want to know.

        function parent()
        {
                lookUpData();
                displayData();
        }
        function lookUpData()
        {
                set up query;
                execute query;
                handle errors;
        }

where "handle errors" might range from returning a failure flag to displaying an error message.

In order that displayData() doesn't fall on its face, I would write the parent function in one of these ways:

        if (lookUpData()) displayData();

in which lookUpData() returns true or false, the record set being passed in a global variable (ugh);

or, if displayData() is smart enough to deal intelligently with a null or empty result set:

        $aResultSet = lookUpData();
        displayData($aResultSet);
or:
        displayData(lookUpData());

in which lookUpData() returns a dataset array that's empty if no records were found or an error was encountered.

In my programming style, I can't imagine wanting to write this code in such a way that lookUpData() didn't return some form of success or error indicator.

Regards,

Paul
__________________________

Paul Novitski
Juniper Webcraft Ltd.
http://juniperwebcraft.com
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux