SlideShare a Scribd company logo
MAGENTO BEST PRACTICES
ALESSANDRO RONCHI
MANCHESTER, NOV. 7th
2015
ABOUT ME: @aleron75
aleron75/mageres
#m
agebp
3/16#magebp rules
● Use the best available tools
● Use Magento runtime
● Use alternatives to Mage::log()
● Extend native autoloading
● Write effective and solid MVC components
● Write scalable code
● Never touch the core
#m
agebp
4/16#magebp example
● Use the best available tools
● Use Magento runtime
● Use alternatives to Mage::log()
● Extend native autoloading
● Write effective and solid MVC components
● Write scalable code
● Never touch the core
#m
agebp
5/16#magebp example
● Use the best tools & habits
– to test code
– to profile code
● Use Magento runtime
– to validate code easily and rapidly
● Write scalable code
– to avoid performance traps
#m
agebp
6/16Based on a real story...
What you will be shown is real code
that run on production.
This code was written to display
a category navigation menu.
No animals were harmed in the making of
this code; a website was.
#m
agebp
7/16
First chunk
seek for category subcategories
$rootCatID = Mage::app()­>getStore()­>getRootCategoryId();
$cat = Mage::getModel('catalog/category')­>load($rootCatID);
$subCats = $cat­>getChildren();
foreach (explode(',', $subCats) as $subCatid) {
  $category = Mage::getModel('catalog/category')
    ­>load($subCatid);
  $subcategories = $category­>getChildren();
}
foreach (explode(',', $subcategories) as $subCategoryid) {
  $subcategory = Mage::getModel('catalog/category')
    ­>load($subCategoryid);
  // Second chunk...
}
#m
agebp
8/16
First chunk
seek for category subcategories
$rootCatID = Mage::app()­>getStore()­>getRootCategoryId();
$cat = Mage::getModel('catalog/category')­>load($rootCatID);
$subCats = $cat­>getChildren();
foreach (explode(',', $subCats) as $subCatid) {
  $category = Mage::getModel('catalog/category')
    ­>load($subCatid);
  $subcategories = $category­>getChildren();
}
foreach (explode(',', $subcategories) as $subCategoryid) {
  $subcategory = Mage::getModel('catalog/category')
    ­>load($subCategoryid);
  // Second chunk...
}
Assuming there is only one sub
category will break menu as soon as
a new one is added: and it happened!
Loading in loops
doesn't scale
#m
agebp
9/16
Second chunk
seek for product categories
foreach (explode(',', $subcategories) as $subCategoryid) {
  $subcategory = Mage::getModel('catalog/category')­>load($subCategoryid);
  $category = Mage::getModel('catalog/category')
    ­>load($subcategory­>getId());
  $subcatProducts = Mage::getModel('catalog/product')
    ­>getCollection()
    ­>addCategoryFilter($subcategory)­>load();
  foreach($subcatProducts as $product) {
    foreach($product­>getCategoryIds() as $categoryId) {
      $myCategory = Mage::getModel('catalog/category')­>load($categoryId);
      $catUrl = $myCategory­>getUrl();
      $prodUrlKey = end($explode("/", $product­>getProductUrl()));
    }
  }
}
#m
agebp
10/16
Second chunk
seek for product categories
foreach (explode(',', $subcategories) as $subCategoryid) {
  $subcategory = Mage::getModel('catalog/category')­>load($subCategoryid);
  $category = Mage::getModel('catalog/category')
    ­>load($subcategory­>getId());
  $subcatProducts = Mage::getModel('catalog/product')
    ­>getCollection()
    ­>addCategoryFilter($subcategory)­>load();
  foreach($subcatProducts as $product) {
    foreach($product­>getCategoryIds() as $categoryId) {
      $myCategory = Mage::getModel('catalog/category')­>load($categoryId);
      $catUrl = $myCategory­>getUrl();
      $prodUrlKey = end($explode("/", $product­>getProductUrl()));
    }
  }
}
$category is not used and
adds another load in a loop!
WHY?
#m
agebp
11/16
There is a name for this...
Code
Running
Ashamedly on
Production
Sadly enough a customer paid for that:
it's unethical other than unprofessional.
This is not technical debt.
This is building with sand.
#m
agebp
12/16
Refactoring first chunk
seek for category subcategories
$configuredCatId = 
Mage::getStoreConfig('my/own/path');
$category = Mage::getModel('catalog/category')
  ­>getCollection()
  ­>addIdFilter($configuredCatId)
  ­>setPageSize(1)
  ­>getFirstItem();
