DTS s03e02 Handling the code

918 views
852 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
918
On SlideShare
0
From Embeds
0
Number of Embeds
201
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

DTS s03e02 Handling the code

  1. 1. Handle the code Development Techniques Seminar s03e02
  2. 2. Outline Scoping concept Components and Packages Modules and Classes Examples
  3. 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. 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. 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. 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. 7. 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
  8. 8. 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')
  9. 9. 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.
  10. 10. 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
  11. 11. 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();
  12. 12. 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
  13. 13. Questions? Prem Gurbani prem@tuenti.com

×