Post on 28-Oct-2014
description
VIRTUES OF PLATFORM DEVELOPMENT
Introduction
Phillip JacksonSomething Digital
@philwinklegithub.com/philwinkle
Frameworks Let You Build ...Something.
Frameworks should…
• Be tasked to build large application structures• Be lean and unopinionated• Contain toolkits that help you solve hard programming problems• I need access control• I need to talk to a database• I need to write some stuff to cache storage
General-purpose libraries form general-purpose communities.
Framework development is for really really smart people. ..
I’m not one of them.
Platform DevelopmentTeaches You to Build Rapidly
• Platforms help you solve hard business problems• I need a customer login form• I need to add products to a cart• I need to accept multiple forms of payment
• It helps you think in terms of solving real-world problems
Stand on the Shoulders of Giants
Everything I know I learned from Magento
“Don’t talk to strangers”
• Class types (final, abstract)• Class/variable scope• Private/protected
“Hold Mommy’s hand”
• Factories• Autoloaders• Bootstrap
“Look both ways before crossing the street”
• Consult the documentation• I kid, I kid.• For the most part, Magento obeys its own rules,
follow them!
• Ask the community
Examples of Core ‘documentation’• Queued workers• Indexing
• ACL• Customer Account route/controllers
• Form validation• Customer registration form
• Orm/Resource models• Sales, Wishlist
• Grids• Sales and Reports
• Reports• Direct SQL (haha jklol) – see here:
http://magento.stackexchange.com/a/7963/336
“Don’t talk with your mouth full”•Lazy Loading
Don’t do this:
Mage::getModel(‘sales/order’)->getCollection()->load();
Do this:
Mage::getModel(‘sales/order’)->getCollection()->getFirst();
…or this:
$collection = Mage::getModel(‘sales/order’)->getCollection();foreach($collection as $order){ //stuff}
“Your face will get stuck like that”
• Dirty Hacks• Best Practice
I’m just going to do this:
echo $product->getMyAwesomeAttribute(); //=> null //hack- fix it later$myProduct = Mage::getModel(‘catalog/product’)->load($product->getId());//=> “this is working”
…cont$collection = Mage::getModel('catalog/product')->getCollection(); $backendModel = $collection->getResource()->getAttribute('media_gallery')->getBackend(); foreach ($collection as $product){
//add gallery to the product$backendModel->afterLoad($product);
}
“Use your manners”• Depends• Controller rewrites• Observers
Predispatch strategy:
<?xml version="1.0"?><global> <events> <controller_action_predispatch_customer_account_index> <observers> <myevent_this_should_be_unique> <class>YourCompany_YourModule_Model_Observer</class> <method>yourMethodName</method> </myevent_this_should_be_unique> </observers> </controller_action_predispatch_customer_account_index> </events></global>
“Don’t talk back”• Closures• Create new closures and hand them around• They’re first-class functions – handy for array_walk:
array_walk($array,function(&$var){ //do stuff to $var});
Don’t do this (but it’s fun)Mage::register(‘closure’,function( return “hello world”; )); //later…$closure = Mage::registry(‘closure’);$closure();//=>”hello world”
…avoiding-difficult-things-to-debug-as-a-service
“Use your inside voice”• Dependency/constructor injection
LIVE DEMO (oh boy…)
If at first you don’t succeed,TRY, try again• Exception routing patternsAvoid:
try { $model>doSomething(); //=>throws} catch(Exception $e){ $this->setSomething(‘wuuuuut’);}
if($this->getSomething()){//….blah}
Exceptions cont…Rather:
try { $model>doSomething(); //=>throws} catch(Exception $e){ $session->addError($this->__(‘wuuuuut’));}
“If you can’t say something nice, don’t say it at all.”
• Return patterns (this is just good programming)
Avoid:
public function getMyStuff($id){$model = Mage::getModel(‘test/test’)->load($id);if($model->getId()){ return $model;} else { return false;}
}
Cont…
Rather:
public function getMyStuff($id){$model = Mage::getModel(‘test/test’)->load($id);if(!$model){ return false;}
return $model;}
“If you can’t say something nice, don’t say it at all.” (cont…)
• Return patterns (this is just good programming)• Models return null• Observers that error don’t halt the next observers in the chain
“Do unto others”• Avoid joins on production sales tables• Shard out BI queries to read slaves • Schedule maintenance overnight• Don’t forget the maintenance.flag file
• Check for running tasks before executing• Queue and batch
“You’re not leaving this house dressed like that”
• Default theme does a lot• Catalog_Navigation is the most overridden class on the Magento Connect marketplace!
“Wait 30 minutes before getting in the POOL”
• Code pools; haha get it?• Don’t copy class files to app/code/local• Override only if necessary• Use observers liberally
• Theme fallback is awesome• Has some limitations• Magento 2 has major improvements• There are extensions to help you set up child themes, etc.
“If you play with it you’ll go blind”
price.phtmlDon’t bother trying to restyle price.phtml. It’s not worth it.
Common myths of platform development
Myths of platform development
• Platforms box you in• I can build it better myself• The platform is more complicated than the problem it solves• It’s so complex - I don’t know where to start
“Why does Magento require me to do *.*”
• Be unopinionated about simple problems• Shipping rates• Layout• Display logic• Inventory
• Be extremely opinionated about complicated problems• Taxes• Multi-store• Multi-shipping• Time zone / locale / language• Complex discount rules
• Magento didn’t make up your crazy business rules
“It’s too Zend-like”
Zend is established and secure. Where used, it steps up to the challenge. Zend pre-existed namespaces, closures, even JSON encoding as part of the core library.
“It’s not Zend enough”“Where is my config.ini and why do I have to write so much XML?”
“There’s so much XML”• YAML was the weird thing that Doctrine used• JSON was the weird thing that required eval, right?• Code generation tools make this a bit nicer• Magento 2 is worse• XML supported and understood by “enterprise” adopters
“There’s no jQuery”• Install any one of 5k+ plugins and I guarantee you they’ll install jQuery for you• Learning prototype is actually a blast - consider it a challenge
“The performance is lousy”• It’s gotten better• It’s not meant to be run on a shared server at GoDaddy or Hostgator• Plugins• Hardware• Full Page Cache• Varnish
“Magento is overengineered for what I need”• Magento Go?• OK but seriously, Magento Go.
• OK - fine. There are a ton of options out there - check out Shopify.
Zend is old because namespaces and IOC / DI
“It’s too complex”• Fair enough.
http://magento.stackexchange.com/a/4700/336
Success == Recognizing and filling a need in the marketplace
Conclusion
There is a huge need for developers. There are practically no Magento developers who can hit the ground running. [It takes weeks] to ramp a new hire up.
eBay [paraphrased]
Come support “MageOverflow”http://magento.stackexchange.com
Fastest Answers:
https://twitter.com/kalenjordan/status/385062073475923968
THANK YOU
We’re hiring!!Jon Tudhope – Director of Software
Q&A