Magento Technical Guidelines

Similar documents
Magento 2 Certified Professional Developer. Exam Study Guide

Object-Oriented Design

Magento 2 Code Customizations

Chapter 5 Object-Oriented Programming

Standard. Number of Correlations

API Knowledge Coding Guide Version 7.2

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

Checked and Unchecked Exceptions in Java

ECE 3574: Dynamic Polymorphism using Inheritance

Programming for the Web with PHP

Socket attaches to a Ratchet. 2) Bridge Decouple an abstraction from its implementation so that the two can vary independently.

foreword to the first edition preface xxi acknowledgments xxiii about this book xxv about the cover illustration

Inheritance STL. Entity Component Systems. Scene Graphs. Event Systems

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

Index. Index. More information. block statements 66 y 107 Boolean 107 break 55, 68 built-in types 107

C++ for System Developers with Design Pattern

CS304 Object Oriented Programming Final Term

DESIGN PATTERN - INTERVIEW QUESTIONS

Software Project Seminar VII: Tools of the Craft. 23 march 2006 Jevgeni Kabanov

Dependency Injection Container Documentation

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Developer Experience

Atelier Java - J2. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

Call: JSP Spring Hibernate Webservice Course Content:35-40hours Course Outline

SDC Design patterns GoF

Written by John Bell for CS 342, Spring 2018

Chapter 8: Class and Method Design

Enterprise JavaBeans 3.1

/ / JAVA TRAINING

Chapter 1 Introducing EJB 1. What is Java EE Introduction to EJB...5 Need of EJB...6 Types of Enterprise Beans...7

PROCESS DEVELOPMENT METHODOLOGY The development process of an API fits the most fundamental iterative code development

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

Advanced C++ Programming Workshop (With C++11, C++14, C++17) & Design Patterns

Introduction to PHP. Handling Html Form With Php. Decisions and loop. Function. String. Array

INHERITANCE WITH JAVA INTERFACES

OO and Ahh! An Introduction to Object Oriented Programming With PHP. Division 1 Systems. John Valance. Copyright John Valance Division 1 Systems

Object-Oriented Design

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

DOT NET Syllabus (6 Months)

C++ (Non for C Programmer) (BT307) 40 Hours

C++ Important Questions with Answers

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

2 ADT Programming User-defined abstract data types

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

JavaScript: Sort of a Big Deal,

JAVA. Duration: 2 Months

be used for more than one use case (for instance, for use cases Create User and Delete User, one can have one UserController, instead of two separate

BEAAquaLogic. Service Bus. Interoperability With EJB Transport

A Primer on Design Patterns

The Design Patterns Matrix From Analysis to Implementation

CPS 506 Comparative Programming Languages. Programming Language

Everything is an object. Almost, but all objects are of type Object!

JVA-117A. Spring-MVC Web Applications

What is it? CMSC 433 Programming Language Technologies and Paradigms Spring Approach 1. Disadvantage of Approach 1

BBM 102 Introduction to Programming II Spring Inheritance

Lecture 5: Inheritance

SUMMARY: MODEL DRIVEN SECURITY

A Reconnaissance on Design Patterns

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

The de constructed. Magento module

Chapter 5 Names, Binding, Type Checking and Scopes

abstract binary class composition diamond Error Exception executable extends friend generic hash implementation implements

step is to see how C++ implements type polymorphism, and this Exploration starts you on that journey.

C a; C b; C e; int c;

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Basic Object-Oriented Concepts. 5-Oct-17

Software Design and Analysis for Engineers

Object Oriented Methods with UML. Introduction to Design Patterns- Lecture 8

Object-Oriented Concepts and Design Principles

A Conceptual Model of the UML

Testing Object-Oriented Software. 22 November 2017

What is Inheritance?

Safety SPL/2010 SPL/20 1

Automatic Code Generation for Non-Functional Aspects in the CORBALC Component Model

Introduction to Programming Using Java (98-388)

Lightweight J2EE Framework

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

OBJECT ORIENTED PROGRAMMING USING C++

1: Introduction to Object (1)

How We Refactor, and How We Know It

Polymorphic Associations Library

Comp215: Thinking Generically

CmSc 150 Fundamentals of Computing I. Lesson 28: Introduction to Classes and Objects in Java. 1. Classes and Objects

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 7

Software Engineering Fall 2015 (CSC 4350/6350) TR. 5:30 pm 7:15 pm. Rao Casturi 11/03/2015

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Implementing a Web Service p. 110 Implementing a Web Service Client p. 114 Summary p. 117 Introduction to Entity Beans p. 119 Persistence Concepts p.

Application Architectures, Design Patterns

AADL Graphical Editor Design

Intro to OOP Visibility/protection levels and constructors Friend, convert constructor, destructor Operator overloading a<=b a.

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

CGS 2405 Advanced Programming with C++ Course Justification

Page 1

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Midterm 2. 7] Explain in your own words the concept of a handle class and how it s implemented in C++: What s wrong with this answer?

Exception Handling Introduction. Error-Prevention Tip 13.1 OBJECTIVES

Technical Debt in Eclipse Development. Eclipse Con France 2018

Chapter 10 Classes Continued. Fundamentals of Java

CS 251, Intermediate Programming Midterm Exam October 9, 2013

Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Transcription:

Magento Technical Guidelines Eugene Shakhsuvarov, Software Engineer @ Magento 2018 Magento, Inc. Page 1

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/technicalguidelines/technical-guidelines.html 2018 Magento, Inc. Page 2

2018 Magento, Inc. Page 3 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 2018 Magento, Inc. Page 4

2018 Magento, Inc. Page 5 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 2018 Magento, Inc. Page 6

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 7

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 8

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 9

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 10

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 11

Composition Example Price Formatter may be easily replaced by injecting another dependency in the constructor Allows precise modification of behavior 2018 Magento, Inc. Page 12

Inheritance Pros Inheritance may still be applicable in specific cases, for example when class is a subtype of another class 2018 Magento, Inc. Page 13

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 14

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 15

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 16

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 17

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 18

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 19

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 20

2018 Magento, Inc. Page 21 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 2018 Magento, Inc. Page 22

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 23

2018 Magento, Inc. Page 24 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 2018 Magento, Inc. Page 25

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 26

2018 Magento, Inc. Page 27 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) 2018 Magento, Inc. Page 28

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 29

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 2018 Magento, Inc. Page 30

Thank you! Q & A 2018 Magento, Inc. Page 31