php - Is this too many lines and too many nested blocks? -


i have function loads list of things database , put them select list. function follows : (pseudocode)

 protected function foo()   {     try {          pdo instance         prepare statement          if (pdo query executes)          {              while (row = fetched rows)              {                 stuff row             }         }       }       catch (pdoexception $ex)       {          error stuff here      }          } 

netbeans gives code hint many lines , many nested blocks. feel function should acceptable. feel break logic smaller functions bit looney, why netbeans lie me:) ?

so questions follows:

is bad logic or go ahead? welcome suggestions on how might redesign function fit within netbean constraints.

edit:

i won't answer own question in case there 1 nested block not needed. pdo retrieved singleton class has try/catch block. dont need repeat again in function because exception of been caught.

edit 2:

removing try catch block robbing peter pay paul. if exception thrown in creation of pdo instance not stop execution. thus, try call prepare statement on pdo object not initialized correctly. forces put in test before prepare call, returning rework of original function.

in experience, means somewhere along line logic floored. going go on design , holla if have worthy say.

thanks again all

your code good. netbeans suggests not rule should follow , wouldn't have worried if used other editor phpstorm (in phpstorm, can set coding style follow psr 1/2).

you @ least away 1 nesting using called guard clause:

protected function foo()  {     try {         pdo instance         prepare statement          if (! pdo query executes) return;           while (row = fetched rows)          {             stuff row         }       }       catch (pdoexception $ex)       {          error stuff here      }         } 

i have seen people having different preferences since there no hard , fast rule. example anthony ferrara personally thinks should not go beyond 4 nested levels think 4 levels much.

point should minimize nesting , number of lines as can. when method big (sometimes called god method), means doing wrong.

you may want @ nice article william durand on subject discuss few (actually 9) proposals suggested jeff bay in book thoughtworks anthology

object calisthenics

also php coding standards fixer friend.

so:

  • your current code fine
  • create personal preference , minimize nesting , number of lines possible.

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -