SlideShare ist ein Scribd-Unternehmen logo
1 von 80
Object Oriented Apologetics Vance Lucas CodeWorks 2009 Dallas September 27, 2009
What? 2 Not an apology Greek root - apologia (Î±Ï€ÎżÎ»ÎżÎłÎŻÎ±) - “speaking in defense” To defend the use of, and provide rational reasoning for
3 Who is it for? For People Who: Are “on the fence” about OOP vs procedural Are unconvinced of the usefulness of OOP Want to learn WHY they should learn about OOP
4 Purpose: To Get You Hooked on OOP
5 NO Academic or Mundane Examples
6 No Shapes, Cars, Fruit, or Bicycles
<?phpclass myClass {     public function myClass(){     }     public function echoMe(){         echo 'me';     } } $mine=new myClass(); $mine->echoMe(); ?> 7 No “Hello World” Scripts
So
 Why OOP?
Polymorphism I  nheritance E ncapsulation 9
10 Polymorphism
Making things that are not the same look the same Relies on a defined interface Inheritance is probably the most used method The ability of type A to be used like type B Think: Interchangeable types or components 11 Polymorphism
12 Real World: Different Implementations
13 Procedural - Inline
14 Object-oriented – Polymorphic Interface
15 Inheritance
Extend from a parent class “is-a” relationship Creates hierarchal relationship Get functionality for free Global changes are easier Inherits all functions and properties from parent Think: A is a B, with a few differences 16 Inheritance
17 Controller: Zend Framework
18 Model: phpDataMapper
19 Warning: Keep Hierarchy Shallow
20 Encapsulation
Hide specific implementation details Reveal only methods and properties required to interact with the object Limits interdependencies between components Think: Separation of responsibilities 21 Encapsulation
22 Payment Interface – Exposed Methods
23 Planetoids (by Micah Jones)
[object Object]
Different: velocities, sizes, shapes, rotations, color, and fragment pieces
All the code and math calculations for movement are encapsulated behind ‘move()’
Allows different types of objects and asteroids to be treated the same in code – “polymorphically”24 Planetoids: Code Abstraction
Other OOP-Only Features? 25
Lazy-Loading
Uses __get() “magic” method in PHP5 object model Also uses SPL interfaces to fire query on: count() foreach() Used as a hook to retrieve related rows on call Caches results so only 1 query is fired Can eliminate N+1 query problem by mapping 27 Lazy-Loading: OOP Only
Classes are actually custom types Can type-hint for classes or interfaces PHP Standard types: string, int, float, boolean, array, object, null resource 28 Custom Type Creation & Type-Hinting
Other Reasons TO Use OOP?
Easily group related properties/data Avoid using globals or passing/returning arrays Suppress errors ala ‘undefined index’ More? Convenience (a.k.a. Laziness) 30
Request object: Why not $_POST? Data comes from multiple sources POST/GET, XML, JSON, etc. Other functions isAjax(), isPost(), etc. Sanitizing user input Session object: Why not $_SESSION? More options for saving/storing Database, separate server, memcached, etc. 31 Request / Session Objects
32 Request Object: Convenience, too!
Need an Example?
E-commerce Cart: Work Scope ,[object Object]
Simple products, no options, stock, etc.
Simple checkout, no user accounts
Authorize.net integration
UPS rate quotes
Admin backend
Order fulfillment
Invoice/packing slip printing,[object Object]
Simple Checkout 36
37 Simple UPS Integration
So far it’s OK It works We finished and worked quickly 38 Status Check
39 Client Message I talked to my next door neighbor’s cousin’s brother’s niece yesterday, and he says all the serious online stores have regular sales. That’s something I can do too, right? - Bob
40 Product Sales
41 New Code for Product Sale
We also have to add this code to the admin backend for customer invoices. And to the email reciepts Sin of code duplication Code smell 42 Thoughts
43 Client Message Hey,I was at the grocery store yesterday and my daughter got 2 candy bars for $1, when they were originally $0.75 each. I know that if I am able to do this, I’ll get a lot of sales and it will make me rich. I need to be able to do this. - Bob
44 Multi-Quantity Discounts
New Code, Again 45
We also have to add this code to the admin backend and other places again. Sin of code duplication We could use procedural functions for this Where would we put them? What responsibilities do they have? 46 Thoughts
47 What about the future?
48 Employee Discounts
49 Switching to FedEx
Stock Checking 50
Clearly, as the project grows, it will become a maintenance nightmare if we continue on the current path. We don’t want our code to be the running joke of the PHP community. 51 We need something better
52
53 Use the right tool for the right job
54 OOP: Right tool for this job
Create a Cart class to store items Encapsulate the pricing logic in an Item class Single place to change the code Item is responsible for knowing it’s price (?) What does this imply? 55 Thoughts
56 Possible Code Changes
Still storing cart in session, but now we can change it later when we need to scale Cart gets item price so it can check quantities Cart is responsible for knowing other items in cart Better separation of responsibility It’s not the job of the display logic to calculate the item’s price or apply discounts What about changing to FedEx? 57 Thoughts
58 Re-factor it into two classes
Package is responsible for knowing it’s own dimensions and weight Quote is responsible for fetching a live rate quote from a carrier API service Always think in terms of responsibility What code is responsible for what functions? Where does it go in my app? Is this code doing too much? 59 Thoughts
60 Think about how an assembly line works
OOP Myths and Misconceptions
62 Myth #1: OOP is about code re-use
63 Truth: Re-useable code is a by-product of good OO
64 Problem: There are lots of ways to make re-useable code that are not object-oriented nor good. It’s a bad goal.
<Code with functions and includes – re-useable> 65 Functions are re-useable
66 Specific implementations are re-useable
<UPS-Specific API Code> or <Payment Gateway-specific API Code> 67 Soft interfaces are re-useable
You must set goals that will help direct you to your desired outcome Goals narrow attention and direct efforts to goal-relevant activities, and away from perceived undesirable and goal-irrelevant actions Re-use as a goal does not help you write good OO code. Re-use is a by-product of good OO. 68 Point: Set Good Goals
69 Myth #2: Objects should always be modeled after real-world objects when possible
70 Truth: Objects should be modeled and built based on what you need to complete your task
71 Problem: Real-world object models are almost never useful in code