$subcategories = $category­>getChildrenCategories();
foreach ($subcategories as $subcategory) {
  // Second chunk...
}
#m
agebp
13/16
Refactoring second chunk
seek for product categories
foreach ($subcategories as $subcategory) {
  $subcatProducts = $subcategory­>getProductCollection();
  foreach($subcatProducts as $product) {
    $productCategories = $product­>getCategoryCollection();
    foreach($productCategories as $myCategory) {
      $catUrl = $myCategory­>getUrl();
      $prodUrl = $product­>getProductUrl();
      $prodUrlKey = substr(strrchr($prodUrl, '/'), 1);
    }
  }
}
#m
agebp
14/16
Comparison
(using XHProf)
Before refactoring
After refactoring
10x faster!
#m
agebp
15/16
Refactoring
(using tests)
● Testing gives us the confidence to change
code without breaking functionality
● Testing doesn't require complex tools
– start from manual tests
– automate to scale
– start from common tools: diff
– use more sophisticated tools when needed
THANK YOU!
https://github.com/aleron75https://github.com/aleron75
https://twitter.com/aleron75https://twitter.com/aleron75
http://leanpub.com/magebp/c/mt15ukhttp://leanpub.com/magebp/c/mt15uk

More Related Content

Similar to A true story about Magento best practices

Android reverse engineering - Analyzing skype
Android reverse engineering - Analyzing skypeAndroid reverse engineering - Analyzing skype
Android reverse engineering - Analyzing skype
Mário Almeida
 
Real World AngularJS recipes: beyond TodoMVC
Real World AngularJS recipes: beyond TodoMVCReal World AngularJS recipes: beyond TodoMVC
Real World AngularJS recipes: beyond TodoMVC
Carlo Bonamico
 
Real World AngularJS recipes: beyond TodoMVC - Carlo Bonamico, Sonia Pini - C...
Real World AngularJS recipes: beyond TodoMVC - Carlo Bonamico, Sonia Pini - C...Real World AngularJS recipes: beyond TodoMVC - Carlo Bonamico, Sonia Pini - C...
Real World AngularJS recipes: beyond TodoMVC - Carlo Bonamico, Sonia Pini - C...
Codemotion
 
Learn Django Tips, Tricks & Techniques for Developers
Learn Django Tips, Tricks & Techniques for DevelopersLearn Django Tips, Tricks & Techniques for Developers
Learn Django Tips, Tricks & Techniques for Developers
Mars Devs
 
Extension Submission to Marketplace
Extension Submission to MarketplaceExtension Submission to Marketplace
Extension Submission to Marketplace
Wagento Kangiya
 
Testing Angular 2 Applications - Rich Web 2016
Testing Angular 2 Applications - Rich Web 2016Testing Angular 2 Applications - Rich Web 2016
Testing Angular 2 Applications - Rich Web 2016
Matt Raible
 
Writing testable Code (MageTitans Mini 2016)
Writing testable Code (MageTitans Mini 2016)Writing testable Code (MageTitans Mini 2016)
Writing testable Code (MageTitans Mini 2016)
vinaikopp
 
Final (3).pptx
Final (3).pptxFinal (3).pptx
Final (3).pptx
RohitYadav429441
 
Getting Started with Scripts #HeroConf London 2015
Getting Started with Scripts #HeroConf London 2015Getting Started with Scripts #HeroConf London 2015
Getting Started with Scripts #HeroConf London 2015
Amy Bishop
 
Complete Website Development Guide by AMit P Kumar
Complete Website Development Guide by AMit P KumarComplete Website Development Guide by AMit P Kumar
Complete Website Development Guide by AMit P Kumar
Amit P Kumar
 
Stapling and patching the web of now - ForwardJS3, San Francisco
Stapling and patching the web of now - ForwardJS3, San FranciscoStapling and patching the web of now - ForwardJS3, San Francisco
Stapling and patching the web of now - ForwardJS3, San Francisco
Christian Heilmann
 
Angular.js for beginners
Angular.js for beginners Angular.js for beginners
Angular.js for beginners
Basia Madej
 
Overview of the AngularJS framework
Overview of the AngularJS framework Overview of the AngularJS framework
Overview of the AngularJS framework
Yakov Fain
 
