🟢 Good code
/**
* This function only does one thing; it returns a value.
*/
/**
* Retrieve the title.
*
* @return string
*/
public function getTitle()
{
// Just get the value of the title, don't do anything else
return $this->title;
}
🔴 Bad code
/**
* This function not only returns the desired value, but carries out other
* actions. An external call is made which modifies other variables or the
* value being returned, or does "other stuff" (side effects) which cause
* unexpected results to occur. These side effects make code hard to test
* and are unknown to the code that is calling it.
*/
/**
* Retrieve the title, and do other stuff.
*
* @return string
*/
public function getTitle()
{
// This is where "other stuff" happens -- a side effect
$this->build();
return $this->title;
}
Visit M.academy to learn much more about Magento, Laravel, PHP, Javascript, & Docker.