Based on the first post related to best practices in Magento 1 (https://community.magento.com/t5/Share-Your-Best-Practices-Tips/Magento-1-x-Best-Practices-and-Conv...), I create this post to discuss about the best practices and conventions in Magento 2.
I will start by adding the best practices from Magento 1 that also apply to Magento 2. Later, I will add the new best practices that apply only to Magento 2, and I will also add your best practices to this post.
By doing that, we can use this first post as a "Wiki" for best practices and conventions for Magento 2.
Back-end
- Do not modify the Magento core: app/code/Magento
This is the #1 rule when it comes to customizing Magento. To be specific, the files from the app/code/Magento folder should never be modified to ensure the stability and upgradability of the Magento store.
- Avoid rewriting classes, use Magento 2 plugins instead
- If you rewrite a class, then annotate the changes you made to the core file
- Avoid implementing a lot of observers
- Remember setting magento in Developer Mode through the bin/magento command line tool
- Always turn off the cache in your local machine to avoid losing time
- Document your classes and methods using PHPDoc comments
- Limit the usage of PHP in the front-end
- Do not use direct SQL queries to connect to the database, use models instead
- Follow Magento naming conventions for classes, methods and files
- Methods and variable names should use camelCase
- Indexed arrays should use snake_case keys
- PHP variable names in the front-end should start with $_ (underscore after $ sign)
Front-end
- Don't use inline CSS in the front-end
- You can use tables, but only if you want to present tabular data
- Use image sprites to load icons in your theme