Welcome Guest, Not a member yet? Register   Sign In
Method Question: Return Bool or Return Data?
#1

[eluser]bunal[/eluser]
Hi,

When there is a method that can either return data or false, for example get_user_detail()

What do you prefer?

Way 1:
get_user_detail() return false if no user found
get_user_detail() return array/object if user found

Way 2:
get_user_detail() return false if no user found
get_user_detail() return true if user found
get_user_detail() store user data in $return_get_user_detail variable
Then developers can use $return_get_user_detail variable to get the data
#2

[eluser]Phil Sturgeon[/eluser]
I used to go to the trouble of returning FALSE if no results were found, but I dont see much point for it anymore.

If you just return $query->row() or $query->result() then you can still use $result == FALSE and it wont break a foreach loop for example.

Code:
FALSE == array(); // TRUE
FALSE === array(); // FALSE

Returning array() over FALSE may not be a good practice for a strongly typed language, but we are using PHP so who gives a damn?
#3

[eluser]Jeremy Gimbel - Conflux Group[/eluser]
I almost always agree with what Phil said. So listen to him.

An added note though, your second method, involving setting another variable with the result of the method would be really bad Object Oriented Programming form.

Try to keep things packaged up together as much as possible so they don't rely on external parts. Keeps things cleaner and easier to maintain.

jeremy




Theme © iAndrew 2016 - Forum software by © MyBB