You are here: Re: Returning NULL vs. returning FALSE « PHP Programming Language « IT news, forums, messages
Re: Returning NULL vs. returning FALSE

Posted by Dikkie Dik on 11/02/30 11:32

What do you WANT your function to return? If (presuming PHP5 here) there
is a real error and the function cannot reasoable be expected to return
anything, throw an exception.

An exception communicates: "This is not my responsibility". It is the
same situation like in a company, an employee hands back a task to his
boss, saying "I cannot do this in the current situation".
It is now the calling code that can take action (with a catch block) or
step aside and let the exception pass to its calling code.
If I would have to find a better name for an exception,I would have
called it an "objection". Like in the american court-of-law TV series.
If someone raises an objection, the judge (the calling code) decides:
forget the question, ask the question differently, ask it to someone
else, or retry and ask again.

It COULD also be that you want your function to return a default (a NULL
object). Suppose you write an application involving customer objects.
You have some function (method of a collection class) that takes the
customer number and returns the customer with that number from the
database. Now a new customer uses the application and no customer number
is given. You could have your lookup function return a new blank
customer object.

Look at what the requesting code does after it receives the FALSE or
NULL value. If it always creates some new, blank structure, have it
returned by the lookup function.

Best regards.

Thomas Mlynarczyk wrote:
> Hi,
>
> It seems to be a generally adopted convention to have a function return
> FALSE in case of an error. But if a function is supposed to return a boolean
> anyway, one cannot distinguish anymore between the "normal" FALSE and the
> "error" FALSE. So why not using NULL instead to indicate an error? Are there
> drawbacks I am not aware of?
>
> Greetings,
> Thomas
>
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация