function return values

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

function return values

Post by s.dot »

Are any of these ways of returning, invalid or could pose a problem?

Code: Select all

private function _compareHashes($hash1, $hash2)
{
	return $hash1 == $hash2;
}

private function _compareHashes($hash1, $hash2)
{
	return $hash1 == $hash2 ? true : false;
}

private function _compareHashes($hash1, $hash2)
{
	if ($hash1 == $hash2)
	{
		return true;
	}

	return false;
}
I've been using the first one a lot lately, using operators to be evaluated in the return statement. I'm unaware of if this is practiced a lot, or if it could potentially cause anything different than an if{} statement.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

It all seriously depends on your personal preference and readability. I would lean towards the first example though.. although I usually wrap my expressions in brackets.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Jcart wrote:It all seriously depends on your personal preference and readability. I would lean towards the first example though.. although I usually wrap my expressions in brackets.
Any reason for that codewise? Or just readability?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Technically, it would be the most likely to have the least number of opcodes thereby executing a fraction of a second faster, depending on how efficient the opcode optimizer is.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

First, for me. Especially since this a private method, and therefore should be small and easily refactorable should such a procedure become necessary.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

#1 FTW!

It's easiest to read. period. It takes a lot of page requests to make up for the time lost due to overly-optimized code.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

They are all identical to the code that calls the function. The question then becomes which is the most readable, consistent and maintainable considering the rest of your codebase.

The first would be my preference as well -- mainly for readability. With it I can see most clearly what is being returned. The others require scanning multiple lines or operators.
(#10850)
Post Reply