04-18-2008, 11:42 AM
|
#9 (permalink)
|
|
Moderateur
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
|
Quote:
Originally Posted by Wildhoney
...
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
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)?
|
|
|
|