Weitere Àhnliche Inhalte

Was ist angesagt?

Packer Genetics: The selfish code
Packer Genetics: The selfish codePacker Genetics: The selfish code
Packer Genetics: The selfish codejduart
 
Software development best practices & coding guidelines
Software development best practices & coding guidelinesSoftware development best practices & coding guidelines
Software development best practices & coding guidelinesAnkur Goyal
 
Beyond Unit Testing
Beyond Unit TestingBeyond Unit Testing
Beyond Unit TestingSĂžren Lund
 
C# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringC# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringEyob Lube
 
Writing High Quality Code in C#
Writing High Quality Code in C#Writing High Quality Code in C#
Writing High Quality Code in C#Svetlin Nakov
 
Documenting Code - Patterns and Anti-patterns - NLPW 2016
Documenting Code - Patterns and Anti-patterns - NLPW 2016Documenting Code - Patterns and Anti-patterns - NLPW 2016
Documenting Code - Patterns and Anti-patterns - NLPW 2016SĂžren Lund
 
Mark asoi ppt
Mark asoi pptMark asoi ppt
Mark asoi pptmark-asoi
 
ShaREing Is Caring
ShaREing Is CaringShaREing Is Caring
ShaREing Is Caringsporst
 
The "Evils" of Optimization
The "Evils" of OptimizationThe "Evils" of Optimization
The "Evils" of OptimizationBlackRabbitCoder
 
Protecting JavaScript source code using obfuscation - OWASP Europe Tour 2013 ...
Protecting JavaScript source code using obfuscation - OWASP Europe Tour 2013 ...Protecting JavaScript source code using obfuscation - OWASP Europe Tour 2013 ...
Protecting JavaScript source code using obfuscation - OWASP Europe Tour 2013 ...AuditMark
 
How to really obfuscate your pdf malware
How to really obfuscate your pdf malwareHow to really obfuscate your pdf malware
How to really obfuscate your pdf malwarezynamics GmbH
 
C# Programming: Fundamentals
C# Programming: FundamentalsC# Programming: Fundamentals
C# Programming: FundamentalsMahmoud Abdallah
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#Hawkman Academy
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ Ganesh Samarthyam
 
Coldfusion
ColdfusionColdfusion
ColdfusionRam
 
Writing clean code in C# and .NET
Writing clean code in C# and .NETWriting clean code in C# and .NET
Writing clean code in C# and .NETDror Helper
 