Testing Angular Applications - Jfokus 2017
Testing Angular Applications - Jfokus 2017Testing Angular Applications - Jfokus 2017
Testing Angular Applications - Jfokus 2017
Matt Raible
 
INTERFACE by apidays 2023 - API Design Governance, Nauman Ali, Stoplight
INTERFACE by apidays 2023 - API Design Governance, Nauman Ali, StoplightINTERFACE by apidays 2023 - API Design Governance, Nauman Ali, Stoplight
INTERFACE by apidays 2023 - API Design Governance, Nauman Ali, Stoplight
apidays
 
Testing stage. being ahead business with cucumber
Testing stage. being ahead business with cucumberTesting stage. being ahead business with cucumber
Testing stage. being ahead business with cucumber
Alex Mikitenko
 
Mobile security recipes for xamarin
Mobile security recipes for xamarinMobile security recipes for xamarin
Mobile security recipes for xamarin
Nicolas Milcoff
 
How can JAVA Performance tuning speed up applications.pdf
How can JAVA Performance tuning speed up applications.pdfHow can JAVA Performance tuning speed up applications.pdf
How can JAVA Performance tuning speed up applications.pdf
Mindfire LLC
 
Wagento Magento 2 developer - Brent W Peterson
Wagento Magento 2 developer - Brent W PetersonWagento Magento 2 developer - Brent W Peterson
Wagento Magento 2 developer - Brent W Peterson
Brent W Peterson
 
API Design Best Practices by Igor Miniailo
API Design Best Practices by Igor MiniailoAPI Design Best Practices by Igor Miniailo
API Design Best Practices by Igor Miniailo
Magecom UK Limited
 

Similar to A true story about Magento best practices (20)

Android reverse engineering - Analyzing skype
Android reverse engineering - Analyzing skypeAndroid reverse engineering - Analyzing skype
Android reverse engineering - Analyzing skype
 
Real World AngularJS recipes: beyond TodoMVC
Real World AngularJS recipes: beyond TodoMVCReal World AngularJS recipes: beyond TodoMVC
Real World AngularJS recipes: beyond TodoMVC
 
Real World AngularJS recipes: beyond TodoMVC - Carlo Bonamico, Sonia Pini - C...
Real World AngularJS recipes: beyond TodoMVC - Carlo Bonamico, Sonia Pini - C...Real World AngularJS recipes: beyond TodoMVC - Carlo Bonamico, Sonia Pini - C...
Real World AngularJS recipes: beyond TodoMVC - Carlo Bonamico, Sonia Pini - C...
 
Learn Django Tips, Tricks & Techniques for Developers
Learn Django Tips, Tricks & Techniques for DevelopersLearn Django Tips, Tricks & Techniques for Developers
Learn Django Tips, Tricks & Techniques for Developers
 
Extension Submission to Marketplace
Extension Submission to MarketplaceExtension Submission to Marketplace
Extension Submission to Marketplace
 
Testing Angular 2 Applications - Rich Web 2016
Testing Angular 2 Applications - Rich Web 2016Testing Angular 2 Applications - Rich Web 2016
Testing Angular 2 Applications - Rich Web 2016
 
Writing testable Code (MageTitans Mini 2016)
Writing testable Code (MageTitans Mini 2016)Writing testable Code (MageTitans Mini 2016)
Writing testable Code (MageTitans Mini 2016)
 
Final (3).pptx
Final (3).pptxFinal (3).pptx
Final (3).pptx
 
Getting Started with Scripts #HeroConf London 2015
Getting Started with Scripts #HeroConf London 2015Getting Started with Scripts #HeroConf London 2015
Getting Started with Scripts #HeroConf London 2015
 
Complete Website Development Guide by AMit P Kumar
Complete Website Development Guide by AMit P KumarComplete Website Development Guide by AMit P Kumar
Complete Website Development Guide by AMit P Kumar
 
Stapling and patching the web of now - ForwardJS3, San Francisco
Stapling and patching the web of now - ForwardJS3, San FranciscoStapling and patching the web of now - ForwardJS3, San Francisco
Stapling and patching the web of now - ForwardJS3, San Francisco
 
Angular.js for beginners
Angular.js for beginners Angular.js for beginners
Angular.js for beginners
 
Overview of the AngularJS framework
Overview of the AngularJS framework Overview of the AngularJS framework
Overview of the AngularJS framework
 
