Actions

Talk

How to use the filesystem package

From Joomla! Documentation

Revision as of 19:02, 1 September 2009 by Gilthans (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The example shows a terrible coding practice: nesting ifs


  if ( strtolower(JFile::getExt($filename) ) == 'jpg') {
     if ( JFile::upload($src, $dest) ) {
        //Redirect to a page of your choice
     } else {
        //Redirect and throw an error message
     }
  } else {
     //Redirect and notify user file is not right extension
  }


This sort of thing creates huge chains of if's that no knows how to look at. I think a better sample code would be:

  if ( strtolower(JFile::getExt($filename) ) != 'jpg')
     //Redirect and notify user file is not right extension
  else if ( !JFile::upload($src, $dest) )
     //Redirect and throw an error message
  else
     //Redirect to a page of your choice

(Ideally we'd put this in a function and return after each if, but KISS :) )