Refactoring: Improve the design of existing code
Refactoring: Improve the design of existing codeRefactoring: Improve the design of existing code
Refactoring: Improve the design of existing codeValerio Maggio
 
Principles in Refactoring
Principles in RefactoringPrinciples in Refactoring
Principles in RefactoringChamnap Chhorn
 

Was ist angesagt? (19)

Packer Genetics: The selfish code
Packer Genetics: The selfish codePacker Genetics: The selfish code
Packer Genetics: The selfish code
 
Software development best practices & coding guidelines
Software development best practices & coding guidelinesSoftware development best practices & coding guidelines
Software development best practices & coding guidelines
 
Beyond Unit Testing
Beyond Unit TestingBeyond Unit Testing
Beyond Unit Testing
 
C# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoringC# coding standards, good programming principles & refactoring
C# coding standards, good programming principles & refactoring
 
Writing High Quality Code in C#
Writing High Quality Code in C#Writing High Quality Code in C#
Writing High Quality Code in C#
 
Documenting Code - Patterns and Anti-patterns - NLPW 2016
Documenting Code - Patterns and Anti-patterns - NLPW 2016Documenting Code - Patterns and Anti-patterns - NLPW 2016
Documenting Code - Patterns and Anti-patterns - NLPW 2016
 
C#
C#C#
C#
 
Mark asoi ppt
Mark asoi pptMark asoi ppt
Mark asoi ppt
 
ShaREing Is Caring
ShaREing Is CaringShaREing Is Caring
ShaREing Is Caring
 
The "Evils" of Optimization
The "Evils" of OptimizationThe "Evils" of Optimization
The "Evils" of Optimization
 
Protecting JavaScript source code using obfuscation - OWASP Europe Tour 2013 ...
Protecting JavaScript source code using obfuscation - OWASP Europe Tour 2013 ...Protecting JavaScript source code using obfuscation - OWASP Europe Tour 2013 ...
Protecting JavaScript source code using obfuscation - OWASP Europe Tour 2013 ...
 
How to really obfuscate your pdf malware
How to really obfuscate your pdf malwareHow to really obfuscate your pdf malware
How to really obfuscate your pdf malware
 
C# Programming: Fundamentals
C# Programming: FundamentalsC# Programming: Fundamentals
C# Programming: Fundamentals
 
C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#C# 101: Intro to Programming with C#
C# 101: Intro to Programming with C#
 
OO Design and Design Patterns in C++
OO Design and Design Patterns in C++ OO Design and Design Patterns in C++
OO Design and Design Patterns in C++
 
Coldfusion
ColdfusionColdfusion
Coldfusion
 
Writing clean code in C# and .NET
Writing clean code in C# and .NETWriting clean code in C# and .NET
Writing clean code in C# and .NET
 
Refactoring: Improve the design of existing code
Refactoring: Improve the design of existing codeRefactoring: Improve the design of existing code
Refactoring: Improve the design of existing code
 
Principles in Refactoring
Principles in RefactoringPrinciples in Refactoring
Principles in Refactoring
 

Andere mochten auch

B-S-T Easy as 1-2-3
B-S-T Easy as 1-2-3B-S-T Easy as 1-2-3
B-S-T Easy as 1-2-3Chris Cornutt
 
Patterns and Tools for Database Versioning, Migration, Data Loading and Test ...
Patterns and Tools for Database Versioning, Migration, Data Loading and Test ...Patterns and Tools for Database Versioning, Migration, Data Loading and Test ...
Patterns and Tools for Database Versioning, Migration, Data Loading and Test ...Alan Pinstein
 
The anatomy of an international checkout page
The anatomy of an international checkout pageThe anatomy of an international checkout page
The anatomy of an international checkout pagemrscammels
 
Stackbox CMS: Next-Generation Content Management
Stackbox CMS: Next-Generation Content ManagementStackbox CMS: Next-Generation Content Management
Stackbox CMS: Next-Generation Content ManagementVance Lucas
 
Bullet: The Functional PHP Micro-Framework
Bullet: The Functional PHP Micro-FrameworkBullet: The Functional PHP Micro-Framework
Bullet: The Functional PHP Micro-FrameworkVance Lucas
 
How to Evaluate Your App Idea
How to Evaluate Your App IdeaHow to Evaluate Your App Idea
How to Evaluate Your App IdeaVance Lucas
 
