SlideShare a Scribd company logo
1 of 32
© 2018 Magento, Inc. Page | 1
Magento Technical Guidelines
Eugene Shakhsuvarov, Software Engineer @ Magento
© 2018 Magento, Inc. Page | 2
© 2018 Magento, Inc. Page | 3
Magento 2 Technical Guidelines
• Document which describes the desired technical state of Magento 2
• Hundreds of architect-hours invested into development of guidelines
• May seem too restrictive and sometimes unobvious in favor of code
readability and extensibility
• All of the new core code must follow the rules
http://devdocs.magento.com/guides/v2.2/coding-standards/technical-
guidelines/technical-guidelines.html
© 2018 Magento, Inc. Page | 4
Basic Principles
© 2018 Magento, Inc. Page | 5
Strict types
• Starting with Magento 2.3 only PHP 7.1+ is supported
• Explicit return types must be declared on functions
• Type hints for scalar arguments should be used
• Declaration of strict_types is encouraged where possible
© 2018 Magento, Inc. Page | 6
Class Design
© 2018 Magento, Inc. Page | 7
Object Manager
• Generally Object Manager should not be used as a class
dependency
• Doing so decreases ability for third parties to customize your code
© 2018 Magento, Inc. Page | 8
Don’t do this
• In this case dependency is
hard coded and can not be
replaced with a different one
• Would require more work to
modify the behavior by third
parties
© 2018 Magento, Inc. Page | 9
Do this instead
• Now logger may be changed
with a simple configuration in
di.xml
• Behavior is changed without
any additional coding
© 2018 Magento, Inc. Page | 10
There are exceptions to this rule
• Object Manager may still be used in classes which create objects
(Factories, Builders)
• May also be used in the core code to maintain backwards
compatibility
© 2018 Magento, Inc. Page | 11
Inheritance
• Inheritance should not be used. Composition should be used
instead
• Magento is moving away from the inheritance as a main code reuse
mechanism
• Inheritance enforces dependency on a specific parent class which
can not be replaced in runtime
• Overwriting logic based on inheritance requires a lot of boilerplate
code
© 2018 Magento, Inc. Page | 12
Inheritance Example
• Simple task of formatting
product price
• May be perfectly fine in
conditions, where third parties
do not need to modify the
behavior
• Requires complete
replacement of the default
renderer, instead of
customizing it
© 2018 Magento, Inc. Page | 13
Composition Example
• Price Formatter may be easily
replaced by injecting another
dependency in the constructor
• Allows precise modification of
behavior
© 2018 Magento, Inc. Page | 14
Inheritance Pros
• Inheritance may still be applicable in specific cases, for example
when class is a subtype of another class
© 2018 Magento, Inc. Page | 15
Single Responsibility
• All Classes should have only Single
Responsibility which is entirely
incapsulated
• Mixing different behaviors in one
class (e. g. classic Helper) greatly
decreases extensibility and
increases coupling in most of the
cases
• Allows for easy replacement of any
specific behavior by providing a
single point for change
© 2018 Magento, Inc. Page | 16
Constructing a Class
• Object must be ready for use after instantiation
• No additional public initialization methods are allowed
• Constructor should throw an exception when validation of an
argument has failed
© 2018 Magento, Inc. Page | 17
Constructor Dependency Injection
• All dependencies must be requested by the most generic type that is
required by the client object
• Class constructor can have only dependency assignment operations
and/or argument validation operations
• Proxies and interceptors must never be explicitly requested in
constructors
© 2018 Magento, Inc. Page | 18
Class members visibility
• All non-public properties and methods should be private
• Discourages use of inheritance
• Protected properties are much harder to remove from the class, as
some client code could be already extending it
© 2018 Magento, Inc. Page | 19
Temporal Coupling
• Temporal coupling must be avoided
• Semantic dependencies between methods is prone to errors as
client code never knows the current state of the system
• Method chaining in class design must be avoided
https://ocramius.github.io/blog/fluent-interfaces-are-evil/
© 2018 Magento, Inc. Page | 20
Object State
• Service classes, ones that provide behavior but not data, should not
have a state
• Only data objects or entities may have an observable state
• Getters should not change the state of an object
© 2018 Magento, Inc. Page | 21
Principle of least knowledge
• Class should have limited knowledge about other classes
• Object should only call methods only on its “friends”
• Do not “talk” to strangers
© 2018 Magento, Inc. Page | 22
Interception
© 2018 Magento, Inc. Page | 23
Interception best practices
• Avoid implementing a plugin when different kind of extension point is
available
• Plugins should not be used within own module
• Plugins should not be added to data objects
• Plugins must be stateless
© 2018 Magento, Inc. Page | 24
“Around” Plugins
• Should only be used when behavior of an original method is
supposed to be substituted in certain scenarios
• Performance penalty is increased compared to other types of
plugins
• Generate a lot of stack frames making it harder to debug the code
© 2018 Magento, Inc. Page | 25
Exception Handling
© 2018 Magento, Inc. Page | 26
Exception handling
• Exceptions must not be handled in the same function where they are
thrown
• Business logic (both application and domain) must not be managed
with exceptions. Conditional statements should be used instead
• All direct communications with third-party libraries must be wrapped
with a try/catch statement
© 2018 Magento, Inc. Page | 27
Exceptions Logging
• It is not allowed to absorb exceptions with no logging or/and any
workaround operation executed
• Any exception should be logged only in the catch block where it is
processed, and should not be re-thrown
© 2018 Magento, Inc. Page | 28
Application Layers
© 2018 Magento, Inc. Page | 29
Presentation Layer
• Request, Response, Session, Store Manager and Cookie objects
must be used only in the Presentation layer
• Controllers should only call appropriate services and return
ResultInterface implementation
• Controllers must be as lightweight as possible
• LocalizedException should only be thrown in the Presentation layer
(Controllers, Blocks)
© 2018 Magento, Inc. Page | 30
Service Layer
• Service layer is a contract for every specific module
• Service layer Interfaces may be used both as API and SPI
• Service Contracts should follow CQRS principle
© 2018 Magento, Inc. Page | 31
Persistence Layer
• Always separate business logic and persistence logic
• Entities must not contain persistence-related logic
• Entities may be persisted in different scopes
• Every persistence operation must be performed with one scope set
Page | 32© 2018 Magento, Inc.
Thank you!
Q & A

More Related Content

Similar to Magento Technical guidelines

Magento 2 Automated Testing via examples of Multi-Source Inventory (MSI)
Magento 2 Automated Testing via examples of Multi-Source Inventory (MSI)Magento 2 Automated Testing via examples of Multi-Source Inventory (MSI)
Magento 2 Automated Testing via examples of Multi-Source Inventory (MSI)Igor Miniailo
 
Automated Testing in Magento 2
Automated Testing in Magento 2Automated Testing in Magento 2
Automated Testing in Magento 2Magecom UK Limited
 
Magento 2.3 Schema and Data Patches
Magento 2.3 Schema and Data PatchesMagento 2.3 Schema and Data Patches
Magento 2.3 Schema and Data Patchesatishgoswami
 
Backward Compatibility Developer's Guide Webinar
Backward Compatibility Developer's Guide WebinarBackward Compatibility Developer's Guide Webinar
Backward Compatibility Developer's Guide WebinarIgor Miniailo
 
API design best practices
API design best practicesAPI design best practices
API design best practicesIgor Miniailo
 
Eugene Shakhsuvarov - Improving enterprise store scalability using AMQP and A...
Eugene Shakhsuvarov - Improving enterprise store scalability using AMQP and A...Eugene Shakhsuvarov - Improving enterprise store scalability using AMQP and A...
Eugene Shakhsuvarov - Improving enterprise store scalability using AMQP and A...Meet Magento Italy
 
Guidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha ProjectsGuidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha ProjectsAmitaSuri
 
Code Smells and Refactoring - Satyajit Dey & Ashif Iqbal
Code Smells and Refactoring - Satyajit Dey & Ashif IqbalCode Smells and Refactoring - Satyajit Dey & Ashif Iqbal
Code Smells and Refactoring - Satyajit Dey & Ashif IqbalCefalo
 
PHP Dublin Meetup - Clean Code in PHP
PHP Dublin Meetup - Clean Code in PHPPHP Dublin Meetup - Clean Code in PHP
PHP Dublin Meetup - Clean Code in PHPParas Sood
 
Igor Miniailo - Magento 2 API Design Best Practices
Igor Miniailo - Magento 2 API Design Best PracticesIgor Miniailo - Magento 2 API Design Best Practices
Igor Miniailo - Magento 2 API Design Best PracticesAtwix
 
Mli 2017 technical intro to magento 2
Mli 2017 technical intro to magento 2Mli 2017 technical intro to magento 2
Mli 2017 technical intro to magento 2Hanoi MagentoMeetup
 
Valeriy Nayda - Best Practices in Magento 2. Based on Multi Source Inventory ...
Valeriy Nayda - Best Practices in Magento 2. Based on Multi Source Inventory ...Valeriy Nayda - Best Practices in Magento 2. Based on Multi Source Inventory ...
Valeriy Nayda - Best Practices in Magento 2. Based on Multi Source Inventory ...Atwix
 
MidwestPHP - Getting Started with Magento 2
MidwestPHP - Getting Started with Magento 2MidwestPHP - Getting Started with Magento 2
MidwestPHP - Getting Started with Magento 2Mathew Beane
 
Backward Compatibility Developer's Guide in Magento 2. #MM17CZ
Backward Compatibility Developer's Guide in Magento 2. #MM17CZBackward Compatibility Developer's Guide in Magento 2. #MM17CZ
Backward Compatibility Developer's Guide in Magento 2. #MM17CZIgor Miniailo
 
Volodymyr Kublytskyi - Develop Product, Design Platform
Volodymyr Kublytskyi - Develop Product, Design PlatformVolodymyr Kublytskyi - Develop Product, Design Platform
Volodymyr Kublytskyi - Develop Product, Design PlatformMeet Magento Italy
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13Terry Yoast
 
Макс Екатериненко - Meet Magento Ukraine - Magento 2 Overview
Макс Екатериненко - Meet Magento Ukraine - Magento 2 OverviewМакс Екатериненко - Meet Magento Ukraine - Magento 2 Overview
Макс Екатериненко - Meet Magento Ukraine - Magento 2 OverviewAtwix
 
Testing in Magento 2
Testing in Magento 2 Testing in Magento 2
Testing in Magento 2 Igor Miniailo
 
Using REST and UI Testing to Test Ajax Web Applications
Using REST and UI Testing to Test Ajax Web ApplicationsUsing REST and UI Testing to Test Ajax Web Applications
Using REST and UI Testing to Test Ajax Web ApplicationsAdam Sandman
 

Similar to Magento Technical guidelines (20)

Magento 2 Automated Testing via examples of Multi-Source Inventory (MSI)
Magento 2 Automated Testing via examples of Multi-Source Inventory (MSI)Magento 2 Automated Testing via examples of Multi-Source Inventory (MSI)
Magento 2 Automated Testing via examples of Multi-Source Inventory (MSI)
 
Automated Testing in Magento 2
Automated Testing in Magento 2Automated Testing in Magento 2
Automated Testing in Magento 2
 
Magento 2.3 Schema and Data Patches
Magento 2.3 Schema and Data PatchesMagento 2.3 Schema and Data Patches
Magento 2.3 Schema and Data Patches
 
