TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Question about 'return' statement in UDF... (http://www.talkphp.com/absolute-beginners/2655-question-about-return-statement-udf.html)

Dave 04-18-2008 01:45 AM

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

TlcAndres 04-18-2008 01:57 AM

When your using longer functions with longer mathematical equations or longer bits of code then it becomes easier to read.

zxt3st 04-18-2008 02:24 AM

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

Wildhoney 04-18-2008 02:42 AM

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.

zxt3st 04-18-2008 02:53 AM

best agree with that example..:D

Wildhoney 04-18-2008 03:02 AM

Ach! Not necessarily. Challenge me :-( !

zxt3st 04-18-2008 03:10 AM

Quote:

Originally Posted by Wildhoney (Post 13591)
Ach! Not necessarily. Challenge me :-( !

nah..no i wont...:-/

Kalle 04-18-2008 09:28 AM

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 =)

Salathe 04-18-2008 11:42 AM

Quote:

Originally Posted by Wildhoney (Post 13585)
...
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 (Post 13591)
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)?

Wildhoney 04-18-2008 01:29 PM

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.

TlcAndres 04-18-2008 07:27 PM

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.

Wildhoney 04-19-2008 01:55 PM

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.


All times are GMT. The time now is 09:26 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0