ORM: Object-relational mapping
ORM: Object-relational mappingORM: Object-relational mapping
ORM: Object-relational mappingAbhilash M A
 
Your first 5 PHP design patterns - ThatConference 2012
Your first 5 PHP design patterns - ThatConference 2012Your first 5 PHP design patterns - ThatConference 2012
Your first 5 PHP design patterns - ThatConference 2012Aaron Saray
 
Alloy HMVC PHP Framework
Alloy HMVC PHP FrameworkAlloy HMVC PHP Framework
Alloy HMVC PHP FrameworkVance Lucas
 
Cross-Platform Mobile Development with Titanium
Cross-Platform Mobile Development with TitaniumCross-Platform Mobile Development with Titanium
Cross-Platform Mobile Development with TitaniumVance Lucas
 
Proved PHP Design Patterns for Data Persistence
Proved PHP Design Patterns for Data PersistenceProved PHP Design Patterns for Data Persistence
Proved PHP Design Patterns for Data PersistenceGjero Krsteski
 
Building Data Mapper PHP5
Building Data Mapper PHP5Building Data Mapper PHP5
Building Data Mapper PHP5Vance Lucas
 
Services Oriented Architecture with PHP and MySQL
Services Oriented Architecture with PHP and MySQLServices Oriented Architecture with PHP and MySQL
Services Oriented Architecture with PHP and MySQLJoe Stump
 
Object Relational Mapping in PHP
Object Relational Mapping in PHPObject Relational Mapping in PHP
Object Relational Mapping in PHPRob Knight
 
25 php interview questions – codementor
25 php interview questions – codementor25 php interview questions – codementor
25 php interview questions – codementorArc & Codementor
 
MVC with Zend Framework
MVC with Zend FrameworkMVC with Zend Framework
MVC with Zend Frameworkwebholics
 

Andere mochten auch (16)

B-S-T Easy as 1-2-3
B-S-T Easy as 1-2-3B-S-T Easy as 1-2-3
B-S-T Easy as 1-2-3
 
Patterns and Tools for Database Versioning, Migration, Data Loading and Test ...
Patterns and Tools for Database Versioning, Migration, Data Loading and Test ...Patterns and Tools for Database Versioning, Migration, Data Loading and Test ...
Patterns and Tools for Database Versioning, Migration, Data Loading and Test ...
 
The anatomy of an international checkout page
The anatomy of an international checkout pageThe anatomy of an international checkout page
The anatomy of an international checkout page
 
Stackbox CMS: Next-Generation Content Management
Stackbox CMS: Next-Generation Content ManagementStackbox CMS: Next-Generation Content Management
Stackbox CMS: Next-Generation Content Management
 
Bullet: The Functional PHP Micro-Framework
Bullet: The Functional PHP Micro-FrameworkBullet: The Functional PHP Micro-Framework
Bullet: The Functional PHP Micro-Framework
 
How to Evaluate Your App Idea
How to Evaluate Your App IdeaHow to Evaluate Your App Idea
How to Evaluate Your App Idea
 
ORM: Object-relational mapping
ORM: Object-relational mappingORM: Object-relational mapping
ORM: Object-relational mapping
 
Your first 5 PHP design patterns - ThatConference 2012
Your first 5 PHP design patterns - ThatConference 2012Your first 5 PHP design patterns - ThatConference 2012
Your first 5 PHP design patterns - ThatConference 2012
 
Alloy HMVC PHP Framework
Alloy HMVC PHP FrameworkAlloy HMVC PHP Framework
Alloy HMVC PHP Framework
 
Cross-Platform Mobile Development with Titanium
Cross-Platform Mobile Development with TitaniumCross-Platform Mobile Development with Titanium
Cross-Platform Mobile Development with Titanium
 
Proved PHP Design Patterns for Data Persistence
Proved PHP Design Patterns for Data PersistenceProved PHP Design Patterns for Data Persistence
Proved PHP Design Patterns for Data Persistence
 
Building Data Mapper PHP5
Building Data Mapper PHP5Building Data Mapper PHP5
Building Data Mapper PHP5
 
Services Oriented Architecture with PHP and MySQL
Services Oriented Architecture with PHP and MySQLServices Oriented Architecture with PHP and MySQL
Services Oriented Architecture with PHP and MySQL
 
Object Relational Mapping in PHP
Object Relational Mapping in PHPObject Relational Mapping in PHP
Object Relational Mapping in PHP
 
