SlideShare a Scribd company logo
1 of 42
Scaling PHP
Josh Levine
josh@shapeways.com
@heavypennies
1
WHAT IS SHAPEWAYS?
2
Shapeways
3
http://www.shapeways.com
http://developers.shapeways.com
http://shapejs.shapeways.com
The worlds leading 3D printing
marketplace and community
ARCHITECTURE
4
Architecture - System
5
Architecture - Framework
6
Road Runner
Architecture – Performance History
7
PATTERNS
PARADIGMS
GOVERNANCE
8
Patterns, Paradigms, and Governance
9
• How do these concepts help us scale PHP ?
• Will I see concrete actionable examples ?
• Will you show me some hardcore PHP h4x ?
• Patterns – code
• Paradigms – knowledge
• Governance – process
10
PATTERNS
11
Programatically enforced goodness
Patterns – Traits of Goodness
12
• Accessibility
• Repeatability
• Clear Rationale
Patterns - Accessibility
13
• An accessible pattern is one that is
intuitive, documented, and when
invoked, reads like natural language
• Reading code that follows an accessible
pattern is easier than reading code that
does not follow
Patterns - Repeatability
14
• The repeatability of a pattern will
ultimately decide its adoption
• If I can copy and paste, or use a scaffold
for a pattern, the pattern is repeatable
and will proliferate itself throughout the
code
Patterns – Clear Rationale
15
• You need to be able to explain why a
pattern was chosen
• All patterns have shortcomings – the
*why* behind the pattern’s design and
proliferation is critical in maintaining
adherence
Pattern Examples: MVC
16
abstract class RoadRunnerController {
$model = $this->getModel();
if($this->preHandle($model)) {
$this->handleRequest($model);
$this->postHandle($model);
}
$this->finalize($model);
}
Pattern Examples: Auto-wiring
17
// Autowire the model manager
public function preHandle($shapewaysModel, ModelManager $modelManager) {
// do something with the ModelManager
return parent::preHandle($shapewaysModel);
}
I can use any Manager in our
domain model by adding it to my
function signature.
Pattern Examples: Auto-wiring
18
public function invokeWithManagers($object, $function, $parameters)
{
$controllerClass = get_class($object);
$function = new ReflectionMethod($controllerClass, $function);
foreach ($function->getParameters() as $reflectionParameter) {
$parameterClass = $reflectionParameter->getClass();
$parameterClassName = $parameterClass->getName();
// substr is much faster than pregmatch!
if (substr($parameterClassName, -7) === "Manager") {
$parameters[] =
$this->instantiateManager($parameterClassName);
}
}
return $function->invokeArgs($object, $parameters);
}
PARADIGMS
19
Good things you need to know
20
Paradigms – Traits of Goodness
• Explicit
• Consistent
• Provide Clear Value
21
Paradigms – Explicit
A paradigm is explicit when the functionality
exposed can be understood from the
invoking side.
Explicit paradigms show visibly throughout a
codebase.
22
Paradigms – Consistent
A paradigm is consistent when it is applied
consistently and everywhere applicable in
the code.
If your paradigm is only consistent in 60% of
your code base, chances are high that folks
will copy and proliferate code that violates
the paradigm.
23
Paradigms – Provide Clear Value
It is important for the entire team to
understand and agree on the value of a
paradigm.
Paradigms can easily feel like overhead - it
is important to know *why* we choose to
adhere to a paradigm.
Paradigm Examples - Constants
$myObj->callFunction(
',', true, false, false
);
Vs.
$myObj->callFunction(
MyObj::DELIMITER_COMMA,
MyObj::MATCH_MULTIPLE,
MyObj::LOG_RESULTS,
MyObj::SKIP_DUPLICATES
);
24
Paradigm Examples – More Constants
$_REQUEST["userId"]
Vs.
const REQUEST_USER_ID = "userId";
…
$_REQUEST[self::REQUEST_USER_ID];
25
Paradigm Examples - Consistent
• Code Style
• Enum/Lookup Tables
• Parameter marshaling
26
Paradigm Examples – More Constants
27
Paradigm Examples – One to Many
Making one query vs. two is often faster
SELECT umf.id
…
, mfma.model_file_material_asset_id
…
FROM udesign_model_file umf
LEFT JOIN model_file_material_asset mfma
ON mfma.model_file_id = umf.id
28
Paradigm Examples – One to Many
Generic function for mapping objects
/**
* Select an array of objects using the given EntityMapper
*
* @param $db the database link to use (read-only/read-write/etc)
* @param $sql the query
* @param $mapper EntityMapper
* @return array
*/
public function selectObjects($db, $sql, $mapper)
{
$db = $this->getDbLink($db);
$this->checkDBConnection($db);
// Add backtrace information to the SQL as a comment
// This helps immensely in slow query identification
$sql = $this->prependURL($sql);
if ($resultSet = $this->runQuery($sql, $db)) {
while ($row = mysql_fetch_assoc($resultSet)) {
$mapper->mapRow($row);
}
mysql_free_result($resultSet);
}
return $mapper->getResults();
}
29
Paradigm Examples – One to Many
Mapping results follows a paradigm
public function mapRow($row)
{
if (!isset($this->rows[$row['id']])) {
$this->rows[$row['id']] = new ModelFileEntity(
$row['id']
);
}
$modelFile = $this->rows[$row['id']];
if (isset($row['model_file_material_asset_id'])) {
$modelFileMaterialAsset = new ModelFileMaterialAsset(
$row['model_file_material_asset_id']
);
$modelFile->addModelFileMaterialAsset($modelFileMaterialAsset);
}
}
30
Paradigm Examples – Caching
Cache derived data
public function getPrice()
{
if ($this->getPrice_cache !== NULL) return $this->getPrice_cache;
// if override is set, use that
if (($this->getPrice_cache = $this->getPriceOverride()) !== NULL) {
return $this->getPrice_cache;
} else {
return ($this->getPrice_cache = $this->getPriceOriginal());
}
}
31
Paradigm Examples – Caching
Give control to the engineers
public function getCurrencyById() {
$cacheKey = 'currencyById-' .
$currencyId . '-' .
date(RoadRunnerDB::DATE_FORMAT_DAY);
$currency = null;
if(RoadRunnerLocalCache::get($cacheKey, $currency) === FALSE) {
if (($currency = $this->memcacheGet($cacheKey, 0)) === FALSE) {
$currency = $this->currencyDB->getcurrencyById($currencyId);
$this->memcacheSet($cacheKey, $currency, 0);
}
RoadRunnerLocalCache::set($cacheKey, $currency);
}
return $currency;
}
32
GOVERNANCE
33
For the good of all 
Governance
Governance is the most difficult of all the
goodness to achieve, and likely it is the most
important for performance.
If you can find a problem before it is live, you
are 10x more likely to fix it before it impacts
your users and your bottom line.
34
Governance Example – DB Query Review
35
Governance Example – DB Query Review
36
Governance Example – DB Query Review
37
Governance Example – Tracking
38
Governance Example – Tracking
watch -n1 "cat rps_uri.sql | mysql tracking"
39
Governance Example – Tracking
40
Governance Example – Quality
41
THANK YOU
42

