Best practice in developing extensions

Hi guys, as you all know how it is important to create perfect Magento modules to make customers and developers happy. In this post we would like to share some tips about module development, that I hope will help you in future. So lets start.

1. Make it as simple as possible

Imagine that we need to develop Magento module that should change checkout functionality. As you know Magento checkout is really complicated, has a lot of steps and very often customers want to see something simple - something like one step checkout module. The page where customer fills data in and just clicks "Purchase" button. So first thing is making user-friendly extension, it should be easy to use it even by your grandma.

2. Code Style

I faced a lot of extensions that made me really unhappy. The reason is in bad-formatted code without using standards like Zend Coding Style or PSR and without comments. I'd recommend you to follow the code standards and comment your code in details. And never use encoders for PHP code like ionCube unless you want to be cursed by Magento community.

3. File hierarchy

As we know a Magento extension has its own folder with all files except templates/layouts/CSS/JS. And I would like to talk about those files and how to deal with them. Firstly, I'd say you should follow Magento standards, creating some custom files and so on isn't a good idea. Magento has special js/ folder for storing JS files, but there is one issue, when you put JS files into that directory it doesn't allow to modify these files because it will cause problems with extension upgrades. So a better idea is to put your JS files into skin/frontend/base/default/<modulename>/js/ folder. So when somebody needs to modify your file they can easily move that file to current theme and change it safely. Also, one more good idea is creating <module>/ folder inside skin/frontend/base/default/<module>/ (for storing JS and CSS) and app/design/frontend/base/default/template/<module>/, app/design/frontend/base/default/layout/<module>/ for storing templates and layouts. It allows to find your files easily, and all files will be located inside same folder name.

4. CSS

If it's possible use existing Magento styles when you design modules, I mean when you need to create three columns use classes .col3-set, col-1, col-2, etc... It allows your module to look exactly like site, provided the design was created using Magento based styles.

5. Inline JS/CSS

Never use inline JS/CSS. Sure you can use some inline JS like var Form = new VarienForm('form'); but nothing else.

6. Avoid rewrites

The rewrites are bottlenecks of Magento extensions, there a lot of modules that rewrite same block/model/etc... and it causes a lot of problems for developers to make extensions working. So just avoid rewrites if possible. It's better to use observer to enhance core functionality.

7. Use logging properly

It's really helpful to write errors and debug info to log files. It allows to find the reason of failure quickly. But there is one bottleneck: you shouldn't write everything into magento var/log/system.log, instead use your own file, for example var/log/hexbrain.log. The following code snippet allows to write info into custom file: Mage::log('Error Message', Zend_Log::ALERT, 'hexbrain.log', true);. But why we need to use separate files? The reason is simple, it allows us to find the problematic extension quickly just by seeing log files modified time and also it prevents growth of your system.log. In this case system log contains only critical error messages and you can easily find it.

8. Reinventing the wheel

The main idea of module development is using existing Magento code style and methods to manipulate data and requests. I've faced mysql_connect function inside magento template once, that's terrible. Always make sure to check if the logic you are going to develop isn't already developed by Magento team.


Thank you for reading our article. Looking forward to reading your advices in comments.

Blog Comments powered by Disqus.