Skip to main content

Magento 2 code optimization (2021)

 


This is my third post related the Magento 2 code optimization. In my first blog post related to the Magento 2 optimization configuration, click hereMy second blog post related to the tips for improving the Magento 2 page speed, if you are aware of it then click here

In this post,  we are learning the guide for the Magento 2 code optimization.  We will discuss the below topics.

  1. Code Optimization
  2. Review the extension at the installation time
Let's start,

Code Optimization:
Magento 2 code optimization is very important for increasing page speed. 

Session storage:
Magento sessions could be stored in a file or MySQL database. But, it really depends on your hosting environment setup. If the database performs well, then storing sessions on the database could benefit your website.

404 errors:
If any pages are not found, at a time 404 errors occur on your website. this causes the Magento application to start for every 404 encountered. You can check your apache logs to make sure of 404 errors solved.

Disable Magento logging:
Magento error information stored on the log file. When your website is in developer mode, all the created errors are stored on the below file.
Go to the var > log in your coding root file. All the available log files appear.
  • Debug.log
  • Exception.log
  • system.log
You need to fix all the required errors for the good performance of your website. Always use “production mode” in live websites.
We can enable/disabled the system log using the below command
Enable: Run the below two commands on terminal
1. Php bin/magento setup:config:set --enable-syslog-logging=true
2. php bin/magento cache:flush
Disabled: Run the below two commands on the terminal
Php bin/magento setup:config:set --enable-syslog-logging=false
Php bin/magento c:f

set of coding standards:

We need to follow the coding standard when we write the code. It’s only possible good coding practices. These standards define coding practices that determine the style of the code. Basically, Magento uses the Zend Framework.  Magento 2 coding standards are based on the following.

Zend Coding Standard.

  • PSR2
  • PSR4

For help on our project, We strongly recommend use the PHP_CodeSniffer tool

It will help your projects coding quality and provide the no of error that you need to fix it.

Develop code to be replaceable:

We need to develop the standard code and as important as making it reusable. Writing replaceable code in your code file makes this practice easier and more efficient.

Avoid creating helper classes:

We need to avoid creating helper classes on the Magento 2 module. It will be best to create only 1 OR 2 helper classes.

Helper classes function as a static function, that is why we can be used anywhere on our module class.So, A helper class that functions as a catch-all for random methods violates the single responsibility. You need to mode the function in your appropriate classes. We can use appropriate model classes. 

Avoid the around plugins:

If not required around method plugins in our module, We need to avoid it because it will affect our website performance.

A use case for around method plugins is when you need to terminate the execution of all original methods.

 Write and utilize reusable code:

Always, Avoid using duplicate code which can be hard to maintain. reuse code as much as possible to prevent code duplication. Write code that is helpful to your feature development. 

Code testing:

Code testing is more important for your module. You need to follow Magento 2 code testing guide.

Review the Installed Extension:

We need to always review the extension at the installation time 

  1. Do not use Too much logic in PHTML templates in Extension.
  2. Coding standard stuff: Purchased modules should follow the official Magento coding standard.
  3. Use the ObjectManager in the correct way: When the ObjectManager is used in the wrong places, it always creates many issues. 
  4. Too many dependencies: always use DI in the correct way. Too many dependencies were injected if it's not used the correct way.
  5. Always use  before() and after() instead of around() plugins.
  6. always use LESS for design.


Comments

Popular posts from this blog

Introduction to flask

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session because we are entering into one of python’s web-based application called flask. In this article, we are going to see the following What is flask Prequistes Installation of flask in python Some of flask template engine. What is flask? Flask is one of python-based framework which is used to create web applications. Flight is a very light web framework. During installation, flask comes with pre-installed modules and functions which are used to backup your own web applications. Flask is very easy to understand and perfect go-to for beginners and with flask, a web server can run in less than 3 lines of code. Prequistes Before learning flask, we recommend the reader to have a basic mastery of python concepts. Installation of flask  Before installing flask, we have to checked if python has been installed or. If n...

How to generate random numbers using NumP1

Hello. Welcome to another edition on this platform. For more better understanding, do checkout our YouTube channel to get the video tutorial. In this article of today, we are going to see how to generate random numbers using any of the following methods: Generating a random number Generating a random float and integer Generating random number arrays Generating random number from an array What is a random number? This is a number which cannot be predicted before its occurrence. This number might not different every time. Programmatically, they are two categories of random numbers:     Pseudo-Random numbers       True Random numbers. Just as programs which are written by programmers are a set of instructions, we must follow an algorithm to generate random numbers. Random numbers which are generated using an algorithm are called Pseudo-Random numbers. To generate a true random number, it is important to get the data from sources such as the keyboards, mou...

Introduction to Django

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session. we are entering into one of python’s application called Django. In this article, we are going to discuss the following: What is Django Why must we use Django  A brief history of Django Installation of Django Popularity of Django What is Django? Python has so many framework application and Django happen to be one of them. Being a python-based-framework, it is used to quickly create web applications.  When building websites, django provides similar ready-made components to handle user authentication, forms, a way to upload components. Why use django? Django is very fast. It takes applications from concept to applications very quickly Django has thousand available packages in it when it is installed. With django, we can launch web applications is a matter of hours. Django is a highly is secured and helps...