More Related Content

What's hot

DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)
Oleg Zinchenko
 
Web app development_php_04
Web app development_php_04Web app development_php_04
Web app development_php_04
Hassen Poreya
 

What's hot (19)

Smarty
SmartySmarty
Smarty
 
The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)The art of readable code (ch1~ch4)
The art of readable code (ch1~ch4)
 
Sencha Touch
Sencha TouchSencha Touch
Sencha Touch
 
Design patterns in PHP
Design patterns in PHPDesign patterns in PHP
Design patterns in PHP
 
Php Tutorials for Beginners
Php Tutorials for BeginnersPhp Tutorials for Beginners
Php Tutorials for Beginners
 
DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)DDD on example of Symfony (Webcamp Odessa 2014)
DDD on example of Symfony (Webcamp Odessa 2014)
 
Vision academy classes bcs_bca_bba_sybba_php
Vision academy  classes bcs_bca_bba_sybba_phpVision academy  classes bcs_bca_bba_sybba_php
Vision academy classes bcs_bca_bba_sybba_php
 
PHP MVC
PHP MVCPHP MVC
PHP MVC
 
Let's write secure Drupal code! - 13.09.2018 @ Drupal Europe, Darmstadt, Germany
Let's write secure Drupal code! - 13.09.2018 @ Drupal Europe, Darmstadt, GermanyLet's write secure Drupal code! - 13.09.2018 @ Drupal Europe, Darmstadt, Germany
Let's write secure Drupal code! - 13.09.2018 @ Drupal Europe, Darmstadt, Germany
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
Drupaljam xl 2019 presentation multilingualism makes better programmers
Drupaljam xl 2019 presentation   multilingualism makes better programmersDrupaljam xl 2019 presentation   multilingualism makes better programmers
Drupaljam xl 2019 presentation multilingualism makes better programmers
 
