CSC/ECE 517 Fall 2011/ch1 1e an: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
Line 138: Line 138:


a) Locality:- The major advantage of block structure is locality. This makes it possible to restrict the existence of an object and its description to the environment where it has meaning.
a) Locality:- The major advantage of block structure is locality. This makes it possible to restrict the existence of an object and its description to the environment where it has meaning.
 
b) Scope Rules:- There are following aspects of scope rules for names declared within an object:  
b) Scope Rules:- There are following aspects of scope rules for names declared within
                an object:  
                 1. They only exist when the object exist. This is a consequence of locality.
                 1. They only exist when the object exist. This is a consequence of locality.
                 2. Access to global names and re-declaration of names.
                 2. Access to global names and re-declaration of names.
                 3. Access to names within a block from “outside” the block may be restricted. Like in Simula language.  
                 3. Access to names within a block from “outside” the block may be restricted. Like in Simula language.
 
 


=== Conclusion ===
=== Conclusion ===

Revision as of 14:58, 6 September 2011

Block Structured Language vs OO Languages

Introduction

A programming paradigm is a fundamental style of computer programming. It provides for the programmer the means and structure for the execution of a program and thus serves as a pattern or model. Many programming paradigms have their own concepts and abstraction thus offering different techniques for the programming world. Also they are as well-known for what they do not do as for what they do.As a result, many programming paradigm are regarded as an improvement over earlier styles. This wiki chapter describes about Block Structured Languages and Object Oriented programming paradigm while emphasizing on the transition/improvement of Block Structured Language to Object Oriented Language due to its advantages and usage in today's programming world.

Overview of Block Structured Languages

Every programming language has a unique syntax and a set of instructions which are usually in a form of statements that should conform to a set of rules in defining and constructing it. The construction of these instruction depends on the language you use. Most of them follow a procedural approach where implementation is done step by step while other require objects to come up with a solution. Among these, the most common and used technique is what we called block structure programming where declarations and statements are grouped together in a form of a block.

Block structured programming language permits the creation of blocks, with blocks nested within other blocks. The function of blocks in programming is to enable groups of statements to be treated as if they were one statement, and to narrow the lexical scope of variables, procedures and functions declared in a block so that they do not conflict with variables having the same name used elsewhere in a program for different purposes.

In a block-structured programming language, the names of variables and other objects such as procedures which are declared in outer blocks are visible inside other inner blocks, unless they are shadowed by an object of the same name. They do not require prior detailing to be given to the definitions made.The structure is formed first without knowledge of inner groupings. This supports top-down development of code in an organized manner.Blocks allow the programmer to treat a group of statements as a unit, and the default values which had to appear in initialization in this style of programming can, with a block structure, be placed closer to the decision. This technique does not only simplifies the code but also contributes to its readability and maintainability.

Example of a block structured code in JavaScript:-

calculateVal:function(val){
   var increment = $(val).increment;
   var incValue = ;
   for(i=0;i<increment.length;i++){
      if(increments[i].selected==true){
            incValue=increment[i].value;
      }
   }
  return incValue;
}


The for looping statement uses the block structure in this code snippet. The value of incValue changes as it passes to the loop.

Implementation of Block Structure in programming languages

ALGOL

  begin 
  integer X;
  Read Int(X);
 
      begin 
      real square;
  
      square= X*X;
      Print Real(square);
      end
  end


In Algol, the block structure is formed by grouping the code by a set of delimiters such as begin.... end. The block of statements is used also to represent the scope of the variable. Here, variable square has the scope within the nested block while scope of variable X is within the whole block.


PASCAL

  if wages > tax_threshold then
       begin 
       paytax := true ;
       tax := (wages - tax_threshold) * tax_rate;
       if wages > supertax_threshold then 
             begin 
             pay_supertax := true;
             supertax := (wages - supertax_threshold) * supertax_rate;
             end
       else
             begin 
             pay_supertax := false;
             supertax := 0;
             end
       end
  else
       begin
       paytax :=0;
       superpaytax :=0;
       tax :=0; supertax :=0;
       end
  finaltax := wages - tax - supertax;


The block structure in this example is implemented in a conditional statement. Notice that is easier to determine the result of the condition by just checking on the inner blocks. Changes can also be integrated with ease in this case without affecting the other blocks. This blocks can also be implemented on looping statements.


Overview of Object Oriented Language

