SlideShare ist ein Scribd-Unternehmen logo
1 von 14
Downloaden Sie, um offline zu lesen
Handle the code


     Development Techniques Seminar
                             s03e02
Outline


  Scoping concept

  Components and Packages

  Modules and Classes

  Examples
Scoping Concept (i)

  Scope is an enclosing context where variables and functions are
  associated


  Defines the visibility and accessibility of code


  If you are not defining your scope properly you risk that somebody
  modifies/accesses attributes or methods without your code knowing
  about it


  Always provide lowest visibility possible to ensure that responsibilities
  are correctly assigned
Scoping Concept (ii)


  By assigning responsibilities, the class or method is responsible for
  providing an interface to modify / provide access to attributes &
  methods
  Allows you to design by contract where given an input you will get an
  output. The encapsulated code will execute, as part of the contract, all
  pre-conditions, perform the action, all post-conditions and produce the
  output in the agreed format
Organizing your Code (i)

  An individual component is a software package or a module that encapsulates a set of
  related functions (or data). It provides an interface so others can interact with it,
  specifying the services that can be used.

  A client doesn't need to know the inner working of another component (i.e.
  encapsulation principle)

  In practice, a component maybe formed of an object or collection of objects (i.e.
  one or more classes).

  The above makes them substitutable so they can easily be replaced by another
  one (upgraded version, etc)

  They provide reusability so they need to be well tested and thoroughly document
  to make the easy to use

  Effectively you can glue together several components to provide a functionality
Organizing your Code (ii)

  A module is just producing a piece of software more scalable and maintainable
  by having separated concerns and maximizing functional decoupling from one
  module to another.


  A class is a base construct used in object oriented programming. It may be
  considered as a cohesive package which has an interface and a structure (out of
  the scope of this talk).


  A package is intended to be an archive format to be installed or be a self-
  sufficient module. It typically contains meta-information such as its description,
  version, dependencies, and documentation.
Scoping Example (i)

class MyClass {

  private $amSafe;
  protected $amNotThatSafe;
  public $ohNoSomebodyHelpMe;

  ...
}
$a = new MyClass();
                                Variable can only be
$a->amSafe = 'no way!';         modified if MyClass has
$a->setAmSafe('nice');          authorized it by providing
                                a setter
Scoping Example (ii)

Here you have no control on who and when changes this
$a->ohNoSomebodyHelpMe = 'exposed';

A new developer comes in and creates:
class AmEvil extends MyClass {
  public function public setAmNotThatSafe($val) {
    ...
  }
  ...
}

This setter is now providing unrestricted public access to it:
$amEvil->setAmNotThatSafe('evil')
Scoping Example (iii) - in JavaScript

  Not scoping in JavaScript is scary
     Non-scoped variables became assigned to 'window'
     Hoisting principles apply
     References to DOM elements within closures will leak
     not scoped

  Typo variables --> JavaScript will never complain, it will
  just create a new global variable in 'window' for you :)
      If you get in the habit of declaring variables within a
      scope, your IDE will be on your side
      NetBeans highlights in green if a variable has not been
      declared within your scope.
Globals Example

Global variables produce confusing code and they can be
overriden mistakenly
$a = 1;
$b = 1;

function myFunc() {
  global $a;                       Use a singleton class
  $a = 2; // or $GLOBALS['a']
  $b = 2;
                                   encapsulating variables
}                                  instead

myFunc();
echo $a; // 2
echo $b; // 1
Namespace Example
   Avoid clashes with existing classes and methods. This is
   vital especially when using 3rd party components

   Allows structuring the code into components by
   packaging them in a hierarchy.
                                              Note that it is also
    PHP example:                              important for the first
                                              element in path to be
namespace TuentiDisplay;                     the company name.
class showSomething() { ... }                 This way we eliminate
                                              the risks of clashing
                                              modules
   Using it:
 echo TuentiDisplayshowSomething();

   In this way, it won't clash with another 3rd party:
ThirdPartyDisplayshowSomething();
In Summary...

  Allows you to write safer and less error prone code

  Your code will be more reusable and scalable

  Avoid mysterious bugs and variables disappearing just to find out that
  something was actually being overwritten.

  More structured code makes it easier for new comers to embrace the
  code

  Classes that have attributes and methods with just the minimum
  visibility they need means simpler and easier to understand interfaces
Questions?




                Prem Gurbani
             prem@tuenti.com

Weitere ähnliche Inhalte

Andere mochten auch

Red Label Films
Red Label FilmsRed Label Films
Red Label FilmsRed Label
 