Introduction to PHP Lecture 1
Introduction to PHP Lecture 1Introduction to PHP Lecture 1
Introduction to PHP Lecture 1
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
Drupal 8: Routing & More
Drupal 8: Routing & MoreDrupal 8: Routing & More
Drupal 8: Routing & More
 
Twig Brief, Tips&Tricks
Twig Brief, Tips&TricksTwig Brief, Tips&Tricks
Twig Brief, Tips&Tricks
 
Basics of Java Script (JS)
Basics of Java Script (JS)Basics of Java Script (JS)
Basics of Java Script (JS)
 
Php Oop
Php OopPhp Oop
Php Oop
 
Web app development_php_04
Web app development_php_04Web app development_php_04
Web app development_php_04
 
(Parameterized) Roles
(Parameterized) Roles(Parameterized) Roles
(Parameterized) Roles
 

Viewers also liked

Ehsaas e Niha : Shyam Sunder
Ehsaas e Niha  :  Shyam SunderEhsaas e Niha  :  Shyam Sunder
Ehsaas e Niha : Shyam Sunder
ehsaaseniha
 

Viewers also liked (8)

August Agenda for Southwest California Legislative Council
August Agenda for Southwest California Legislative CouncilAugust Agenda for Southwest California Legislative Council
August Agenda for Southwest California Legislative Council
 
Purushottam vijay first jail yatra 1930 32
Purushottam vijay first jail yatra 1930 32Purushottam vijay first jail yatra 1930 32
Purushottam vijay first jail yatra 1930 32
 
May 2014 Realtor Report
May 2014 Realtor ReportMay 2014 Realtor Report
May 2014 Realtor Report
 
Ehsaas e Niha : Shyam Sunder
Ehsaas e Niha  :  Shyam SunderEhsaas e Niha  :  Shyam Sunder
Ehsaas e Niha : Shyam Sunder
 
Draft Urban Advertisement Policy, Madhya Pradesh
Draft Urban Advertisement Policy, Madhya PradeshDraft Urban Advertisement Policy, Madhya Pradesh
Draft Urban Advertisement Policy, Madhya Pradesh
 
SWCLC Bill Tracker through 6/17/13
SWCLC Bill Tracker through 6/17/13SWCLC Bill Tracker through 6/17/13
SWCLC Bill Tracker through 6/17/13
 
TREB Housing Market Charts June 2012
TREB Housing Market Charts June 2012TREB Housing Market Charts June 2012
TREB Housing Market Charts June 2012
 
2014 bill tracker final 2
2014 bill tracker final 22014 bill tracker final 2
2014 bill tracker final 2
 

Similar to Patterns, Paradigms, and Governance 2013-10-03

DDD on example of Symfony (SfCampUA14)
DDD on example of Symfony (SfCampUA14)DDD on example of Symfony (SfCampUA14)
DDD on example of Symfony (SfCampUA14)
Oleg Zinchenko
 
Clean code for WordPress
Clean code for WordPressClean code for WordPress
Clean code for WordPress
mtoppa
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
elliando dias
 
Creating "Secure" PHP Applications, Part 1, Explicit Code & QA
Creating "Secure" PHP Applications, Part 1, Explicit Code & QACreating "Secure" PHP Applications, Part 1, Explicit Code & QA
Creating "Secure" PHP Applications, Part 1, Explicit Code & QA
archwisp
 
第49回Php勉強会@関東 Datasource
第49回Php勉強会@関東 Datasource第49回Php勉強会@関東 Datasource
第49回Php勉強会@関東 Datasource
Kaz Watanabe
 

Similar to Patterns, Paradigms, and Governance 2013-10-03 (20)

WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег ЗинченкоWebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
WebCamp: Developer Day: DDD in PHP on example of Symfony - Олег Зинченко
 
Objects, Testing, and Responsibility
Objects, Testing, and ResponsibilityObjects, Testing, and Responsibility
Objects, Testing, and Responsibility
 
Fatc
FatcFatc
Fatc
 
Building Testable PHP Applications
Building Testable PHP ApplicationsBuilding Testable PHP Applications
Building Testable PHP Applications
 
Scaling php applications with redis
Scaling php applications with redisScaling php applications with redis
Scaling php applications with redis
 
Phpspec tips&tricks
Phpspec tips&tricksPhpspec tips&tricks
Phpspec tips&tricks
 