Backward Compatibility Developer's Guide Webinar
Backward Compatibility Developer's Guide WebinarBackward Compatibility Developer's Guide Webinar
Backward Compatibility Developer's Guide Webinar
 
API design best practices
API design best practicesAPI design best practices
API design best practices
 
Eugene Shakhsuvarov - Improving enterprise store scalability using AMQP and A...
Eugene Shakhsuvarov - Improving enterprise store scalability using AMQP and A...Eugene Shakhsuvarov - Improving enterprise store scalability using AMQP and A...
Eugene Shakhsuvarov - Improving enterprise store scalability using AMQP and A...
 
Guidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha ProjectsGuidelines and Best Practices for Sencha Projects
Guidelines and Best Practices for Sencha Projects
 
Code Smells and Refactoring - Satyajit Dey & Ashif Iqbal
Code Smells and Refactoring - Satyajit Dey & Ashif IqbalCode Smells and Refactoring - Satyajit Dey & Ashif Iqbal
Code Smells and Refactoring - Satyajit Dey & Ashif Iqbal
 
PHP Dublin Meetup - Clean Code in PHP
PHP Dublin Meetup - Clean Code in PHPPHP Dublin Meetup - Clean Code in PHP
PHP Dublin Meetup - Clean Code in PHP
 
Igor Miniailo - Magento 2 API Design Best Practices
Igor Miniailo - Magento 2 API Design Best PracticesIgor Miniailo - Magento 2 API Design Best Practices
Igor Miniailo - Magento 2 API Design Best Practices
 
Mli 2017 technical intro to magento 2
Mli 2017 technical intro to magento 2Mli 2017 technical intro to magento 2
Mli 2017 technical intro to magento 2
 
Valeriy Nayda - Best Practices in Magento 2. Based on Multi Source Inventory ...
Valeriy Nayda - Best Practices in Magento 2. Based on Multi Source Inventory ...Valeriy Nayda - Best Practices in Magento 2. Based on Multi Source Inventory ...
Valeriy Nayda - Best Practices in Magento 2. Based on Multi Source Inventory ...
 
MidwestPHP - Getting Started with Magento 2
MidwestPHP - Getting Started with Magento 2MidwestPHP - Getting Started with Magento 2
MidwestPHP - Getting Started with Magento 2
 
Backward Compatibility Developer's Guide in Magento 2. #MM17CZ
Backward Compatibility Developer's Guide in Magento 2. #MM17CZBackward Compatibility Developer's Guide in Magento 2. #MM17CZ
Backward Compatibility Developer's Guide in Magento 2. #MM17CZ
 
Mvc architecture
Mvc architectureMvc architecture
Mvc architecture
 
Volodymyr Kublytskyi - Develop Product, Design Platform
Volodymyr Kublytskyi - Develop Product, Design PlatformVolodymyr Kublytskyi - Develop Product, Design Platform
Volodymyr Kublytskyi - Develop Product, Design Platform
 
9781337102087 ppt ch13
9781337102087 ppt ch139781337102087 ppt ch13
9781337102087 ppt ch13
 
Макс Екатериненко - Meet Magento Ukraine - Magento 2 Overview
Макс Екатериненко - Meet Magento Ukraine - Magento 2 OverviewМакс Екатериненко - Meet Magento Ukraine - Magento 2 Overview
Макс Екатериненко - Meet Magento Ukraine - Magento 2 Overview
 
Testing in Magento 2
Testing in Magento 2 Testing in Magento 2
Testing in Magento 2
 
Using REST and UI Testing to Test Ajax Web Applications
Using REST and UI Testing to Test Ajax Web ApplicationsUsing REST and UI Testing to Test Ajax Web Applications
Using REST and UI Testing to Test Ajax Web Applications
 

More from Elogic Magento Development

