Sign in
Log inSign up

Code optimization vs readable code

Ovidiu Bădiță's photo
Ovidiu Bădiță
·May 10, 2017

Assuming you have a code such as this:

foreach ($products as $product) {    
 process(product);     
 if(somecondition){     
   doSomething(product);     
 } 
}

How bad would you consider this form of refactoring?

foreach ($products as $product) {     
  process(product);
}

foreach ($products as $product) {     
  if(somecondition){     
    doSomething(product); 
  }    
}

The reason I like it is because you can extract both things in separate methods, and I do not feel that the performance is hurt that bad...