Vocabulary Night
Vocabulary NightVocabulary Night
Vocabulary Nightguest0026992
 
Shannon Plummer Folio I
Shannon Plummer Folio IShannon Plummer Folio I
Shannon Plummer Folio Ishannonplummer
 
Tuenti Tech Teams. Frontend, Backend, Systems and more, working together
Tuenti Tech Teams. Frontend, Backend, Systems and more, working togetherTuenti Tech Teams. Frontend, Backend, Systems and more, working together
Tuenti Tech Teams. Frontend, Backend, Systems and more, working togetherTuenti
 
Shannon Plummer Folio II
Shannon Plummer Folio IIShannon Plummer Folio II
Shannon Plummer Folio IIshannonplummer
 
2009 Annual SWE Team Tech Presentation
2009 Annual SWE Team Tech Presentation2009 Annual SWE Team Tech Presentation
2009 Annual SWE Team Tech Presentationwtushaus
 
Building a Tech Team for Non-Techie Founders
Building a Tech Team for Non-Techie FoundersBuilding a Tech Team for Non-Techie Founders
Building a Tech Team for Non-Techie FoundersJohanna Brewer
 
How I Grew in Tech - A Startup Story in 3 Acts
How I Grew in Tech - A Startup Story in 3 ActsHow I Grew in Tech - A Startup Story in 3 Acts
How I Grew in Tech - A Startup Story in 3 ActsElisha Tan
 

Andere mochten auch (8)

Red Label Films
Red Label FilmsRed Label Films
Red Label Films
 
Vocabulary Night
Vocabulary NightVocabulary Night
Vocabulary Night
 
Shannon Plummer Folio I
Shannon Plummer Folio IShannon Plummer Folio I
Shannon Plummer Folio I
 
Tuenti Tech Teams. Frontend, Backend, Systems and more, working together
Tuenti Tech Teams. Frontend, Backend, Systems and more, working togetherTuenti Tech Teams. Frontend, Backend, Systems and more, working together
Tuenti Tech Teams. Frontend, Backend, Systems and more, working together
 
Shannon Plummer Folio II
Shannon Plummer Folio IIShannon Plummer Folio II
Shannon Plummer Folio II
 
2009 Annual SWE Team Tech Presentation
2009 Annual SWE Team Tech Presentation2009 Annual SWE Team Tech Presentation
2009 Annual SWE Team Tech Presentation
 
Building a Tech Team for Non-Techie Founders
Building a Tech Team for Non-Techie FoundersBuilding a Tech Team for Non-Techie Founders
Building a Tech Team for Non-Techie Founders
 
How I Grew in Tech - A Startup Story in 3 Acts
How I Grew in Tech - A Startup Story in 3 ActsHow I Grew in Tech - A Startup Story in 3 Acts
How I Grew in Tech - A Startup Story in 3 Acts
 

Ă„hnlich wie DTS s03e02 Handling the code

4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPTAjay Chimmani
 
Software Patterns
Software PatternsSoftware Patterns
Software Patternsbonej010
 
Software development effort reduction with Co-op
Software development effort reduction with Co-opSoftware development effort reduction with Co-op
Software development effort reduction with Co-oplbergmans
 
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxdanhaley45372
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)Jay Patel
 
1. Mini seminar intro
1. Mini seminar intro1. Mini seminar intro
1. Mini seminar introLeonid Maslov
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesDurgesh Singh
 
Application package
Application packageApplication package
Application packageJAYAARC
 
Design patterns
Design patternsDesign patterns
Design patternsBinu Bhasuran
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer ProgrammingInocentshuja Ahmad
 
Generic Repository Pattern in MVC3 Application with Entity Framework
Generic Repository Pattern in MVC3 Application with Entity FrameworkGeneric Repository Pattern in MVC3 Application with Entity Framework
Generic Repository Pattern in MVC3 Application with Entity FrameworkAkhil Mittal
 
Dot Net Fundamentals
Dot Net FundamentalsDot Net Fundamentals
Dot Net FundamentalsLiquidHub
 
Polymorphism and interface in vb.net
Polymorphism and interface in vb.netPolymorphism and interface in vb.net
Polymorphism and interface in vb.netKarthigaGunasekaran1
 
Intro Java Rev010
Intro Java Rev010Intro Java Rev010
Intro Java Rev010Rich Helton
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++Amresh Raj
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#SyedUmairAli9
 

Ă„hnlich wie DTS s03e02 Handling the code (20)

4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
 
Software Patterns
Software PatternsSoftware Patterns
Software Patterns
 