Миграция кода с Magento 1 на Magento 2
Миграция кода с Magento 1 на Magento 2Миграция кода с Magento 1 на Magento 2
Миграция кода с Magento 1 на Magento 2Elogic Magento Development
 
CQRS and Event-Sourcing in Magento2 by examples of MSI
CQRS and Event-Sourcing in Magento2 by examples of MSICQRS and Event-Sourcing in Magento2 by examples of MSI
CQRS and Event-Sourcing in Magento2 by examples of MSIElogic Magento Development
 
Chernivtsi Magento Meetup&Contribution day. Miniailo.I.
Chernivtsi Magento Meetup&Contribution day. Miniailo.I. Chernivtsi Magento Meetup&Contribution day. Miniailo.I.
Chernivtsi Magento Meetup&Contribution day. Miniailo.I. Elogic Magento Development
 
Chernivtsi Magento Meetup&Contribution day. V. Kublytskyi
 Chernivtsi Magento Meetup&Contribution day. V. Kublytskyi Chernivtsi Magento Meetup&Contribution day. V. Kublytskyi
Chernivtsi Magento Meetup&Contribution day. V. KublytskyiElogic Magento Development
 
12 Ways to Improve Magento 2 Security and Performance
12 Ways to Improve Magento 2 Security and Performance12 Ways to Improve Magento 2 Security and Performance
12 Ways to Improve Magento 2 Security and PerformanceElogic Magento Development
 
Как благодаря композеру использовать сторонние компоненты в Magento 2
Как благодаря композеру использовать сторонние компоненты в Magento 2Как благодаря композеру использовать сторонние компоненты в Magento 2
Как благодаря композеру использовать сторонние компоненты в Magento 2Elogic Magento Development
 
Як перехід на Magento допоміг нам стати лідером
Як перехід на Magento допоміг нам стати лідеромЯк перехід на Magento допоміг нам стати лідером
Як перехід на Magento допоміг нам стати лідеромElogic Magento Development
 
Как переписать модуль с Magento 1 на Magento 2
Как переписать модуль с Magento 1 на Magento 2Как переписать модуль с Magento 1 на Magento 2
Как переписать модуль с Magento 1 на Magento 2Elogic Magento Development
 

More from Elogic Magento Development (15)

Миграция кода с Magento 1 на Magento 2
Миграция кода с Magento 1 на Magento 2Миграция кода с Magento 1 на Magento 2
Миграция кода с Magento 1 на Magento 2
 
Introduction to Magento Community
Introduction to Magento Community Introduction to Magento Community
Introduction to Magento Community
 
CQRS and Event-Sourcing in Magento2 by examples of MSI
CQRS and Event-Sourcing in Magento2 by examples of MSICQRS and Event-Sourcing in Magento2 by examples of MSI
CQRS and Event-Sourcing in Magento2 by examples of MSI
 
Chernivtsi Magento Meetup&Contribution day. Miniailo.I.
Chernivtsi Magento Meetup&Contribution day. Miniailo.I. Chernivtsi Magento Meetup&Contribution day. Miniailo.I.
Chernivtsi Magento Meetup&Contribution day. Miniailo.I.
 
Chernivtsi Magento Meetup&Contribution day. V. Kublytskyi
 Chernivtsi Magento Meetup&Contribution day. V. Kublytskyi Chernivtsi Magento Meetup&Contribution day. V. Kublytskyi
Chernivtsi Magento Meetup&Contribution day. V. Kublytskyi
 
The process of a Lean Magento development
The process of a Lean Magento developmentThe process of a Lean Magento development
The process of a Lean Magento development
 
12 Ways to Improve Magento 2 Security and Performance
12 Ways to Improve Magento 2 Security and Performance12 Ways to Improve Magento 2 Security and Performance
12 Ways to Improve Magento 2 Security and Performance
 
MMnl Pavlo Okhrem
MMnl Pavlo Okhrem MMnl Pavlo Okhrem
MMnl Pavlo Okhrem
 