25 php interview questions – codementor
25 php interview questions – codementor25 php interview questions – codementor
25 php interview questions – codementor
 
MVC with Zend Framework
MVC with Zend FrameworkMVC with Zend Framework
MVC with Zend Framework
 

Ähnlich wie Object Oriented Apologetics

Oops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperOops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperNyros Technologies
 
Stop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principlesStop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principlesEdorian
 
PHP Interview Questions for Freshers 2018
PHP Interview Questions for Freshers 2018PHP Interview Questions for Freshers 2018
PHP Interview Questions for Freshers 2018AshokKumar3319
 
Confoo 2024 Gettings started with OpenAI and data science
Confoo 2024 Gettings started with OpenAI and data scienceConfoo 2024 Gettings started with OpenAI and data science
Confoo 2024 Gettings started with OpenAI and data scienceSusan Ibach
 
Summary machine learning and model deployment
Summary machine learning and model deploymentSummary machine learning and model deployment
Summary machine learning and model deploymentNovita Sari
 
Design patterns
Design patternsDesign patterns
Design patternsJason Austin
 
An SEO’s Intro to Web Dev PHP
An SEO’s Intro to Web Dev PHPAn SEO’s Intro to Web Dev PHP
An SEO’s Intro to Web Dev PHPTroyfawkes
 
Oop's in php
Oop's in php Oop's in php
Oop's in php umesh patil
 
Advanced java script essentials v1
Advanced java script essentials v1Advanced java script essentials v1
Advanced java script essentials v1ASHUTOSHPATKAR1
 
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...Akhil Mittal
 
Principled And Clean Coding
Principled And Clean CodingPrincipled And Clean Coding
Principled And Clean CodingMetin Ogurlu
 
Top 100-php-interview-questions-and-answers-are-below-120816023558-phpapp01
Top 100-php-interview-questions-and-answers-are-below-120816023558-phpapp01Top 100-php-interview-questions-and-answers-are-below-120816023558-phpapp01
Top 100-php-interview-questions-and-answers-are-below-120816023558-phpapp01Tekblink Jeeten
 
New Ideas for Old Code - Greach
New Ideas for Old Code - GreachNew Ideas for Old Code - Greach
New Ideas for Old Code - GreachHamletDRC
 
You shouldneverdo
You shouldneverdoYou shouldneverdo
You shouldneverdodaniil3
 
2009-02 Oops!
2009-02 Oops!2009-02 Oops!
2009-02 Oops!terry chay
 
Creativity vs Best Practices
Creativity vs Best PracticesCreativity vs Best Practices
Creativity vs Best PracticesSupun Dissanayake
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principlesdeonpmeyer
 

Ähnlich wie Object Oriented Apologetics (20)

Oops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperOops in PHP By Nyros Developer
Oops in PHP By Nyros Developer
 
Stop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principlesStop wasting-time-by-applying-clean-code-principles
Stop wasting-time-by-applying-clean-code-principles
 
PHP Interview Questions for Freshers 2018
PHP Interview Questions for Freshers 2018PHP Interview Questions for Freshers 2018
PHP Interview Questions for Freshers 2018
 
Confoo 2024 Gettings started with OpenAI and data science
Confoo 2024 Gettings started with OpenAI and data scienceConfoo 2024 Gettings started with OpenAI and data science
Confoo 2024 Gettings started with OpenAI and data science
 
Summary machine learning and model deployment
Summary machine learning and model deploymentSummary machine learning and model deployment
Summary machine learning and model deployment
 
Design patterns
Design patternsDesign patterns
Design patterns
 
An SEO’s Intro to Web Dev PHP
An SEO’s Intro to Web Dev PHPAn SEO’s Intro to Web Dev PHP
An SEO’s Intro to Web Dev PHP
 
Performance Tuning with XHProf
Performance Tuning with XHProfPerformance Tuning with XHProf
Performance Tuning with XHProf
 
Oop's in php
Oop's in php Oop's in php
Oop's in php
 
Oops in PHP
Oops in PHPOops in PHP
Oops in PHP
 
Advanced java script essentials v1
Advanced java script essentials v1Advanced java script essentials v1
Advanced java script essentials v1
 
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile T...
 
Principled And Clean Coding
Principled And Clean CodingPrincipled And Clean Coding
Principled And Clean Coding
 
