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

Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
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
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 

Kürzlich hochgeladen (20)

Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
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
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 

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