LIOF 2016
LIOF 2016LIOF 2016
LIOF 2016
 
Payment integration patterns в Magento2
Payment integration patterns в Magento2Payment integration patterns в Magento2
Payment integration patterns в Magento2
 
Как благодаря композеру использовать сторонние компоненты в Magento 2
Как благодаря композеру использовать сторонние компоненты в Magento 2Как благодаря композеру использовать сторонние компоненты в Magento 2
Как благодаря композеру использовать сторонние компоненты в Magento 2
 
Magento 2 - the future of eCommerce
Magento 2 - the future of eCommerceMagento 2 - the future of eCommerce
Magento 2 - the future of eCommerce
 
RequireJS і Magento 2
RequireJS і Magento 2RequireJS і Magento 2
RequireJS і Magento 2
 
Як перехід на Magento допоміг нам стати лідером
Як перехід на Magento допоміг нам стати лідеромЯк перехід на Magento допоміг нам стати лідером
Як перехід на Magento допоміг нам стати лідером
 
Как переписать модуль с Magento 1 на Magento 2
Как переписать модуль с Magento 1 на Magento 2Как переписать модуль с Magento 1 на Magento 2
Как переписать модуль с Magento 1 на Magento 2
 

Recently uploaded

Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 

Recently uploaded (20)

E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 