Top 100-php-interview-questions-and-answers-are-below-120816023558-phpapp01
Top 100-php-interview-questions-and-answers-are-below-120816023558-phpapp01Top 100-php-interview-questions-and-answers-are-below-120816023558-phpapp01
Top 100-php-interview-questions-and-answers-are-below-120816023558-phpapp01
 
Unit 1
Unit 1Unit 1
Unit 1
 
New Ideas for Old Code - Greach
New Ideas for Old Code - GreachNew Ideas for Old Code - Greach
New Ideas for Old Code - Greach
 
You shouldneverdo
You shouldneverdoYou shouldneverdo
You shouldneverdo
 
2009-02 Oops!
2009-02 Oops!2009-02 Oops!
2009-02 Oops!
 
Creativity vs Best Practices
Creativity vs Best PracticesCreativity vs Best Practices
Creativity vs Best Practices
 
Object Oriented Concepts and Principles
Object Oriented Concepts and PrinciplesObject Oriented Concepts and Principles
Object Oriented Concepts and Principles
 

KĂŒrzlich hochgeladen

DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
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, ...apidays
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
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...apidays
 
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 FresherRemote DBA Services
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
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 ...apidays
 
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 TerraformAndrey Devyatkin
 
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...Orbitshub
 
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 WoodJuan lago vĂĄzquez
 
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 DevelopersWSO2
 
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...apidays
 

KĂŒrzlich hochgeladen (20)

DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
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, ...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
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...
 
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
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
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 ...
 
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...
 
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
 
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
 
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...
 

