CSC/ECE 517 Fall 2011/ch1 1e an
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
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 :-
- simplicity: software objects model real world objects, so the complexity is reduced and the program structure is very clear.
- modularity: each object forms a separate entity whose internal workings are decoupled from other parts of the system.
- 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.
- extensibility: adding new features or responding to changing operating environments can be solved by introducing a few new objects and modifying some existing ones.
- maintainability: objects can be maintained separately, making locating and fixing problems easier.
- 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 id; Employee(int Eage,String Ename, int Eid) { age=Eage; name=Ename; id=Eid; } void display() { System.out.println("Employee "+ name + " has age " + age " and id " +id "); } }
This code describes a class which also defines a block structure describing class and methods in JAVA.
Following aspects of block structure are viewed in OOP languages like:-
- 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.
- Scope Rule :- These are following aspects of scope rules for names declared within an object:
- They only exist when the object exist. This is a consequence of locality.
- Access to global names and re-declaration of names.
- Access to names within a block from “outside” the block may be restricted. Like in Simula language.
Comparison of Block Structure and Object Oriented Language
Block-Structured Languages | Object Oriented Languages |
---|---|
Block Structure involves section of code grouped together with one or more declaration and statements | Object Oriented Programming involves programming around objects which is an instance of a class representing data fields and methods for interaction. |
Block Structure provides no means of access to variables and limits the scope within the block. | Object Oriented Programming allows to define different access specifiers(public,private,protected) when defining any field or method. |
Maintainibility is difficult as the programs size increases | Easy to maintain as objects can be maintained separately thus making easy to locate and manage. |
Program flow has a single point of entry and exit | Program flow depends on the state and interaction within objects. |
Has limited modifiability where a procedure is defined for performing certain function which cannot be modified to a greater extent | Easy to make changes in the data representation and methods as changes within a class do not affect others. |
No re-usability | Objects can be re-used in another programs. |
Not preferable when working with large programs | Can handle large programs due to its design, organization and flow of the program. |
Limited Modularity | The objects form a separate entity whose internal workings are decoupled from other parts of the system. |
Limited extensibility as very difficult to extend the modules and its function which requires re-structuring | Introduction of few new objects can help in adding or changing new features. |