SlideShare une entreprise Scribd logo
1  sur  14
Télécharger pour lire hors ligne
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

Contenu connexe

En vedette

Red Label Films
Red Label FilmsRed Label Films
Red Label FilmsRed Label
 
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
 

En vedette (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
 

Similaire à 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
 
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
 

Similaire à 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#
 

Plus de 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
 

Plus de 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
 

Dernier

Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxfnnc6jmgwh
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsRavi Sanghani
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 

Dernier (20)

Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptxGenerative AI - Gitex v1Generative AI - Gitex v1.pptx
Generative AI - Gitex v1Generative AI - Gitex v1.pptx
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Potential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and InsightsPotential of AI (Generative AI) in Business: Learnings and Insights
Potential of AI (Generative AI) in Business: Learnings and Insights
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 

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