CSC/ECE 517 Fall 2011/ch1 1e aa

From Expertiza_Wiki
Jump to navigation Jump to search

Wiki Chapter: CSC/ECE 517 Fall 2011/ch1 1e aa

Block-Structured languages vs Object-Oriented languages; effectiveness of Object-Oriented languages and use of block-structure in Object-Oriented languages.

Brief Background on the Programming Paradigms

Programming Paradigms form the fundamental basis of the style in which we code. Paradigms define the way the code is structured aesthetically. Different paradigms differ in the way in which a language defines its concepts about the way to represent the code elements i.e. variables, functions, objects etc. and the way in which computation of the code takes place. Thus, any paradigm acts as a structure or set of rules on which that language is based. This provides the programmer with set of principles which are to be obeyed when the language is used.

There are many different programming paradigms which are developed over the years. Each one offers something different than the others and many are considered much better over the others. Another flavour to paradigms is that some languages can support more than one paradigms. This gives the programmer the choice of how to use the elements of different paradigms in his own discretion.

In this article, we focus on two programming paradigms: Block-Structured programming and Object-Oriented Programming.

Introduction

This Wiki chapter talks about the basic fundamentals of two programming paradigms; block structured programming and Object Oriented programming and explains the advantages of Object Oriented programming over block structured programming which made O-O languages more common and widely used in the Software Industry today. We also focus on the practicability of using block structured approach in O-O languages.

Block-Structured Languages

A Block is a part of code that is clustered together. It is thus; a group of program statements and variables referred to in those statements. Block of code always begins with variable declarations and is followed by procedural declarations, is always contained within delimiters; typically begin-end, opening and closing curly braces '{ }' and can be compiled and executed as a single execution unit. Block can be the body of a subroutine, a function or an entire program. The main block can contain subsections consisting of inner blocks. Those inner blocks can contain more inner blocks giving rise to a nested block structure. Typically, Nesting can be repeated to any depth required. One example of a language which allows such block structure is Pascal[1].