Software development effort reduction with Co-op
Software development effort reduction with Co-opSoftware development effort reduction with Co-op
Software development effort reduction with Co-op
 
Mca 504 dotnet_unit3
Mca 504 dotnet_unit3Mca 504 dotnet_unit3
Mca 504 dotnet_unit3
 
Clean code
Clean codeClean code
Clean code
 
Patterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docxPatterns (contd)Software Development ProcessDesign patte.docx
Patterns (contd)Software Development ProcessDesign patte.docx
 
I assignmnt(oops)
I assignmnt(oops)I assignmnt(oops)
I assignmnt(oops)
 
1. Mini seminar intro
1. Mini seminar intro1. Mini seminar intro
1. Mini seminar intro
 
Object-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modulesObject-oriented programming (OOP) with Complete understanding modules
Object-oriented programming (OOP) with Complete understanding modules
 
Application package
Application packageApplication package
Application package
 
Topic 1 PBO
Topic 1 PBOTopic 1 PBO
Topic 1 PBO
 
Design Patterns
Design PatternsDesign Patterns
Design Patterns
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Question and answer Programming
Question and answer ProgrammingQuestion and answer Programming
Question and answer Programming
 
Generic Repository Pattern in MVC3 Application with Entity Framework
Generic Repository Pattern in MVC3 Application with Entity FrameworkGeneric Repository Pattern in MVC3 Application with Entity Framework
Generic Repository Pattern in MVC3 Application with Entity Framework
 
Dot Net Fundamentals
Dot Net FundamentalsDot Net Fundamentals
Dot Net Fundamentals
 
Polymorphism and interface in vb.net
Polymorphism and interface in vb.netPolymorphism and interface in vb.net
Polymorphism and interface in vb.net
 
Intro Java Rev010
Intro Java Rev010Intro Java Rev010
Intro Java Rev010
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#
 

Mehr von Tuenti

Tuenti Release Workflow v1.1
Tuenti Release Workflow v1.1Tuenti Release Workflow v1.1
Tuenti Release Workflow v1.1Tuenti
 
Tu: Telco 2.0 at FICOD 2011
Tu: Telco 2.0 at FICOD 2011Tu: Telco 2.0 at FICOD 2011
Tu: Telco 2.0 at FICOD 2011Tuenti
 
Tuenti - de la idea a la web
Tuenti -  de la idea a la webTuenti -  de la idea a la web
Tuenti - de la idea a la webTuenti
 
Tuenti Mobile Development
Tuenti Mobile DevelopmentTuenti Mobile Development
Tuenti Mobile DevelopmentTuenti
 
Tuenti: Web Application Security
Tuenti: Web Application SecurityTuenti: Web Application Security
Tuenti: Web Application SecurityTuenti
 
Tuenti Release Workflow
Tuenti Release WorkflowTuenti Release Workflow
Tuenti Release WorkflowTuenti
 
Tuenti release process
Tuenti release processTuenti release process
Tuenti release processTuenti
 
Tuenti - tu entidad
Tuenti -  tu entidadTuenti -  tu entidad
Tuenti - tu entidadTuenti
 
DTS s03e04 Typing
DTS s03e04 TypingDTS s03e04 Typing
DTS s03e04 TypingTuenti
 
AJAX for Scalability
AJAX for ScalabilityAJAX for Scalability
AJAX for ScalabilityTuenti
 

Mehr von Tuenti (10)

Tuenti Release Workflow v1.1
Tuenti Release Workflow v1.1Tuenti Release Workflow v1.1
Tuenti Release Workflow v1.1
 
Tu: Telco 2.0 at FICOD 2011
Tu: Telco 2.0 at FICOD 2011Tu: Telco 2.0 at FICOD 2011
Tu: Telco 2.0 at FICOD 2011
 
Tuenti - de la idea a la web
Tuenti -  de la idea a la webTuenti -  de la idea a la web
Tuenti - de la idea a la web
 
Tuenti Mobile Development
Tuenti Mobile DevelopmentTuenti Mobile Development
Tuenti Mobile Development
 
Tuenti: Web Application Security
Tuenti: Web Application SecurityTuenti: Web Application Security
Tuenti: Web Application Security
 
Tuenti Release Workflow
Tuenti Release WorkflowTuenti Release Workflow
Tuenti Release Workflow
 
Tuenti release process
Tuenti release processTuenti release process
Tuenti release process
 
Tuenti - tu entidad
Tuenti -  tu entidadTuenti -  tu entidad
Tuenti - tu entidad
 