Block Structure vs OO Language

History of Block Structure: Before OO Language

Ideas of block structure were developed in the 1950s during the development of the first autocodes, and were formalized in the Algol 58 and Algol 60 reports. Algol 58 introduced the notion of the "compound statement", which was related solely to control flow. The subsequent Revised Report which described the syntax and semantics of Algol 60 introduced the notion of a block, consisting of " A sequence of declarations followed by a sequence of statements and enclosed between begin and end..." in which every declaration appears in a block in this way and is valid only for that block.

The notion of blocks is introduced by different syntax in different languages, but there are two broad families: the ALGOL family in which blocks are delimited by the keywords begin and end, and the C family in which blocks are delimited by curly braces "{" and "}".

Block Structure basically provides a restricted visibility for the local identifiers. The visibility or scope is used to associate an identifier with its proper declaration. In case of blocks, the visibility restrictions help prevent unintended interference with other uses of the same identifier. In case of nested procedures, the scope rules allow the procedure to include the definition of subsidiary procedures without concern for interference with other global procedures. This capability facilitates writing procedures and often simplifies their usage.

Block Structure permits efficient use of storage. As a consequence of the scope rules, storage may be shared among local variables of procedures declared at the level. This is the reason that an Algol program, for example, often requires less storage than a Fortran program. In Algol, storage is required for data associated for active procedures while in Fortran the storage is required for data associated for all procedures.


Transition to OO language

The main purpose of OO language taking over the block structured was the debugging comfort that it provided as the programs were getting larger and larger and it become difficult to debug. Also one of the most important characteristics of OOP is data encapsulation where the internal representation of an object is generally hidden from view outside of the object's definition. The public interface, formed by the collections of messages understood by an object, completely defines how to use this object. Programs that want to manipulate an object, only have to be concerned about which messages this object understands, and do not have to worry about how these tasks are achieved nor the internal structure of the object. The hiding of internal details makes an object abstract, and the technique is normally known as data abstraction.

Another powerful feature of OOP, is the concept of inheritance, meaning the derivation of a similar or related object from a more general based object. The derived class can inherit the properties of its base class and also adds its own data and routines. The concept above is known as single inheritance, but it is also possible to derive a class from several base classes, which is known as multiple inheritance.

And one more important characteristics is polymorphism where message is sent to an object without concern about how the software is going to accomplish the task, and furthermore it means that the task can be executed in completely different ways depending on the object that receives the message.When the decision as to which actions are going to be executed is made at run-time, the polymorphism is referred to as late binding.

Other important points that make OOP better than Block Structure are :-

1) simplicity: software objects model real world objects, so the complexity is reduced and the program structure is very clear. 2) modularity: each object forms a separate entity whose internal workings are decoupled from other parts of the system. 3) modifiability: it is easy to make minor changes in the data representation or the procedures in an OO program. Changes inside the class

  do not affect any other part of a program, since the only public interface that the external world has to a class is
  through the use of methods;

4) extensibility: adding new features or responding to changing operating environments can be solved by introducing a few new objects

  and modifying some existing ones;

5) maintainability: objects can be maintained separately, making locating and fixing problems easier; 6) re-usability: objects can be reused in different programs

Block Structure in OO language

Block Structure in any language is defined to be a block of a code that is grouped together. It can be any block of code like conditional statements as in 'if' and 'else' or looping constructs like 'for' loop or 'while' loop. Most OOP language basically involves such conditional statement and constructs like Java, C++ or Ruby. Block Structure is basically still a part of OOP languages nowadays.

In OOP terminology, block structure means classes, nested classes, methods and blocks. Object is used as a common name for instance of a class. A block activation is an instance of a block while in OOP it is an instance of a class which is an object.

Simple Class Definition in Java :-


    Class Employee
    {
        int age;
        String name;
        int empID;
    }


This code describes a class which also defines a block structure in JAVA.


Many aspects of block structure are viewed in OOP languages like:-

a) Locality:- The major advantage of block structure is locality. This makes it possible to restrict the existence of an object and its description to the environment where it has meaning. b) Scope Rules:- There are following aspects of scope rules for names declared within an object:

                1. They only exist when the object exist. This is a consequence of locality.
                2. Access to global names and re-declaration of names.
                3. Access to names within a block from “outside” the block may be restricted. Like in Simula language.

Conclusion