Quantcast
Channel: Magento – Ken Nguyen
Viewing all articles
Browse latest Browse all 14

10 worst Magento practices

$
0
0

Wrong Magento implementation can have critical consequences as dropping conversion rates. How to save your eCommerce before bad practices? It should be in our best interest to optimize code and therefore improve user experience. PHP code issues can be easier to find and fix than infrastructure ones.

I will introduce you the 10 worst Magento development practices followed by detailed examples. I picked the most common and the most important ones. I hope this article will help many to save their eCommerce before bad practices and to optimize their code.

1. Overriding core files

Magento uses nice event hooking system, following the Observer Pattern, allowing additional functionality to be plugged in and out without modifying the core code.

Very often developers rewrite a core file when there’s a possible way to accomplish the functionality with an event observer. Magento provides a lot of events that are dispatched at different times. All of these events can be observed, and the objects provided by the events can be modified.

There’s is no possible way to rewrite the same core file more than once without creating extends chain. By using event observers multiple modules can exist at the same time without conflicting — many modules can observe the same event.

2. Collections – query results limitation

In order to improve code performance and scalability remember to apply limitation on the collection’s query results — it’s much better to do $collection->getSize() instead of$collection->count() or count($collection) because that ways, all of the items will be loaded from the database and than iterated.

3. Memory – fetching large result set

Method fetchAll() used to fetch and iterate over larger result sets will lead to a very long execution time, moreover PHP will probably run out of memory. The better solution is to fetch row by row using the fetch() method. For example:

Wrong:

$rowSet = $this->_getReadAdapter()->fetchAll($select);
foreach ($rowSet as $row) {
    //process row
}

 Good

$query = $this->_getReadAdapter()->query($select);

while ($row = $query->fetch()) {
    //process row
}

4. Counting array

PHP Function count() is fast but when used in a loop this changes really severely. Calculating the size of an array on each iteration of a loop, if the array or collection contains a lot of items, will result in much longer execution time. Counting size should be done outside the loop not on each iteration.

Wrong:

for ($i = 0; $i < count($array); $i++){
    // do something
}

Good:

$size = count($array);
for ($i = 0; $i < $size; $i++){
    // do something
}

 

 


Viewing all articles
Browse latest Browse all 14

Latest Images

Trending Articles



Latest Images