program a;  
   var id1, id2, id3 : integer;     { program a declarations }  
                                                  
   procedure b;                            
         var id1 : integer;         { procedure b declarations } 
                                                        
       procedure c;                         
              var id2 : integer;    { procedure c declarations}      
              begin    { Beginning of c's statement part }             
              id2 := id1;                     
              end;              
         begin     { Beginning of b's statement part }
         id1 := id3;                           
         id2 := id1;
         end; 
                                                                      
    begin     { Beginning of main program's statement part } 
    id1 := id2; 
    end.

In most primitive block structured languages, the scope of a variable can be limited to the block in which it is declared. This is called lexical scoping. Thus, referring to the nested structure of the blocks; all the variables declared in the outer block can be accessed within that block and all of its inner blocks but are not accessible outside that block. Additionally, values of the variables in the outer blocks are accessible in the inner blocks if and only if there is no other variable in the inner block with the same name. This duplicate declaration of variables is called Shadowing.

By having statements grouped together as a Block allows us to treat it as a single statement and thus allows the programmer to keep the 'lexical' scope of the functions, variables and procedures closely bound to that Block. Earliest block-structured languages were Algol 58 and Algol 60 with which the initial idea of block was born.

Relation of Block-Structured Programming to Structured Programming

There is a subtle relation between block programming and structured programming. Structured programming encompasses majority of the fundamentals of block programming paradigm. Most of the block-structured languages fall under the structured programming paradigm for example: Algol, Pascal. In essence, structured programming employs a hierarchical approach in which the main problem is broken down into different smaller modules. Thus, it breaks down a bigger task into smaller ones and therefore solving the smaller tasks leads to indirectly solving the actual problem.

The important thing to note here is that such programs always have a single point of entry and often have single points of exit. The modules in this paradigm are independent of each other and thus; are blocks of code where the scope is limited to that particular module. Structured Programming normally imply simple hierarchical flow structures consisting of sequence (execution of statements in particular order), selection (some selection criteria) and iteration (repetition until the program reaches a certain state).

Features of Block-Structured Languages

  • Structured programming is task-centric
  • Applies a top-down approach of problem solving.
  • It is a straight forward programming approach with a pre-defined flow.
  • Programs have a modular design structure.
  • Employs an approach of bringing data which is to be operated upon to the functions or methods.
  • Most often; such programs have a single point of entry and single point of exit.
  • Allows the programmer to keep the program within his intellectual grasp due to its modular design and limited variable scope.
  • Programs have data-structures with a limited scope.
  • Programs allow limited control structures.

Advantages of Block-Structured Languages and related programming paradigms

  • Simplicity in Writing Code: It is extremely easy to write code in a block structured language. Modularity is the prime reason due to which programmers can concentrate on various aspects of the program and design their code in the most efficient way. The concept of single point of entry also allows the programmer to better design their code in a heirarchial strucuture and thus create a better solution. Easiness in writing code amounts to saving precious time. If written efficiently, procedures can also be used in other programs requiring the same functionality.
  • Debugging made easy: Modular structure provides the progammer to isolate bugs easily. As each procedure does only one particular task, it is easy to debug individually. Programmer can recognize the errors by simply narrowing it down to the procedure which is faulty. Additionally, each procedure in the modular design has a single point of entry i.e. through any other procedure. This makes it easy to write and use Stubs for testing individual procedures before they are used or integrated into the main program. Stubs are dummy procedures which provide test data to the procedures.
  • Understandability of Code: It is extremely easy to look at procedures and figure out the entire modular structure of the program. Each procedure and variables have meaningful names which makes it very lucid and easy to understand. Morever, the scope of the variables in the procedure is often limited to that procedure itself which adds to the simplicity of figuring what that variable is used for.
  • Modification made simple: Due to all the above properties of a block structured program, any programmer looking at code written by some other programmer can easily understand and thus modify it with least effort. Additionally, if the specifications of the program change later, changes to it can be made easily.

Limitations of Block-Structured Languages and related programming paradigm

  • Top-down design approach focuses more on the design of sequence of instructions required for the solution. Design of data-structures which is also an integral part of designing the solution to the problem is outside the scope of the top-down design approach.
  • As data is to be passed to the methods; there is no encapsulation. A better approach is keeping data as it is and declaring the necessary funcitons near the data.
  • There is no information hiding concept in structured programming. The concept of lexical scope applies but is not equivalent to information hiding or encapsulation.
  • Top-down design approach does not suit all type of problems. If we cannot determine the sequence of instructions in advance, structured programming cannot be applied for that problem.
  • The modular design of structured programming poses a very big problem. By dividing the problem into seperate methods/functions, it limits the usability of those functions to only that problem or problems of the specific genre. These modules/methods cannot be used easily into other problems. Use of such modules will require serious re-design and effort.
  • Debugging is not simple once the size of the program increases. Programmer has to actively understand the entire structure of the program to debug even a smallest problem as modules in the structure depend on each other.

Object-Oriented Programming

Object-oriented programming (OOP) is a programming paradigm which focuses on objects instead of actions and data instead of logic. Historically, a program has always been viewed as a logical sequence of instructions that takes the input, processes it, and produces the output. Due to this focus, the programming challenge has always been the logical sequence, rather than defining data. Whereas, OOP takes the focus away from the procedure. It represents data from the real world (called as objects) which we really want to manipulate rather than the logic required to manipulate them.

While Simula was the first object-oriented programming language, the most popular OOP languages used today are Java, Python, C++, Visual Basic .NET and Ruby. Although many languages claim to be solely object oriented, most of the time that is not the case. There are some languages that are purely o-o ,while others are hybrid. Now, a language must capture several qualities for it to be purely O-O. These qualities are:

  • Encapsulation/Information Hiding
  • Inheritance
  • Polymorphism/Dynamic Binding
  • All pre-defined types are Objects
  • All operations performed by sending messages to Objects
  • All user-defined types are Objects

Below is an small example of Object-Oriented Programming in Java:

class A {
 int x;
 int y;
 int get(int p, int q){
 x=p; y=q; return(0);
 }
 void Show(){
 System.out.println(x);
 }
}  // end of Class A    
       
class B extends A{
 public static void main(String args[]){
 A a = new A();
 a.get(5,6);
 a.Show();
 }
 void display(){
 System.out.println("B");
 }
} // end of Class B

Pure O-O languages satisfy all the above qualities, whereas, hybrid languages support some of these. Typically, many languages support first three qualities, but not the last three. Some examples of pure O-O languages are Eiffel, Smalltalk, and Ruby.

Many think of Java as a pure Object-Oriented language, but by its inclusion of "basic" types that are not objects, it fails to meet the fourth quality. Also it fails to meet quality five by implementing basic arithmetic as built-in operators, rather than messages to objects. C++ supports multiple paradigms, O-O being one of them. Thus it is not a pure oo language. Another seemingly object oriented language, Python is actually a multi-paradigm supporting language. At times, o-o concepts seem to be fixed up in it. Some operations are implemented as methods, while others are implemented as global functions. The self parameter adds to its awkwardness.

Ruby on the other hand, is a scripting language which was created as a reaction to Python and Perl. The designers of Ruby wanted a language that was stronger than Perl and more object oriented than Python. Visual Basic and Perl are both procedural languages that have had some Object-Oriented support added on as the languages have matured.

Features of Object-Oriented Languages

Object-Oriented Terms and Concepts

In OOP the encapsulation is mainly achieved by including within a program object all the resources needed for the object to function i.e. methods and data. Due to this, a class may change its internal implementation without affecting the overall functioning of the system. Thus encapsulation hides what a class and makes it a black box. Interfaces are used to interact with the objects and hide the implementation of the object.

To make it more lucid, lets take a look at an example:


public class Encapsulation{
  private String name;
  private String id;
  private int age;
  public int getAge(){
     return age;
  }
  public String getName(){
     return name;
  }
  public String getId(){
     return id;
  }
  public void setAge( int newAge){
     age = newAge;
  }
  public void setName(String newName){
     name = newName;
  }
  public void setId( String newId){
     id = newId;
  }
}

Abstraction is suppressing the implementation details while representing the data by focusing on the idea, qualities and properties. Abstraction makes concentrating on the concepts easier by factoring out the details. It is the primary means of managing complexity in large programs. Example of Abstraction:

public abstract class Animal {
 public int no_of_legs;
 public double weight;
 public void makeSound(){
  System.out.println("I don't know as I have no type!");
 }
 public void eat(){
  System.out.println("Chomp! Chomp!");
 }
}
public class Lion extends Animal {
 public int length_of_mane;
 public boolean isKingOfJungle;
 public void makeSound(){
  System.out.println("I am A Lion! Roaarrrrr!");
 }
}

Thus, classes are declared as abstract in Java by using the 'abstract' keyword. Use of Abstraction is necessary during design when such parent classes have to be made as the contain a functionality common to all child classes. The abstract class is useless unless it is inherited. An object of an abstract class cannot be made because its 'too' abstract to exist on its own.

Deriving a new class from an existing one by simply extending the parent class is called as inheritance. The extended class is called as a subclass and it inherits attributes and behaviors of its parent class which is also called as superclass or base class. Example for Inheritance:

class Animal {
  ..........
}
class Pig extends Animal {
  ..........
}
class Dog extends Animal {
  ..........
}
class Elephant extends Animal {
  ..........
}

The dictionary meaning of polymorphism is “many shapes”. In OOP, it is the ability of an interface to be realized in multiple ways. In OOP the polymorphism is achieved by using many different techniques named method overloading, operator overloading and method overriding.

Method overloading : The method overloading is the ability to define several methods all with the same name but different signatures.

Operator overloading : The operator overloading is a property in which all the operators like +, - or == are treated as polymorphic functions and as such have different behaviors depending on the types of its arguments.

Method overriding : Method overriding is a language feature that allows a subclass to override a specific implementation of a method that is already provided by one of its super-classes.

Example of Polymorphism:

public interface NonVegetarian{}
public class Animal{}
public class Lion extends Animal implements NonVegetarian{}
Lion l = new Lion(); //Creating a new Lion Object
Animal a = l;        //Lion is-a Animal. Hence, Animal object reference can refer to Lion
NonVegetarian n = l; //Lion is-a NonVegetarain. Hence, NonVegetarian object reference can refer to Lion
Object o = l;        //Lion is-a Object (root of the Class Hierarchy). Hence, Object's object reference can refer to Lion

Thus, The type of the reference variable would determine the methods that it can invoke on the object.

What makes Object-Oriented Languages better than Block-structured Languages?

What block-structured programming does for legacy systems, object-oriented programming does for software systems in general. That is, it manages the complexity of these systems. But object- oriented technology has better things to offer. Here is how:

  • The program structure is simplified as the real world objects have been modeled in the software objects. This makes designing the problem much more simple that block structured programming where procedures have to be written for every functionality needed. [2]
  • The program becomes modular as the internal working of each object is highly decoupled from other parts of the program which is not the case in block structured programming where modules depend on one another as compared to O-O programming. [3]
  • Debugging and testing becomes an easy job in O-O Programming. Unit tests can be written for each class and thus its objects and they can be tested exhaustively. Also making minor changes in data representation or procedures is simple and does not affect any other component of the code. This makes the code maintainable as well as modifiable. [4]
  • Classes and their Objects can be thought of self-contained as they contain data and functions that act on data tied together. Thus, using these classes and thus objects in another program where the same functionality is needed is possible. It is also possible to extend the functions provided by the class easily. Reuse of code in new applications becomes easy. [5]
  • Classes and Objects provide data security through the principles of encapsulation and access specifiers. Thus, objects can contain data which is available to the outside world and data which is completely controlled by itself. Object provides interfaces to access this data whose implementation is not available to other parts of the program. Data security is not provided by block structured programming where only scope rules apply.
  • As compared to structured programming, OOP is more scalable. An object’s interface may guide you to reuse the code in new software, besides providing you with the information that needs to be replaced without affecting other code. Thus, newer technology can replace the aging code hassle free.
  • Adding new features or responding to changing operating environments can be solved by introducing a few new objects and modifying some existing ones; making the code easily extensible. This requires considerable effort in Block-structured programming where adding new features can result into dependency problems with other existing modules. [6]
  • Real world modeling is possible using Object-oriented system in a more complete fashion as compared to traditional methods. Organizing objects and methods into classes is what makes it easier to reflect the real world. This makes it possible to visualize the problem easily and practically.
  • The modular structure for programs in O-O Programming makes it possible for defining abstract data-types according to required specifications where implementation details are hidden and the unit has a clearly defined interface. This is not possible in Block structured programming. [7]
  • OOP provides a good frameworks for code libraries where supplied software components can be easily adapted and modified by the programmer. This is particularly useful for developing scalable applications. This facility is not available in Block-structured programming. [8]
  • Some other advantages of OOP are that it makes code development faster, has better IDEs, allows single-instance code, testability, Catch errors at compile time rather than at run-time.

Limitations of Object-Oriented Languages

  • It is not always that the real world neatly divides into classes and subclasses. There may arise some ambiguity as the complexity increases. This may lead to artificial class relations
  • O-O programs is sometimes hard to test, especially in case of classes with low cohesion.
  • As the complexity of the problem increases, unnecessary complications in the program structure may be introduced making it difficult to interpret.

Block-structure in Object-Oriented Programming

The fundamentals of a Block-structure cannot be eradicated from modern programming. O-O languages such as Java encompass block structure in the declaration of methods, functions and procedures. The Object-Oriented properties of such languages make them not-block structured.

Java has all the features of an Object-Oriented language but makes use of block structures in writing looping constructs such as 'if-else', 'while', 'for'. The functions written in Java also make use of the lexical scope rules. This means that when we write a function in Java, the local variables declared within the function block are known to that particular function only. Thus, this is logically equivalent to the functions in block-structured languages such as C. Java also contains the concept of global variables which are accessible throughout the program to all classes.

Example of local variables is shown below. These variables are only available when the function is called using an object of the Class type Structure.

class Structure
{
  private int a;
  private int b;
  public void isItAStructure(boolean t) {
    int local_variable1;
    int local_variable2;
    ..........
     }
}

Nested classes are also supported in Java. Thus, we can have class declared under a class. There are two types of nested classes; non-static( which are called inner classes ) and static. Scoping rules apply for nested classes. The inner class instance can access the variables and methods of the enclosing class even if declared private. Additionally, this inner class instance can only exist if there is a corresponding outer class instance. This is an efficient way of increasing encapsulation.

Example of nested classes is shown below.[9]

public class OuterClass
{
  private String outerInstanceVar;
  public class InnerClass
  {
     public void printVars()
     {
        System.out.println( "Print Outer Class Instance Var.:" + outerInstanceVar);
     }
  } 
}

Java also allows organizing our code into packages. Packages also have scoping rules. Classes declared in one package cannot be accessed outside that package unless the package is explicitly imported into the program. This can be thought of logically as being one block of code(consisting of multiple files) which has scoping restrictions. Thus, block-structure can be used and is used in some of today's O-O languages.

Comparison in a Nutshell

Let us compare both the programming paradigms with respect to different points which brings out a strong distinction between the two.

Point of Comparison Block-Structured Languages Object-Oriented Languages
  • Primary focus
Focus on finding the sequence of instructions necessary to solve the problem. Design of the necessary data-structures is out of scope. It is task-centric. Task-centric here means that the prime focus is on development of functions which manipulate data and pass them on to other functions or as they are called: modules. Focus on identifying and representing the problem in terms of an 'object' which has its own data, sub-routines and state. Different objects in the problem interact by sending messages to each other and thus result in change in its internal state. The final state and values of the objects refer to the solution. It is data-centric.
  • Problem Solving Approach
Primarily Top-down design Identification and design of necessary objects. Close to being 'better models of the way the world works'.
  • Program Flow
Often sequential with program having single point of entry and exit. Complex program flow. Can sometimes depend on the internal state of the objects.
  • Modularity
Limited modularity. Program is divided into modules or per say procedures independent of each other but are constrained due to uniqueness to that particular problem. Extremely modular due to the presence of objects which contain their own data and sub-routines.
  • Data Protection/Hiding
No concept of data-hiding. Variables local to one method cannot be accessed by other method. But, Global variables can be accessed anywhere within the program. One of the main fundamentals of O-O languages. Access specifiers like 'public', 'private' and 'protected' dictate the rules of data-hiding. Data which is private is confined to one object and cannot be directly changed by any other method except its own. This places the responsibility of managing data with the object itself This is called as ownership. Thus, data can be accessed ( read/write/modified ) only through the object's own interfaces.
  • Ease of Understanding
Smaller programs are easy to understand but as the program increases in size; understanding is a struggle. Easy to understand due to its real world-like design and flow.
  • Reuse of Code
Limited or no re-usability. Highly re-usable code as the code developed can be easily modified or extended to suit a problem's need.
  • Support for declaring new data types
Extremely difficult as no in-built functionality exists. Easily possible due to the concept of classes. Generic classes can be built as per the required specifications.
  • Efficiency
Efficient for solving small problems. Efficient for solving large problems which have a complex structure and require complex data-types, abstraction and data-security.
  • Maintenance
Maintenance is easy for smaller programs but can consume a-lot of effort for larger program size as it requires the programmer to know and understand the dependencies of every module in the program. This makes it difficult to debug and test the program. Extremely simple as O-O languages aim for high modularity. Secondly, programmer is not concerned with the details of how the data is stored and represented. Thirdly, they also tend to keep low coupling which makes it easy to debug and test different modules in the program.
  • Extensibility
Less Extensible as modules developed need to be re-organised and re-structured heavily in order to meet different needs. High extensibility is one of the most important advantages of OOP. Code can be easily modified and 'plugged-in' to a different program. Methods can be exteneded due to many properties such as polymorphism, inheritance and support for multiple inheritance through interfaces.
  • Flexibility
Less flexible. Sometimes, certain problems do not fit into the 'top-down design' approach. High flexibility. The modelling of problems into world-like objects makes it easy to solve any practical problem.
  • Examples
C, Pascal, Algol 58, Algol 60 C++, Java, Ruby, Python.

Conclusion

The article has successfully summarized the advantages and disadvantages of both block-structured and object-oriented programming. Thus, Object-oriented programming is much better than Block-Structured programming in different aspects and offers much more language-features. Object oriented programming provides the user to deal with real world objects and thus makes it more easier for the programmer to deal with large complex problems. Block structured programming provides the users with a structured task-centric approach and some of its basic fundamentals are still used in Object-Oriented languages. With the ever growing need for scalability, modularization, maintainability and re-usability; Object-Oriented programming is going to be preferred paradigm of programmers.

References