Programming style - duck type
Upcoming SlideShare
Loading in...5
×
 

Programming style - duck type

on

  • 521 views

 

Statistics

Views

Total Views
521
Views on SlideShare
521
Embed Views
0

Actions

Likes
0
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Programming style - duck type Programming style - duck type Presentation Transcript

  • What is -
    duck typing is a style of dynamic typing in which
    • an object's current set of methods 
    and
    • propertiesdetermines the valid semantics,
    rather than its inheritance from -
    • a particular class or implementation of a
    specific interface.
    DUCK TYPING
    PROGRAMMING STYLE
    Presented by
    Abdun Nur Tomal
  • Define -
    may be phrased as follows:
    "When I see a bird that walks like a duckand swims like a duck and quacks like a duck, I call that bird a duck.“
    In duck typing, one is concerned with just those aspects of an object that are
    • used,
    • rather than with the type of the object itself.
     
    DUCK TYPING
    PROGRAMMING STYLE
  • For example :-
    • in a non-duck-typed language,
    one can create a function that takes an object of type Duck and calls that object's -walk and -quack methods.
    • In a duck-typed language,
    >the equivalent function would take an object of any type and
    >call that object's walk and quack methods.
    >If the object does not have the methods that are called then the function signals a run-time error.
    >It is this action of any object having the correct walk and quack methods being accepted by the function that evokes
    the quotation and hence the name of this form of typing.
    DUCK TYPING
    PROGRAMMING STYLE
  • example :-
    • Duck typing is aided by habitually not testing for the type of arguments in method and function bodies,
    • relying on documentation, clear code, and testing to ensure correct use.
    • pseudo-code for a duck typed language:
    function calculate(a, b, c) => return (a+b)*c
    example1 = calculate (1, 2, 3)
    example2 = calculate ([1, 2, 3], [4, 5, 6], 2)
    example3 = calculate ('apples ', 'and oranges, ', 3)
    printto_string example1
    printto_string example2
    printto_string example3
    • the result of the code would be:
    9
    [1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6]
    apples and oranges, apples and oranges, apples and oranges
    DUCK TYPING
    PROGRAMMING STYLE
    • Thus, duck typing allows polymorphismwithout inheritance.
    • The only requirement thatfunction calculate needs in its variables is having the "+" and the "*" methods.
    • The duck test can be seen in the following example:
    As far as the function in_the_forest is concerned, the Person object is a duck:
    class Duck:
    def quack(self):
    print("Quaaaaaack!")
    def feathers(self):
    print("The duck has white and gray feathers.")
    class Person:
    def quack(self):
    print("The person imitates a duck.")
    def feathers(self):
    print("The person takes a feather from the ground and shows it.")
    def name(self):
    print("John Smith")
    defin_the_forest(duck):
    duck.quack()
    duck.feathers()
    def game():
    donald = Duck()
    john = Person()
    in_the_forest(donald)
    in_the_forest(john)
    game()
    DUCK TYPING
    PROGRAMMING STYLE
  • This way software can be developed by extending partially working duck typed code.
    • In JavaScript
    var Duck = function () {};
    Duck.prototype = {
    quack: function (){ alert("Quaaaaaack!"); },
    feathers: function () { alert("The duck has white and gray feathers."); }
    };
    var Person = function () {};
    Person.prototype = {
    quack: function () { alert("The person imitates a duck."); },
    feathers: function () { alert("The person takes a feather from the ground and shows it.");},
    name: function () { alert("John Smith"); }
    };
    varin_the_forest = function (duck)
    {
    duck.quack();
    duck.feathers();
    };
    var game = function ()
    {
    vardonald = new Duck();
    var john = new Person();
    in_the_forest(donald)
    in_the_forest(john)
    };
    game();
    DUCK TYPING
    PROGRAMMING STYLE
  • Resources –
    Wikipedia
    http://en.wikipedia.org/wiki/Duck_typing#Concept_examples
    Dr. Dobb’s
    http://drdobbs.com/cpp/184401971
    http://drdobbs.com/open-source
    Big Dingus
    http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/
    Apache Commons
    http://commons.apache.org/proxy/
    DUCK TYPING
    PROGRAMMING STYLE
  • Resources –
    Wikipedia
    http://en.wikipedia.org/wiki/Duck_typing#Concept_examples
    Dr. Dobb’s
    http://drdobbs.com/cpp/184401971
    http://drdobbs.com/open-source
    Big Dingus
    http://bigdingus.com/2007/12/08/just-what-is-this-javascript-object-you-handed-me/
    Apache Commons
    http://commons.apache.org/proxy/
    DUCK TYPING
    PROGRAMMING STYLE