Magento 2 code optimization (2021)


Magento 2 code optimization

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.

Ask Question #trycoder
Facebook Page👉
Watch more videos here
Click to SUBSCRIBE to more Videos!

Please Like, Comment, Share and Subscribe THANK YOU!