Advanced Php - Macq Electronique 2010
Advanced Php - Macq Electronique 2010Advanced Php - Macq Electronique 2010
Advanced Php - Macq Electronique 2010
 
DDD on example of Symfony (SfCampUA14)
DDD on example of Symfony (SfCampUA14)DDD on example of Symfony (SfCampUA14)
DDD on example of Symfony (SfCampUA14)
 
Advanced symfony Techniques
Advanced symfony TechniquesAdvanced symfony Techniques
Advanced symfony Techniques
 
Clean code for WordPress
Clean code for WordPressClean code for WordPress
Clean code for WordPress
 
PHP and Rich Internet Applications
PHP and Rich Internet ApplicationsPHP and Rich Internet Applications
PHP and Rich Internet Applications
 
Kicking off with Zend Expressive and Doctrine ORM (PHPNW2016)
Kicking off with Zend Expressive and Doctrine ORM (PHPNW2016)Kicking off with Zend Expressive and Doctrine ORM (PHPNW2016)
Kicking off with Zend Expressive and Doctrine ORM (PHPNW2016)
 
Hexagonal architecture in PHP
Hexagonal architecture in PHPHexagonal architecture in PHP
Hexagonal architecture in PHP
 
Bhavesh ro r
Bhavesh ro rBhavesh ro r
Bhavesh ro r
 
Solid principles
Solid principlesSolid principles
Solid principles
 
Java colombo-deep-dive-into-jax-rs
Java colombo-deep-dive-into-jax-rsJava colombo-deep-dive-into-jax-rs
Java colombo-deep-dive-into-jax-rs
 
Spl Not A Bridge Too Far phpNW09
Spl Not A Bridge Too Far phpNW09Spl Not A Bridge Too Far phpNW09
Spl Not A Bridge Too Far phpNW09
 
Quality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStormQuality assurance for php projects with PHPStorm
Quality assurance for php projects with PHPStorm
 
Creating "Secure" PHP Applications, Part 1, Explicit Code & QA
Creating "Secure" PHP Applications, Part 1, Explicit Code & QACreating "Secure" PHP Applications, Part 1, Explicit Code & QA
Creating "Secure" PHP Applications, Part 1, Explicit Code & QA
 
第49回Php勉強会@関東 Datasource
第49回Php勉強会@関東 Datasource第49回Php勉強会@関東 Datasource
第49回Php勉強会@関東 Datasource
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 

Recently uploaded (20)

Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 

