SlideShare a Scribd company logo
1 of 16
Download to read offline
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 skypeMário Almeida
 
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
 
Real World AngularJS recipes: beyond TodoMVC
Real World AngularJS recipes: beyond TodoMVCReal World AngularJS recipes: beyond TodoMVC
Real World AngularJS recipes: beyond TodoMVCCarlo Bonamico
 
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 DevelopersMars Devs
 
Extension Submission to Marketplace
Extension Submission to MarketplaceExtension Submission to Marketplace
Extension Submission to MarketplaceWagento 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 2016Matt 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
 
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 2015Amy 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 KumarAmit 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 FranciscoChristian 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 2017Matt 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, Stoplightapidays
 
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 cucumberAlex Mikitenko
 
Mobile security recipes for xamarin
Mobile security recipes for xamarinMobile security recipes for xamarin
Mobile security recipes for xamarinNicolas 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.pdfMindfire 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 PetersonBrent 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 MiniailoMagecom 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 - 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...
 
Real World AngularJS recipes: beyond TodoMVC
Real World AngularJS recipes: beyond TodoMVCReal World AngularJS recipes: beyond TodoMVC
Real World AngularJS recipes: beyond TodoMVC
 
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.3Alessandro Ronchi
 
Some lessons learned contributing to #MagentoMSI
Some lessons learned contributing to #MagentoMSISome lessons learned contributing to #MagentoMSI
Some lessons learned contributing to #MagentoMSIAlessandro 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 #MagentoMSIAlessandro 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 coreAlessandro 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 coreAlessandro Ronchi
 
B2B & Magento: a long journey tale
B2B & Magento: a long journey taleB2B & Magento: a long journey tale
B2B & Magento: a long journey taleAlessandro 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 forwardAlessandro Ronchi
 
Mageploy presentato al Mage::day() 2013
Mageploy presentato al Mage::day() 2013Mageploy presentato al Mage::day() 2013
Mageploy presentato al Mage::day() 2013Alessandro 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

%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park masabamasaba
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile EnvironmentVictorSzoltysek
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfonteinmasabamasaba
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...masabamasaba
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2
 

Recently uploaded (20)

%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT  - Elevating Productivity in Today's Agile EnvironmentHarnessing ChatGPT  - Elevating Productivity in Today's Agile Environment
Harnessing ChatGPT - Elevating Productivity in Today's Agile Environment
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
%+27788225528 love spells in Huntington Beach Psychic Readings, Attraction sp...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 

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