Testing Angular Applications - Jfokus 2017
Testing Angular Applications - Jfokus 2017Testing Angular Applications - Jfokus 2017
Testing Angular Applications - Jfokus 2017
 
INTERFACE by apidays 2023 - API Design Governance, Nauman Ali, Stoplight
INTERFACE by apidays 2023 - API Design Governance, Nauman Ali, StoplightINTERFACE by apidays 2023 - API Design Governance, Nauman Ali, Stoplight
INTERFACE by apidays 2023 - API Design Governance, Nauman Ali, Stoplight
 
Testing stage. being ahead business with cucumber
Testing stage. being ahead business with cucumberTesting stage. being ahead business with cucumber
Testing stage. being ahead business with cucumber
 
Mobile security recipes for xamarin
Mobile security recipes for xamarinMobile security recipes for xamarin
Mobile security recipes for xamarin
 
How can JAVA Performance tuning speed up applications.pdf
How can JAVA Performance tuning speed up applications.pdfHow can JAVA Performance tuning speed up applications.pdf
How can JAVA Performance tuning speed up applications.pdf
 
Wagento Magento 2 developer - Brent W Peterson
Wagento Magento 2 developer - Brent W PetersonWagento Magento 2 developer - Brent W Peterson
Wagento Magento 2 developer - Brent W Peterson
 
API Design Best Practices by Igor Miniailo
API Design Best Practices by Igor MiniailoAPI Design Best Practices by Igor Miniailo
API Design Best Practices by Igor Miniailo
 

More from Alessandro Ronchi

Meet Magento IT 2021 - Principles & Advantages of Hexagonal Architecture on M...
Meet Magento IT 2021 - Principles & Advantages of Hexagonal Architecture on M...Meet Magento IT 2021 - Principles & Advantages of Hexagonal Architecture on M...
Meet Magento IT 2021 - Principles & Advantages of Hexagonal Architecture on M...
Alessandro Ronchi
 
Awesome architectures in Magento 2.3
Awesome architectures in Magento 2.3Awesome architectures in Magento 2.3
Awesome architectures in Magento 2.3
Alessandro Ronchi
 
Some lessons learned contributing to #MagentoMSI
Some lessons learned contributing to #MagentoMSISome lessons learned contributing to #MagentoMSI
Some lessons learned contributing to #MagentoMSI
Alessandro Ronchi
 
The lessons I learned contributing to #MagentoMSI
The lessons I learned contributing to #MagentoMSIThe lessons I learned contributing to #MagentoMSI
The lessons I learned contributing to #MagentoMSI
Alessandro Ronchi
 
How I ended up contributing to Magento core
How I ended up contributing to Magento coreHow I ended up contributing to Magento core
How I ended up contributing to Magento core
Alessandro Ronchi
 
How I ended up touching Magento core
How I ended up touching Magento coreHow I ended up touching Magento core
How I ended up touching Magento core
Alessandro Ronchi
 
B2B & Magento: a long journey tale
B2B & Magento: a long journey taleB2B & Magento: a long journey tale
B2B & Magento: a long journey tale
Alessandro Ronchi
 
Need to estimate? Let's play poker!
Need to estimate? Let's play poker!Need to estimate? Let's play poker!
Need to estimate? Let's play poker!
Alessandro Ronchi
 
Why I did one step backward to go forward
Why I did one step backward to go forwardWhy I did one step backward to go forward
Why I did one step backward to go forward
Alessandro Ronchi
 
Mageday::2014 - Workshop
Mageday::2014 - WorkshopMageday::2014 - Workshop
Mageday::2014 - Workshop
Alessandro Ronchi
 
Mageday::2014
Mageday::2014Mageday::2014
Mageday::2014
Alessandro Ronchi
 
Mageploy presentato al Mage::day() 2013
Mageploy presentato al Mage::day() 2013Mageploy presentato al Mage::day() 2013
Mageploy presentato al Mage::day() 2013
Alessandro Ronchi
 

More from Alessandro Ronchi (12)

Meet Magento IT 2021 - Principles & Advantages of Hexagonal Architecture on M...
Meet Magento IT 2021 - Principles & Advantages of Hexagonal Architecture on M...Meet Magento IT 2021 - Principles & Advantages of Hexagonal Architecture on M...
Meet Magento IT 2021 - Principles & Advantages of Hexagonal Architecture on M...
 
Awesome architectures in Magento 2.3
Awesome architectures in Magento 2.3Awesome architectures in Magento 2.3
Awesome architectures in Magento 2.3
 
