Magento Technical Guidelines Eugene Shakhsuvarov, Software Magento

Slides:



Advertisements
Similar presentations
Web Applications Development Using Coldbox Platform Eddie Johnston.
Advertisements

CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
Intro to OOP with Java, C. Thomas Wu Inheritance and Polymorphism
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Feb. 23, 2004CS WPI1 CS 509 Design of Software Systems Lecture #5 Monday, Feb. 23, 2004.
Chapter 1 Principles of Programming and Software Engineering.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
8.1 Classes & Inheritance Inheritance Objects are created to model ‘things’ Sometimes, ‘things’ may be different, but still have many attributes.
CVSQL 2 The Design. System Overview System Components CVSQL Server –Three network interfaces –Modular data source provider framework –Decoupled SQL parsing.
Introduction to the Enterprise Library. Sounds familiar? Writing a component to encapsulate data access Building a component that allows you to log errors.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Chapter 8 More Object Concepts
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
Component frameworks Roy Kensmil. Historical trens in software development. ABSTRACT INTERACTIONS COMPONENT BUS COMPONENT GLUE THIRD-PARTY BINDING.
Dependency Injection Technion – Institute of Technology Author: Gal Lalouche - Technion 2015 ©
Best Practices. Contents Bad Practices Good Practices.
SWE © Solomon Seifu ELABORATION. SWE © Solomon Seifu Lesson 12-5 Software Engineering Design Goals.
Chapter 8 Object Design Reuse and Patterns. Object Design Object design is the process of adding details to the requirements analysis and making implementation.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Reformatted slides from the textbook, C++ How to Program, 6/e Pearson Education, Inc. All rights reserved Chapter 3. [Lecture 02] Introduction to.
Object Oriented Software Development
Exceptions, cont’d. Factory Design Pattern COMP 401 Fall 2014 Lecture 12 9/30/2014.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Object Oriented Programming
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Software Engineering and Object-Oriented Design Topics: Solutions Modules Key Programming Issues Development Methods Object-Oriented Principles.
C++ Inheritance Data Structures & OO Development I 1 Computer Science Dept Va Tech June 2007 © McQuain Generalization versus Abstraction Abstraction:simplify.
Introduction to Object-Oriented Programming Lesson 2.
Component Patterns – Architecture and Applications with EJB copyright © 2001, MATHEMA AG Component Patterns Architecture and Applications with EJB Markus.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Enterprise Library 3.0 Memi Lavi Solution Architect Microsoft Consulting Services Guy Burstein Senior Consultant Advantech – Microsoft Division.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
By Aleksey Stukalov How to Develop Highly Customizable Off-the-Shelf Software.
Java Programming Fifth Edition Chapter 9 Introduction to Inheritance.
Data Abstraction: The Walls
Object-Oriented Design
Polymorphism, Interfaces & Operator Overloading
Object-Oriented Programming: Inheritance
MPCS – Advanced java Programming
Inheritance and Polymorphism
Chapter 11 Object-Oriented Design
Principles of Programming and Software Engineering
Indexer AKEEL AHMED.
TIM 58 Chapter 8: Class and Method Design
Chapter 6: Using Design Patterns
Functional Programming with Java
Object Oriented Practices
Understanding Inheritance
Advanced Programming Behnam Hatami Fall 2017.
Partnership.
The Object-Oriented Thought Process Chapter 05
Lecture 22 Inheritance Richard Gesick.
Object Oriented Programming: Inheritance
Introduction to Classes and Objects
Object Oriented Practices
Programming in C# Lesson 5. Exceptions..
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
SAMANVITHA RAMAYANAM 18TH FEBRUARY 2010 CPE 691
Object-Oriented Programming: Inheritance and Polymorphism
Fundaments of Game Design
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Computer Science 340 Software Design & Testing
CMSC 202 Exceptions.
Classes and Objects Systems Programming.
Presentation transcript:

Magento Technical Guidelines Eugene Shakhsuvarov, Software Engineer @ Magento

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

Basic Principles

Strict types Starting with Magento 2.2 only PHP 7.0 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

Class Design

Object Manager Generally Object Manager should not be used as a class dependency Doing so decreases ability for third parties to customize your code

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

Do this instead Now logger may be changed with a simple configuration in di.xml Behavior is changed without any additional coding

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

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

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

Composition Example Price Formatter may be easily replaced by injecting another dependency in the constructor Allows precise modification of behavior

Inheritance Pros Inheritance may still be applicable in specific cases, for example when class is a subtype of another class

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

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

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

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

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/

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

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

Interception

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

“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

Exception Handling

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 A separate exceptions hierarchy should be defined on each application layer. It is allowed to throw exceptions that are only defined on the same layer

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

Application Layers

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)

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

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

Thank you! Q & A