Object Oriented Apologetics

  • 1. Object Oriented Apologetics Vance Lucas CodeWorks 2009 Dallas September 27, 2009
  • 2. What? 2 Not an apology Greek root - apologia (Î±Ï€ÎżÎ»ÎżÎłÎŻÎ±) - “speaking in defense” To defend the use of, and provide rational reasoning for
  • 3. 3 Who is it for? For People Who: Are “on the fence” about OOP vs procedural Are unconvinced of the usefulness of OOP Want to learn WHY they should learn about OOP
  • 4. 4 Purpose: To Get You Hooked on OOP
  • 5. 5 NO Academic or Mundane Examples
  • 6. 6 No Shapes, Cars, Fruit, or Bicycles
  • 7. <?phpclass myClass {     public function myClass(){     }     public function echoMe(){         echo 'me';     } } $mine=new myClass(); $mine->echoMe(); ?> 7 No “Hello World” Scripts
  • 9. Polymorphism I nheritance E ncapsulation 9
  • 11. Making things that are not the same look the same Relies on a defined interface Inheritance is probably the most used method The ability of type A to be used like type B Think: Interchangeable types or components 11 Polymorphism
  • 12. 12 Real World: Different Implementations
  • 13. 13 Procedural - Inline
  • 14. 14 Object-oriented – Polymorphic Interface
  • 16. Extend from a parent class “is-a” relationship Creates hierarchal relationship Get functionality for free Global changes are easier Inherits all functions and properties from parent Think: A is a B, with a few differences 16 Inheritance
  • 17. 17 Controller: Zend Framework
  • 19. 19 Warning: Keep Hierarchy Shallow
  • 21. Hide specific implementation details Reveal only methods and properties required to interact with the object Limits interdependencies between components Think: Separation of responsibilities 21 Encapsulation
  • 22. 22 Payment Interface – Exposed Methods
  • 23. 23 Planetoids (by Micah Jones)
  • 24.
  • 25. Different: velocities, sizes, shapes, rotations, color, and fragment pieces
  • 26. All the code and math calculations for movement are encapsulated behind ‘move()’
  • 27. Allows different types of objects and asteroids to be treated the same in code – “polymorphically”24 Planetoids: Code Abstraction
  • 30. Uses __get() “magic” method in PHP5 object model Also uses SPL interfaces to fire query on: count() foreach() Used as a hook to retrieve related rows on call Caches results so only 1 query is fired Can eliminate N+1 query problem by mapping 27 Lazy-Loading: OOP Only
  • 31. Classes are actually custom types Can type-hint for classes or interfaces PHP Standard types: string, int, float, boolean, array, object, null resource 28 Custom Type Creation & Type-Hinting
  • 32. Other Reasons TO Use OOP?
  • 33. Easily group related properties/data Avoid using globals or passing/returning arrays Suppress errors ala ‘undefined index’ More? Convenience (a.k.a. Laziness) 30
  • 34. Request object: Why not $_POST? Data comes from multiple sources POST/GET, XML, JSON, etc. Other functions isAjax(), isPost(), etc. Sanitizing user input Session object: Why not $_SESSION? More options for saving/storing Database, separate server, memcached, etc. 31 Request / Session Objects
  • 35. 32 Request Object: Convenience, too!
  • 37.
  • 38. Simple products, no options, stock, etc.
  • 39. Simple checkout, no user accounts
  • 44.
  • 46. 37 Simple UPS Integration
  • 47. So far it’s OK It works We finished and worked quickly 38 Status Check
  • 48. 39 Client Message I talked to my next door neighbor’s cousin’s brother’s niece yesterday, and he says all the serious online stores have regular sales. That’s something I can do too, right? - Bob
  • 50. 41 New Code for Product Sale
  • 51. We also have to add this code to the admin backend for customer invoices. And to the email reciepts Sin of code duplication Code smell 42 Thoughts
  • 52. 43 Client Message Hey,I was at the grocery store yesterday and my daughter got 2 candy bars for $1, when they were originally $0.75 each. I know that if I am able to do this, I’ll get a lot of sales and it will make me rich. I need to be able to do this. - Bob
  • 55. We also have to add this code to the admin backend and other places again. Sin of code duplication We could use procedural functions for this Where would we put them? What responsibilities do they have? 46 Thoughts
  • 56. 47 What about the future?
  • 60. Clearly, as the project grows, it will become a maintenance nightmare if we continue on the current path. We don’t want our code to be the running joke of the PHP community. 51 We need something better
  • 61. 52
  • 62. 53 Use the right tool for the right job
  • 63. 54 OOP: Right tool for this job
  • 64. Create a Cart class to store items Encapsulate the pricing logic in an Item class Single place to change the code Item is responsible for knowing it’s price (?) What does this imply? 55 Thoughts
  • 65. 56 Possible Code Changes
  • 66. Still storing cart in session, but now we can change it later when we need to scale Cart gets item price so it can check quantities Cart is responsible for knowing other items in cart Better separation of responsibility It’s not the job of the display logic to calculate the item’s price or apply discounts What about changing to FedEx? 57 Thoughts
  • 67. 58 Re-factor it into two classes
  • 68. Package is responsible for knowing it’s own dimensions and weight Quote is responsible for fetching a live rate quote from a carrier API service Always think in terms of responsibility What code is responsible for what functions? Where does it go in my app? Is this code doing too much? 59 Thoughts
  • 69. 60 Think about how an assembly line works
  • 70. OOP Myths and Misconceptions
  • 71. 62 Myth #1: OOP is about code re-use
  • 72. 63 Truth: Re-useable code is a by-product of good OO
  • 73. 64 Problem: There are lots of ways to make re-useable code that are not object-oriented nor good. It’s a bad goal.
  • 74. <Code with functions and includes – re-useable> 65 Functions are re-useable
  • 75. 66 Specific implementations are re-useable
  • 76. <UPS-Specific API Code> or <Payment Gateway-specific API Code> 67 Soft interfaces are re-useable
  • 77. You must set goals that will help direct you to your desired outcome Goals narrow attention and direct efforts to goal-relevant activities, and away from perceived undesirable and goal-irrelevant actions Re-use as a goal does not help you write good OO code. Re-use is a by-product of good OO. 68 Point: Set Good Goals
  • 78. 69 Myth #2: Objects should always be modeled after real-world objects when possible
  • 79. 70 Truth: Objects should be modeled and built based on what you need to complete your task
  • 80. 71 Problem: Real-world object models are almost never useful in code
  • 82. 73 You are modeling things that don’t exist
  • 83. 74
  • 84. 75 Myth #3: Everything should be objects
  • 85. 76 Truth: Make objects for only what you need to. Most of the time this is data. Don’t over-complicate your code when it’s not necessary.
  • 86. 77 Problem: Not everything your code does can be easily represented with an object
  • 87. 78 Application Flow MVC diagram for XEROX PARC 1978-79
  • 88. 79
  • 89. Programming PHP for over 10 years Web: http://www.vancelucas.com Twitter: @vlucas Email: vance@vancelucas.com GitHub: http://github.com/vlucas Photo Set: http://www.flickr.com/photos/30728345@N05/galleries/72157622318592067/ 80 Vance Lucas