TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-18-2008, 01:45 AM   #1 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Confused Question about 'return' statement in UDF...

I'm having trouble understanding a concept...(nothing new about that...) Here it is:

Here is UDF#1:

function udf_1($number) {
$newnumber = ($number + 100);
return $newnumber ;
}

Here is UDF#2:

function udf_2($number) {
return $number + 100 ;
}

My question is, why would it ever be beneficial to use udf_1() over the more simple udf_2()?

Thanks!
Dave
Dave is offline  
Reply With Quote
Old 04-18-2008, 01:57 AM   #2 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 264
Thanks: 2
TlcAndres is on a distinguished road
Default

When your using longer functions with longer mathematical equations or longer bits of code then it becomes easier to read.
__________________
"What everyone seems to forget is that while knowledge certainly is something - it's the implementation of knowledge that brings power" - Andres Galindo.
TlcAndres is offline  
Reply With Quote
Old 04-18-2008, 02:24 AM   #3 (permalink)
The Addict
 
zxt3st's Avatar
 
Join Date: Apr 2008
Posts: 200
Thanks: 18
zxt3st is on a distinguished road
Default

the first one also is more elaborated and more easy to understand..while the second one is quite a shortcut method in returning a variable..:P

simplified one is more easy to read and debug..:D
zxt3st is offline  
Reply With Quote
Old 04-18-2008, 02:42 AM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

As the others have mentioned, it is easier to read when it becomes more complex. Take the following for instance:

php Code:
$szFilename = 'image.jpg';
$szExtension = strtolower(array_pop(explode('.', $szFilename)));
printf('The extension is: %s', $szExtension);

Although this may seem nicer because it is shorter, we also run into readability issues. As a programmer you should be aware of the amount of memory used for your applications, but not paranoid. If it reads nicer, and also easier to debug, by utilising a couple more variables then I would say take that approach.

We could thus refactor the above to something like the following:

php Code:
$szFilename = 'image.jpg';
$aParts = explode('.', $szFilename);
$szExtension = end($aParts);
$szExtension = strtolower($szExtension);
printf('The extension is: %s', $szExtension);

Now you may think that's quite wasteful. However, in the first instance we are assuming everything will be fine and dandy, but being programmers, we know it's never that simple, and so now we've broken it up, we can add some conditional statements to check if everything really is fine and dandy:

php Code:
$szFilename = 'image.jpg';
$aParts = explode('.', $szFilename);

if(count($aParts) == 1)
{
    printf('Unable to find the extension for %s', $szFilename);
    return;
}

$szExtension = end($aParts);
$szExtension = strtolower($szExtension);
printf('The extension is: %s', $szExtension);

Additionally, if you show let PHP notify you of every little thing, you will see that PHP do not recommend encasing functions within functions, as in our first example.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 04-18-2008, 02:53 AM   #5 (permalink)
The Addict
 
zxt3st's Avatar
 
Join Date: Apr 2008
Posts: 200
Thanks: 18
zxt3st is on a distinguished road
Default

best agree with that example..:D
zxt3st is offline  
Reply With Quote
Old 04-18-2008, 03:02 AM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Ach! Not necessarily. Challenge me !
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 04-18-2008, 03:10 AM   #7 (permalink)
The Addict
 
zxt3st's Avatar
 
Join Date: Apr 2008
Posts: 200
Thanks: 18
zxt3st is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
Ach! Not necessarily. Challenge me !
nah..no i wont...
zxt3st is offline  
Reply With Quote
Old 04-18-2008, 09:28 AM   #8 (permalink)
The Frequenter
Zend Certified 
 
Join Date: Sep 2007
Location: Denmark
Posts: 352
Thanks: 8
Kalle is on a distinguished road
Default

Personally I would go off solution two, but thats many because I read both examples the same and I don't see a reason to assign alot of variables in favour of readability =)
__________________
Send a message via MSN to Kalle Send a message via Skype™ to Kalle
Kalle is offline  
Reply With Quote
Old 04-18-2008, 11:42 AM   #9 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Wildhoney View Post
...
We could thus refactor the above to something like the following:
...

php Code:
$szFilename = 'image.jpg';
$aParts = explode('.', $szFilename);

if(count($aParts) == 1)
{
    printf('Unable to find the extension for %s', $szFilename);
    return;
}

$szExtension = end($aParts);
$szExtension = strtolower($szExtension);
printf('The extension is: %s', $szExtension);
Quote:
Originally Posted by Wildhoney View Post
Ach! Not necessarily. Challenge me !
I'll challenge you. In terms of readability, does that code snippet say to you "find the extension for this file name"? How about:
PHP Code:
$path = 'image.jpg';
$ext  = pathinfo($path, PATHINFO_EXTENSION);
// return ($ext === '') ? FALSE : $ext;

if ($ext === '')
{
    printf('Unable to find extension for %s', $path);
    return FALSE;
}

printf('Extension for %s is %s', $path, $ext);
return $ext;

Quote:
Originally Posted by Wildhoney
If it reads nicer, and also easier to debug, by utilising a couple more variables then I would say take that approach.
It's also often nicer to entirely rethink what you're writing and find a more suitable method of doing it. I could be wrong but is my snippet nicer to read, easier to debug and uses less variables (than your refactoring, the same as your original messy one)?
Salathe is offline  
Reply With Quote
Old 04-18-2008, 01:29 PM   #10 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

The whole reason I took the prolix approach was to make a point. There are a million and one ways to find the file's extension. You're right though, your code is simpler to read, and would have been the next step in refactoring if you were to continue to find better approaches.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 04-18-2008, 07:27 PM   #11 (permalink)
The Addict
 
Join Date: Nov 2007
Posts: 264
Thanks: 2
TlcAndres is on a distinguished road
Default

Pushing aside the issue of which return style to use - I always thought functions were to output nothing as in not display any information to the user.
__________________
"What everyone seems to forget is that while knowledge certainly is something - it's the implementation of knowledge that brings power" - Andres Galindo.
TlcAndres is offline  
Reply With Quote
Old 04-19-2008, 01:55 PM   #12 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Functions can be used pretty much anywhere where code would otherwise be repeated. In the case of this thread, such a function would be somewhat pointless, but for the extension code in my example, you would most certainly add that into a function. Whether it echoes its output or not is entirely up to you.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:25 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design