Some lessons learned contributing to #MagentoMSI
Some lessons learned contributing to #MagentoMSISome lessons learned contributing to #MagentoMSI
Some lessons learned contributing to #MagentoMSI
 
The lessons I learned contributing to #MagentoMSI
The lessons I learned contributing to #MagentoMSIThe lessons I learned contributing to #MagentoMSI
The lessons I learned contributing to #MagentoMSI
 
How I ended up contributing to Magento core
How I ended up contributing to Magento coreHow I ended up contributing to Magento core
How I ended up contributing to Magento core
 
How I ended up touching Magento core
How I ended up touching Magento coreHow I ended up touching Magento core
How I ended up touching Magento core
 
B2B & Magento: a long journey tale
B2B & Magento: a long journey taleB2B & Magento: a long journey tale
B2B & Magento: a long journey tale
 
Need to estimate? Let's play poker!
Need to estimate? Let's play poker!Need to estimate? Let's play poker!
Need to estimate? Let's play poker!
 
Why I did one step backward to go forward
Why I did one step backward to go forwardWhy I did one step backward to go forward
Why I did one step backward to go forward
 
Mageday::2014 - Workshop
Mageday::2014 - WorkshopMageday::2014 - Workshop
Mageday::2014 - Workshop
 
Mageday::2014
Mageday::2014Mageday::2014
Mageday::2014
 
Mageploy presentato al Mage::day() 2013
Mageploy presentato al Mage::day() 2013Mageploy presentato al Mage::day() 2013
Mageploy presentato al Mage::day() 2013
 

Recently uploaded

Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
timtebeek1
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
Aftab Hussain
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
Boni García
 
Launch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in MinutesLaunch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in Minutes
Roshan Dwivedi
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
Google
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
Shane Coughlan
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
TheSMSPoint
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
Ayan Halder
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
lorraineandreiamcidl
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
Neo4j
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
Alina Yurenko
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
Octavian Nadolu
 

Recently uploaded (20)

Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
 
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of CodeA Study of Variable-Role-based Feature Enrichment in Neural Models of Code
A Study of Variable-Role-based Feature Enrichment in Neural Models of Code
 
APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)APIs for Browser Automation (MoT Meetup 2024)
APIs for Browser Automation (MoT Meetup 2024)
 
Launch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in MinutesLaunch Your Streaming Platforms in Minutes
Launch Your Streaming Platforms in Minutes
 
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI AppAI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
AI Fusion Buddy Review: Brand New, Groundbreaking Gemini-Powered AI App
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024OpenMetadata Community Meeting - 5th June 2024
OpenMetadata Community Meeting - 5th June 2024
 
openEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain SecurityopenEuler Case Study - The Journey to Supply Chain Security
openEuler Case Study - The Journey to Supply Chain Security
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Transform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR SolutionsTransform Your Communication with Cloud-Based IVR Solutions
Transform Your Communication with Cloud-Based IVR Solutions
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Using Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional SafetyUsing Xen Hypervisor for Functional Safety
Using Xen Hypervisor for Functional Safety
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
 
GraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph TechnologyGraphSummit Paris - The art of the possible with Graph Technology
GraphSummit Paris - The art of the possible with Graph Technology
 
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)GOING AOT WITH GRAALVM FOR  SPRING BOOT (SPRING IO)
GOING AOT WITH GRAALVM FOR SPRING BOOT (SPRING IO)
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
Artificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension FunctionsArtificia Intellicence and XPath Extension Functions
Artificia Intellicence and XPath Extension Functions
 