Magento Technical guidelines

  • 1. © 2018 Magento, Inc. Page | 1 Magento Technical Guidelines Eugene Shakhsuvarov, Software Engineer @ Magento
  • 2. © 2018 Magento, Inc. Page | 2
  • 3. © 2018 Magento, Inc. Page | 3 Magento 2 Technical Guidelines • Document which describes the desired technical state of Magento 2 • Hundreds of architect-hours invested into development of guidelines • May seem too restrictive and sometimes unobvious in favor of code readability and extensibility • All of the new core code must follow the rules http://devdocs.magento.com/guides/v2.2/coding-standards/technical- guidelines/technical-guidelines.html
  • 4. © 2018 Magento, Inc. Page | 4 Basic Principles
  • 5. © 2018 Magento, Inc. Page | 5 Strict types • Starting with Magento 2.3 only PHP 7.1+ is supported • Explicit return types must be declared on functions • Type hints for scalar arguments should be used • Declaration of strict_types is encouraged where possible
  • 6. © 2018 Magento, Inc. Page | 6 Class Design
  • 7. © 2018 Magento, Inc. Page | 7 Object Manager • Generally Object Manager should not be used as a class dependency • Doing so decreases ability for third parties to customize your code
  • 8. © 2018 Magento, Inc. Page | 8 Don’t do this • In this case dependency is hard coded and can not be replaced with a different one • Would require more work to modify the behavior by third parties
  • 9. © 2018 Magento, Inc. Page | 9 Do this instead • Now logger may be changed with a simple configuration in di.xml • Behavior is changed without any additional coding
  • 10. © 2018 Magento, Inc. Page | 10 There are exceptions to this rule • Object Manager may still be used in classes which create objects (Factories, Builders) • May also be used in the core code to maintain backwards compatibility
  • 11. © 2018 Magento, Inc. Page | 11 Inheritance • Inheritance should not be used. Composition should be used instead • Magento is moving away from the inheritance as a main code reuse mechanism • Inheritance enforces dependency on a specific parent class which can not be replaced in runtime • Overwriting logic based on inheritance requires a lot of boilerplate code
  • 12. © 2018 Magento, Inc. Page | 12 Inheritance Example • Simple task of formatting product price • May be perfectly fine in conditions, where third parties do not need to modify the behavior • Requires complete replacement of the default renderer, instead of customizing it
  • 13. © 2018 Magento, Inc. Page | 13 Composition Example • Price Formatter may be easily replaced by injecting another dependency in the constructor • Allows precise modification of behavior
  • 14. © 2018 Magento, Inc. Page | 14 Inheritance Pros • Inheritance may still be applicable in specific cases, for example when class is a subtype of another class
  • 15. © 2018 Magento, Inc. Page | 15 Single Responsibility • All Classes should have only Single Responsibility which is entirely incapsulated • Mixing different behaviors in one class (e. g. classic Helper) greatly decreases extensibility and increases coupling in most of the cases • Allows for easy replacement of any specific behavior by providing a single point for change
  • 16. © 2018 Magento, Inc. Page | 16 Constructing a Class • Object must be ready for use after instantiation • No additional public initialization methods are allowed • Constructor should throw an exception when validation of an argument has failed
  • 17. © 2018 Magento, Inc. Page | 17 Constructor Dependency Injection • All dependencies must be requested by the most generic type that is required by the client object • Class constructor can have only dependency assignment operations and/or argument validation operations • Proxies and interceptors must never be explicitly requested in constructors
  • 18. © 2018 Magento, Inc. Page | 18 Class members visibility • All non-public properties and methods should be private • Discourages use of inheritance • Protected properties are much harder to remove from the class, as some client code could be already extending it
  • 19. © 2018 Magento, Inc. Page | 19 Temporal Coupling • Temporal coupling must be avoided • Semantic dependencies between methods is prone to errors as client code never knows the current state of the system • Method chaining in class design must be avoided https://ocramius.github.io/blog/fluent-interfaces-are-evil/
  • 20. © 2018 Magento, Inc. Page | 20 Object State • Service classes, ones that provide behavior but not data, should not have a state • Only data objects or entities may have an observable state • Getters should not change the state of an object
  • 21. © 2018 Magento, Inc. Page | 21 Principle of least knowledge • Class should have limited knowledge about other classes • Object should only call methods only on its “friends” • Do not “talk” to strangers
  • 22. © 2018 Magento, Inc. Page | 22 Interception
  • 23. © 2018 Magento, Inc. Page | 23 Interception best practices • Avoid implementing a plugin when different kind of extension point is available • Plugins should not be used within own module • Plugins should not be added to data objects • Plugins must be stateless
  • 24. © 2018 Magento, Inc. Page | 24 “Around” Plugins • Should only be used when behavior of an original method is supposed to be substituted in certain scenarios • Performance penalty is increased compared to other types of plugins • Generate a lot of stack frames making it harder to debug the code
  • 25. © 2018 Magento, Inc. Page | 25 Exception Handling
  • 26. © 2018 Magento, Inc. Page | 26 Exception handling • Exceptions must not be handled in the same function where they are thrown • Business logic (both application and domain) must not be managed with exceptions. Conditional statements should be used instead • All direct communications with third-party libraries must be wrapped with a try/catch statement
  • 27. © 2018 Magento, Inc. Page | 27 Exceptions Logging • It is not allowed to absorb exceptions with no logging or/and any workaround operation executed • Any exception should be logged only in the catch block where it is processed, and should not be re-thrown
  • 28. © 2018 Magento, Inc. Page | 28 Application Layers
  • 29. © 2018 Magento, Inc. Page | 29 Presentation Layer • Request, Response, Session, Store Manager and Cookie objects must be used only in the Presentation layer • Controllers should only call appropriate services and return ResultInterface implementation • Controllers must be as lightweight as possible • LocalizedException should only be thrown in the Presentation layer (Controllers, Blocks)
  • 30. © 2018 Magento, Inc. Page | 30 Service Layer • Service layer is a contract for every specific module • Service layer Interfaces may be used both as API and SPI • Service Contracts should follow CQRS principle
  • 31. © 2018 Magento, Inc. Page | 31 Persistence Layer • Always separate business logic and persistence logic • Entities must not contain persistence-related logic • Entities may be persisted in different scopes • Every persistence operation must be performed with one scope set
  • 32. Page | 32© 2018 Magento, Inc. Thank you! Q & A