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

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
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
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 

Dernier (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
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
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 

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