A true story about Magento best practices

  • 1. MAGENTO BEST PRACTICES ALESSANDRO RONCHI MANCHESTER, NOV. 7th 2015
  • 3. #m agebp 3/16#magebp rules ● Use the best available tools ● Use Magento runtime ● Use alternatives to Mage::log() ● Extend native autoloading ● Write effective and solid MVC components ● Write scalable code ● Never touch the core
  • 4. #m agebp 4/16#magebp example ● Use the best available tools ● Use Magento runtime ● Use alternatives to Mage::log() ● Extend native autoloading ● Write effective and solid MVC components ● Write scalable code ● Never touch the core
  • 5. #m agebp 5/16#magebp example ● Use the best tools & habits – to test code – to profile code ● Use Magento runtime – to validate code easily and rapidly ● Write scalable code – to avoid performance traps
  • 6. #m agebp 6/16Based on a real story... What you will be shown is real code that run on production. This code was written to display a category navigation menu. No animals were harmed in the making of this code; a website was.
  • 7. #m agebp 7/16 First chunk seek for category subcategories $rootCatID = Mage::app()­>getStore()­>getRootCategoryId(); $cat = Mage::getModel('catalog/category')­>load($rootCatID); $subCats = $cat­>getChildren(); foreach (explode(',', $subCats) as $subCatid) {   $category = Mage::getModel('catalog/category')     ­>load($subCatid);   $subcategories = $category­>getChildren(); } foreach (explode(',', $subcategories) as $subCategoryid) {   $subcategory = Mage::getModel('catalog/category')     ­>load($subCategoryid);   // Second chunk... }
  • 8. #m agebp 8/16 First chunk seek for category subcategories $rootCatID = Mage::app()­>getStore()­>getRootCategoryId(); $cat = Mage::getModel('catalog/category')­>load($rootCatID); $subCats = $cat­>getChildren(); foreach (explode(',', $subCats) as $subCatid) {   $category = Mage::getModel('catalog/category')     ­>load($subCatid);   $subcategories = $category­>getChildren(); } foreach (explode(',', $subcategories) as $subCategoryid) {   $subcategory = Mage::getModel('catalog/category')     ­>load($subCategoryid);   // Second chunk... } Assuming there is only one sub category will break menu as soon as a new one is added: and it happened! Loading in loops doesn't scale
  • 9. #m agebp 9/16 Second chunk seek for product categories foreach (explode(',', $subcategories) as $subCategoryid) {   $subcategory = Mage::getModel('catalog/category')­>load($subCategoryid);   $category = Mage::getModel('catalog/category')     ­>load($subcategory­>getId());   $subcatProducts = Mage::getModel('catalog/product')     ­>getCollection()     ­>addCategoryFilter($subcategory)­>load();   foreach($subcatProducts as $product) {     foreach($product­>getCategoryIds() as $categoryId) {       $myCategory = Mage::getModel('catalog/category')­>load($categoryId);       $catUrl = $myCategory­>getUrl();       $prodUrlKey = end($explode("/", $product­>getProductUrl()));     }   } }
  • 10. #m agebp 10/16 Second chunk seek for product categories foreach (explode(',', $subcategories) as $subCategoryid) {   $subcategory = Mage::getModel('catalog/category')­>load($subCategoryid);   $category = Mage::getModel('catalog/category')     ­>load($subcategory­>getId());   $subcatProducts = Mage::getModel('catalog/product')     ­>getCollection()     ­>addCategoryFilter($subcategory)­>load();   foreach($subcatProducts as $product) {     foreach($product­>getCategoryIds() as $categoryId) {       $myCategory = Mage::getModel('catalog/category')­>load($categoryId);       $catUrl = $myCategory­>getUrl();       $prodUrlKey = end($explode("/", $product­>getProductUrl()));     }   } } $category is not used and adds another load in a loop! WHY?
  • 11. #m agebp 11/16 There is a name for this... Code Running Ashamedly on Production Sadly enough a customer paid for that: it's unethical other than unprofessional. This is not technical debt. This is building with sand.
  • 12. #m agebp 12/16 Refactoring first chunk seek for category subcategories $configuredCatId =  Mage::getStoreConfig('my/own/path'); $category = Mage::getModel('catalog/category')   ­>getCollection()   ­>addIdFilter($configuredCatId)   ­>setPageSize(1)   ­>getFirstItem(); $subcategories = $category­>getChildrenCategories(); foreach ($subcategories as $subcategory) {   // Second chunk... }
  • 13. #m agebp 13/16 Refactoring second chunk seek for product categories foreach ($subcategories as $subcategory) {   $subcatProducts = $subcategory­>getProductCollection();   foreach($subcatProducts as $product) {     $productCategories = $product­>getCategoryCollection();     foreach($productCategories as $myCategory) {       $catUrl = $myCategory­>getUrl();       $prodUrl = $product­>getProductUrl();       $prodUrlKey = substr(strrchr($prodUrl, '/'), 1);     }   } }
  • 15. #m agebp 15/16 Refactoring (using tests) ● Testing gives us the confidence to change code without breaking functionality ● Testing doesn't require complex tools – start from manual tests – automate to scale – start from common tools: diff – use more sophisticated tools when needed