Patterns, Paradigms, and Governance 2013-10-03

  • 9. Patterns, Paradigms, and Governance 9 • How do these concepts help us scale PHP ? • Will I see concrete actionable examples ? • Will you show me some hardcore PHP h4x ?
  • 10. • Patterns – code • Paradigms – knowledge • Governance – process 10
  • 12. Patterns – Traits of Goodness 12 • Accessibility • Repeatability • Clear Rationale
  • 13. Patterns - Accessibility 13 • An accessible pattern is one that is intuitive, documented, and when invoked, reads like natural language • Reading code that follows an accessible pattern is easier than reading code that does not follow
  • 14. Patterns - Repeatability 14 • The repeatability of a pattern will ultimately decide its adoption • If I can copy and paste, or use a scaffold for a pattern, the pattern is repeatable and will proliferate itself throughout the code
  • 15. Patterns – Clear Rationale 15 • You need to be able to explain why a pattern was chosen • All patterns have shortcomings – the *why* behind the pattern’s design and proliferation is critical in maintaining adherence
  • 16. Pattern Examples: MVC 16 abstract class RoadRunnerController { $model = $this->getModel(); if($this->preHandle($model)) { $this->handleRequest($model); $this->postHandle($model); } $this->finalize($model); }
  • 17. Pattern Examples: Auto-wiring 17 // Autowire the model manager public function preHandle($shapewaysModel, ModelManager $modelManager) { // do something with the ModelManager return parent::preHandle($shapewaysModel); } I can use any Manager in our domain model by adding it to my function signature.
  • 18. Pattern Examples: Auto-wiring 18 public function invokeWithManagers($object, $function, $parameters) { $controllerClass = get_class($object); $function = new ReflectionMethod($controllerClass, $function); foreach ($function->getParameters() as $reflectionParameter) { $parameterClass = $reflectionParameter->getClass(); $parameterClassName = $parameterClass->getName(); // substr is much faster than pregmatch! if (substr($parameterClassName, -7) === "Manager") { $parameters[] = $this->instantiateManager($parameterClassName); } } return $function->invokeArgs($object, $parameters); }
  • 20. 20 Paradigms – Traits of Goodness • Explicit • Consistent • Provide Clear Value
  • 21. 21 Paradigms – Explicit A paradigm is explicit when the functionality exposed can be understood from the invoking side. Explicit paradigms show visibly throughout a codebase.
  • 22. 22 Paradigms – Consistent A paradigm is consistent when it is applied consistently and everywhere applicable in the code. If your paradigm is only consistent in 60% of your code base, chances are high that folks will copy and proliferate code that violates the paradigm.
  • 23. 23 Paradigms – Provide Clear Value It is important for the entire team to understand and agree on the value of a paradigm. Paradigms can easily feel like overhead - it is important to know *why* we choose to adhere to a paradigm.
  • 24. Paradigm Examples - Constants $myObj->callFunction( ',', true, false, false ); Vs. $myObj->callFunction( MyObj::DELIMITER_COMMA, MyObj::MATCH_MULTIPLE, MyObj::LOG_RESULTS, MyObj::SKIP_DUPLICATES ); 24
  • 25. Paradigm Examples – More Constants $_REQUEST["userId"] Vs. const REQUEST_USER_ID = "userId"; … $_REQUEST[self::REQUEST_USER_ID]; 25
  • 26. Paradigm Examples - Consistent • Code Style • Enum/Lookup Tables • Parameter marshaling 26
  • 27. Paradigm Examples – More Constants 27
  • 28. Paradigm Examples – One to Many Making one query vs. two is often faster SELECT umf.id … , mfma.model_file_material_asset_id … FROM udesign_model_file umf LEFT JOIN model_file_material_asset mfma ON mfma.model_file_id = umf.id 28
  • 29. Paradigm Examples – One to Many Generic function for mapping objects /** * Select an array of objects using the given EntityMapper * * @param $db the database link to use (read-only/read-write/etc) * @param $sql the query * @param $mapper EntityMapper * @return array */ public function selectObjects($db, $sql, $mapper) { $db = $this->getDbLink($db); $this->checkDBConnection($db); // Add backtrace information to the SQL as a comment // This helps immensely in slow query identification $sql = $this->prependURL($sql); if ($resultSet = $this->runQuery($sql, $db)) { while ($row = mysql_fetch_assoc($resultSet)) { $mapper->mapRow($row); } mysql_free_result($resultSet); } return $mapper->getResults(); } 29
  • 30. Paradigm Examples – One to Many Mapping results follows a paradigm public function mapRow($row) { if (!isset($this->rows[$row['id']])) { $this->rows[$row['id']] = new ModelFileEntity( $row['id'] ); } $modelFile = $this->rows[$row['id']]; if (isset($row['model_file_material_asset_id'])) { $modelFileMaterialAsset = new ModelFileMaterialAsset( $row['model_file_material_asset_id'] ); $modelFile->addModelFileMaterialAsset($modelFileMaterialAsset); } } 30
  • 31. Paradigm Examples – Caching Cache derived data public function getPrice() { if ($this->getPrice_cache !== NULL) return $this->getPrice_cache; // if override is set, use that if (($this->getPrice_cache = $this->getPriceOverride()) !== NULL) { return $this->getPrice_cache; } else { return ($this->getPrice_cache = $this->getPriceOriginal()); } } 31
  • 32. Paradigm Examples – Caching Give control to the engineers public function getCurrencyById() { $cacheKey = 'currencyById-' . $currencyId . '-' . date(RoadRunnerDB::DATE_FORMAT_DAY); $currency = null; if(RoadRunnerLocalCache::get($cacheKey, $currency) === FALSE) { if (($currency = $this->memcacheGet($cacheKey, 0)) === FALSE) { $currency = $this->currencyDB->getcurrencyById($currencyId); $this->memcacheSet($cacheKey, $currency, 0); } RoadRunnerLocalCache::set($cacheKey, $currency); } return $currency; } 32
  • 34. Governance Governance is the most difficult of all the goodness to achieve, and likely it is the most important for performance. If you can find a problem before it is live, you are 10x more likely to fix it before it impacts your users and your bottom line. 34
  • 35. Governance Example – DB Query Review 35
  • 36. Governance Example – DB Query Review 36
  • 37. Governance Example – DB Query Review 37
  • 38. Governance Example – Tracking 38
  • 39. Governance Example – Tracking watch -n1 "cat rps_uri.sql | mysql tracking" 39
  • 40. Governance Example – Tracking 40