DTS s03e04 Typing
DTS s03e04 TypingDTS s03e04 Typing
DTS s03e04 Typing
 
AJAX for Scalability
AJAX for ScalabilityAJAX for Scalability
AJAX for Scalability
 

KĂĽrzlich hochgeladen

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
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
 
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
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
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
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
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 ModelDeepika Singh
 
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 2024Victor Rentea
 
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
 
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 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
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
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
 
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
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
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
 

KĂĽrzlich hochgeladen (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
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
 
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
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
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...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
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
 
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
 
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 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...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
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 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, ...
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
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 ...
 

DTS s03e02 Handling the code

  • 1. Handle the code Development Techniques Seminar s03e02
  • 2. Outline Scoping concept Components and Packages Modules and Classes Examples
  • 3. Scoping Concept (i) Scope is an enclosing context where variables and functions are associated Defines the visibility and accessibility of code If you are not defining your scope properly you risk that somebody modifies/accesses attributes or methods without your code knowing about it Always provide lowest visibility possible to ensure that responsibilities are correctly assigned
  • 4. Scoping Concept (ii) By assigning responsibilities, the class or method is responsible for providing an interface to modify / provide access to attributes & methods Allows you to design by contract where given an input you will get an output. The encapsulated code will execute, as part of the contract, all pre-conditions, perform the action, all post-conditions and produce the output in the agreed format
  • 5. Organizing your Code (i) An individual component is a software package or a module that encapsulates a set of related functions (or data). It provides an interface so others can interact with it, specifying the services that can be used. A client doesn't need to know the inner working of another component (i.e. encapsulation principle) In practice, a component maybe formed of an object or collection of objects (i.e. one or more classes). The above makes them substitutable so they can easily be replaced by another one (upgraded version, etc) They provide reusability so they need to be well tested and thoroughly document to make the easy to use Effectively you can glue together several components to provide a functionality
  • 6. Organizing your Code (ii) A module is just producing a piece of software more scalable and maintainable by having separated concerns and maximizing functional decoupling from one module to another. A class is a base construct used in object oriented programming. It may be considered as a cohesive package which has an interface and a structure (out of the scope of this talk). A package is intended to be an archive format to be installed or be a self- sufficient module. It typically contains meta-information such as its description, version, dependencies, and documentation.
  • 7.
  • 8. Scoping Example (i) class MyClass { private $amSafe; protected $amNotThatSafe; public $ohNoSomebodyHelpMe; ... } $a = new MyClass(); Variable can only be $a->amSafe = 'no way!'; modified if MyClass has $a->setAmSafe('nice'); authorized it by providing a setter
  • 9. Scoping Example (ii) Here you have no control on who and when changes this $a->ohNoSomebodyHelpMe = 'exposed'; A new developer comes in and creates: class AmEvil extends MyClass { public function public setAmNotThatSafe($val) { ... } ... } This setter is now providing unrestricted public access to it: $amEvil->setAmNotThatSafe('evil')
  • 10. Scoping Example (iii) - in JavaScript Not scoping in JavaScript is scary Non-scoped variables became assigned to 'window' Hoisting principles apply References to DOM elements within closures will leak not scoped Typo variables --> JavaScript will never complain, it will just create a new global variable in 'window' for you :) If you get in the habit of declaring variables within a scope, your IDE will be on your side NetBeans highlights in green if a variable has not been declared within your scope.
  • 11. Globals Example Global variables produce confusing code and they can be overriden mistakenly $a = 1; $b = 1; function myFunc() { global $a; Use a singleton class $a = 2; // or $GLOBALS['a'] $b = 2; encapsulating variables } instead myFunc(); echo $a; // 2 echo $b; // 1
  • 12. Namespace Example Avoid clashes with existing classes and methods. This is vital especially when using 3rd party components Allows structuring the code into components by packaging them in a hierarchy. Note that it is also PHP example: important for the first element in path to be namespace TuentiDisplay; the company name. class showSomething() { ... } This way we eliminate the risks of clashing modules Using it: echo TuentiDisplayshowSomething(); In this way, it won't clash with another 3rd party: ThirdPartyDisplayshowSomething();
  • 13. In Summary... Allows you to write safer and less error prone code Your code will be more reusable and scalable Avoid mysterious bugs and variables disappearing just to find out that something was actually being overwritten. More structured code makes it easier for new comers to embrace the code Classes that have attributes and methods with just the minimum visibility they need means simpler and easier to understand interfaces
  • 14. Questions? Prem Gurbani prem@tuenti.com