CSC/ECE 517 Fall 2011/ch1 1g jn: Difference between revisions
Line 113: | Line 113: | ||
The above code compiles without any problems and no exceptions are thrown during runtime. | The above code compiles without any problems and no exceptions are thrown during runtime. | ||
Languages like javascript allow weak typing. | Languages like javascript allow weak typing. | ||
[[File:Typing.jpg]] | |||
=='''Is scripting essentially synonymous with dynamic typing?'''== | =='''Is scripting essentially synonymous with dynamic typing?'''== |
Revision as of 21:16, 8 September 2011
Comparing Object-Oriented and Scripting Languages
Scripting Languages
Scripting languages have gained popularity mainly because they make programming tasks easier.They control the operation of the program by giving a sequence of work to do in a batch. They are different form the core code of the application and can often be modified by the end user. Earlier Shell scripts were popular. As programmable features of shells became more powerful they began to represent interpreted languages like Lisp and the name “Scripting Languages” came into existence.
Now scripting languages are used for a variety of purposes. They are used for Batch Jobs,in web browsers , GUI Scripts , Text processing , Application specific languages , General purpose Dynamic languages etc. Few key features of Scripting Languages include facility of changing code dynamically at runtime, using interpreters instead of compilers, easy integration with other systems and automatic type conversion. They interface with underlying operating system in order to run other applications and to communicate with them.
Commonly used Scripting Languages are :-
Overview:-Object Oriented and Scripting Languages
Object-Oriented Languages |
---|
1.OOP languages focus on abstract relationships and hierarchy of related functionality. |
2.Any problem is handled as a collection of real-world objects that provide services to solve that problem. |
3.It bounds data closely to the functions that operate on it and protects it from accidental modification from outside functions. |
4.Encapsulation and polymorphism emphasizes on re-usability by keeping the implementation flexible without having the need to change a great deal of code. |
5.Object-orientation is a sturdy compromise between performance and organization. Programmer needs to know how to effectively represent real-life scenarios into objects to fill the needs of market. |
Scripting Languages |
1.High-level software development language,primarily used for the purpose of adding dynamic and interactive components to web pages. |
2.They are not stand-alone programming languages with support of large system development. However, they can be integrated with existing technologies to provide required functionality. |
3.Portable and Platform independent because they can be created and edited easily on any text editors. |
4.Easy to learn and require minimum programming knowledge or experience. |
5.It must be efficient when calling system resources such as file operations, inter-process communications, and process control. |
Dynamic Typing in Scripting Languages
Dynamically Typed Languages
Dynamic typing do not enforce specification of type and type safety at compile time.The type check is deferred till run time. i.e explicit declaration of variable type is not needed in the program. The type of the variables can change during the course of program and it is resolved at run time.In dynamic typing the object method look ups are performed during the run time.This is also called as late binding. This allows objects to change behavior at run time.
Static typing and Dynamic typing:
Static typing check type and type safety at compile time. These languages require explicit type declaration before using the variable.
consider the following tcl command(using dynamic typing)
button .b -text Hello! -font {Times 16} -command {puts hello}
The above command creates a button that displays a message when user clicks it. It contains various types of thing in that command like Font name Time , text hello. Tcl represents all these things as strings. Their exact type is determined at runtime and the appropriate font and size and text are used.
The same example in Microsoft Foundation Classes (MFC)(uses static typing) requires about 25 lines of code in three procedures. Just setting the font requires several lines of code in MFC:
CFont *fontPtr = new CFont(); fontPtr->CreateFont(16, 0, 0, 0, 700, 0, 0, 0, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH|FF_DONTCARE, “Times New Roman”); buttonPtr->SetFont(fontPtr);
This is due to static typing. To set the font of a button we need to invoke SetFont () method. It requires the a pointer of type CFont Object. This requires declaration and initialization if CFont Object. So Dynamic Typing in languages reduces the developer time and results in fewer lines of code.
The disadvantage associated with dynamic typing is that all the possible errors are detected only at runtime.
Dynamic typing is different from weak typing. A dynamically typed language can be either strong typed or Weak typed.
Strong typing : Strongly typed dynamic languages enforces certain restrictions on operations involving different types. For example ,
i := 3 s := "4" x := i + s
The types of i , s ,x are not enforced at compile time but during run time. This compiles correctly but gives run time error as addition if “integer” and a “string” is not allowed. Ruby is a strongly typed dynamic language.
Weak Typing: In weakly typed dynamic languages the operations between incompatible types is allowed. For example,
i := 3 s := "4" x := i + s
The above code compiles without any problems and no exceptions are thrown during runtime. Languages like javascript allow weak typing.
Is scripting essentially synonymous with dynamic typing?
The Initial history of dynamic languages consisted of individual languages like Lisp and Small talk. Now it includes a group of languages such as scripting languages including Ruby , Perl , Python - forging a common direction. Although majority of dynamically typed languages contain scripting language It doesn’t mean that every dynamic type language is a scripting language.
Lisp was the first language to use dynamic typing. It had minimal Syntax. Its derivative scheme is also a very popular dynamic typing language. Small talk’s syntax is small and uncomplicated. All these languages ease of use is attributed to dynamic typing. Many other languages like scheme , Objective C are dynamically typed languages. Many Text Processing languages like SNOBOL4 also use dynamic typing.
Why scripting languages use dynamic typing?
Scripting languages always assume the presence of components and they are used to extend the features of component or to glue components together. Dynamic typing in scripting languages make easier to bind the components together. Variables can be anything and hold any type it is easy to move these around the components. Dynamic typing makes doesn’t impose any rule on types to its easy to pass values to components. for example to connect two programs we can pass output of one program as input to another.
Advantages of scripting languages that are not object oriented
- Procedural approach is useful in applications where we can’t have certain functions and data together. For example we need not have math functions in an object. Just a function would do our work.
- OOP need not be used in applications which concentrate on flow rather than data.
Earlier many scripting languages like perl , PHP , Rexx etc didn’t support OO but support for it was added in later versions. Perl version 5 is object oriented , Rexx with object oriented features is Object Rexx. The latest versions added OO as an additional feature to the existing language.
PHP3 supports OO features. But it can be used both for OO and Procedural Programming. Developers use it accordingly based on the need of the application.
There are few more languages that don’t support OO. Few of them are lua , vbscript , SCSH(Scheme as scripting language).
lua: The advantages of Lua is that it's very small, very fast and very flexible. Since everything revolves around the idea of tables, you can construct pretty much any class-based system you want; you can have inheritance (like most languages) or interfaces (like Google Go) or anything you want really.
VBScript: It is modeled over visual Basic. Procedure is main construct in vbscript. Procedure divides program into smaller modules.
SCSH: SCSH is a scripting language based on scheme. Here the regular expressions are not string based. Instead it is a domain specific language embedded in scheme shell. It has a syntax similar to lisp.
Advantages that object orientation brings to a scripting languages
Advantages of object orientation on scripting languages can be explained on the basis of core O-O concepts:-
Code Reusablity
To allow developers to pay more attention on program functionality, code reuse is a very effective technique that saves time and effort by significantly reducing the redundant code. Administrators and script developers can share functionality by breaking their programs into reusable units like modules functions etc. and using them as other program’s components. For example:-
- Python supports program reusability by implementing modules and packages in standard Python Library.
- Windows PowerShell offer Modules that enable production-ready scripting solutions. These redistributable components have added benefit of allowing users to repackage and abstract multiple components to create custom solutions.
- CPAN (Comprehensive Perl Archive Network) is an online repository of Perl modules which are collection of files of Perl code that can be reused from program to program.
Inheritance
One major advantage of Object Inheritance, apart from its usefulness in defining and abstracting functionality, is it permits the implementation of additional functionality in similar objects without the need to re-implement all of the shared functionality. Object oriented languages implement class-based inheritance and thus have deep hierarchies. Whereas most of the scripting languages use prototypical inheritance where classes and objects have shallow hierarchies and are more expressive. Since scripting languages are loosely-typed languages, object references don’t need casting. For example :-
- JavaScript and PowerShell support inheritance through prototyping as well as properties and method.
- In JavaScript,Class objects are soft and thus new members can be added by simple assignment. All object types have a prototype property that can be both extended and inherited. Prototyping also has its disadvantages like object creation can much slower than it can be in a more conventional class/object dichotomy scheme.
- Inheritance in Python has close resemblance with classical inheritance as it allows multiple base classes, a derived class can override any methods of its base class or classes, and a method can call the method of a base class with the same name.
- @ISA variable defined for each package in PERL governs inheritance.If a call to a method on an object or class returns null in that object’s package, Perl looks to @ISA for other packages to go looking through in search of the missing method.
The main reason behind inheritance exhibiting a different behavior in scripting languages is, it needs to be as dynamic as the environment it is in.
Encapsulation
Encapsulation is a useful technique in programming which allows you to separate an abstraction's implementation from its interface, thus allowing enhancements without affecting the interface.
- JavaScript implements encapsulation using Closures which can be used to group interrelated and dependent code. For example, A Closure allows the buffer array to be associated with the function that is dependent upon it and simultaneously keep the property name to which the buffer array as assigned out of the global namespace and free of the risk of name conflicts.
- Python supports encapsulation with naming conventions; however they are not strictly implemented. For Example, it differentiates between identifiers starting with a single versus double underscore (akin to protected versus private).
function Classroom(studname, std) { this.toString = function() { return 'Student Name: ' + studname + ', Standard: ' + std; };}
Polymorphism
Polymorphic variable in scripting languages contributing significantly in memory conservation as a single variable can be used to store multiple data types (integers, strings, etc.),rather than declaring variable for each data format to be used.
- JavaScript implement it using the prototype operator.
- Python’s dynamic typing in an object-oriented context helps in accomplishing generic programming and polymorphism.
Few polymorphism examples:-
In Python:-
myString = 'Hello, world!' myList = [0, 'one', 1, 'two', 3, 'five', 8] print myString[:5] # prints Hello print myList[:5] # prints [0, 'one', 1, 'two', 3] print 'e' in myString # prints True print 5 in myList # prints False
In Perl:-
package Animal; sub new { my ($class, $name) = @_; bless {name => $name}, $class; } package Cat; @ISA = "Animal"; sub talk {"Meow"} package Dog; @ISA = "Animal"; sub talk {"Woof! Woof!"} package main; my @animals = ( Cat->new("Missy"), Cat->new("Mr. Mistoffelees"), Dog->new("Lassie"), ); for my $animal (@animals) { print $animal->{name} . ": " . $animal->talk . "\n"; } # prints the following: # Missy: Meow # Mr. Mistoffelees: Meow # Lassie: Woof! Woof!
For more examples please refer following wiki page :- Polymorphism in Object Oriented Programming
Abstraction and Real-World Modeling
Object-oriented system tends to model the real world in a more complete manner than do traditional methods. Objects are organized by classes and focus is more on their behavior than data and processing. Real-world constructs can be best described and represented using Abstract data types. Scripting languages like JavaScript use functions as object descriptors.
Advantages that scripting capability bring to an object-oriented languages
Scripting functionality in addition with Object-orientation creates a favorable environment for Web application Development. Following are few advantages that scripting capability adds :-
Gluing
Scripting Languages assume existence of a set of powerful components and that can be connected to implement web features or extend features of existing components. Tcl and Visual Basic offer collection of UI controls whereas Shell Scripts are used to assemble filter programs into pipelines. They are not intended for writing application from scratch.
Rapid and Incremental Application Development
Scripting languages provide rapid turnaround during development by eliminating compile times and allowing interpreters to program application at runtime. Browser can parse web page by translating the HTML for the page into a script using a few regular expression substitutions and then executing that script to render page on browser screen.
Dynamic Typing
Dynamic typed programming does not require the explicit declaration of the variables before they’re used. For example num=5, num can be assigned value before being initialized.In order to implement dynamic typing, Names are bound to objects at execution time by means of assignment statements, and it is possible to bind a name to objects of different types during the execution of the program. As a result of dynamic typing, programs are not limited by the expressiveness of the type system of the language — e.g. heterogeneous data structures w/o explicit tagging. It also opens door for open recursion, late binding, dynamic scope, and duck typing.
Embedded within HTML
Scripting Languages are easy to use because they are contained directly in an HTML page and created in plain ASCII text, thus simple to implement and change. They have their application in the field of gaming as they provide good interactive experience as well as perform basic interactive functions required by website such as data validation.
References
- John Ousterhout. Scripting: Higher-level programming for the 21st century. IEEE Computer, 31(3):23.30, March 1998.
- Dynamically typed languages, Laurence Tratt, Advances in Computing, vol. 77, pages 149-184, July 2009
- http://www.perlhome.com/perloops/
- http://blob.perl.org/books/beginning-perl/3145_Chap11.pdf
- http://www.oreillynet.com/pub/a/oreilly/perl/news/importance_0498.html
- http://www.artima.com/weblogs/viewpost.jsp?thread=7590
- http://www.sitepoint.com/typing-versus-dynamic-typing/
- http://www.htmlgoodies.com/beyond/javascript/article.php/3470971/Java-vs-JavaScript.htm
- http://www.dba-oracle.com/t_object_oriented_approach.htm
- http://www.sqa.org.uk/e-learning/ClientSide01CD/page_22.htm
- http://www.cprogramming.com/langs.html
- http://www.codeproject.com/KB/scripting/object_oriented_jscript.aspx?display=Print#pppmembers
- http://pythonconquerstheuniverse.wordpress.com/category/java-and-python/
- http://www.debugmagazine.com/freelancing-tips-tricks/procedural-vs-object-oriented-php-programming