CSC/ECE 517 Fall 2010/ch1 1e vs

From Expertiza_Wiki
Revision as of 02:39, 2 September 2011 by Vvarre (talk | contribs)
Jump to navigation Jump to search

CSC/ECE 517 Fall 2010/ch1 1e vs


Introduction

This article mainly compares and contrasts Block-Structured languages and Object-Oriented languages and also focuses on the advantages of Object-Oriented languages over Block-structured languages. It also emphasizes on the usage of block structures in Object-Oriented programming.

What is a Block-Structured Language?

A block is a section of code which is grouped together and consists of one or more declarations and statements.

A block structured programming languages is a class of high level programming languages that allows the creation of blocks and includes the nested blocks as components where nesting could be extended to any depth. Some examples of block structured languages are: ALGOL,PASCAL,FORTRAN etc.. Block structured languages have a syntax such that the structures are enclosed within the bracketed keywords like if....fi in ALGOL language.

An example of a block in ALGOL looks as shown below:

begin
  integer N;
  Read Int(N);
  begin
    real array Data[1:N]
    real sum,avg;
    sum:=0;
    for i:=1 step 1 until N do
       begin real val;
        Read Real (val);
        Data[i]:=if val<0 then -val else val
       end;
    for i:=1 step 1 until N do
     sum:=sum+Data[i];
   avg:=sum/N;
   Print Real(avg);
  end 
end

Features

The essential composition of block structured programming tends to include three basic elements:

  • Concatenation: Concatenation has to do with the logical sequence of the statements that make up the basics for the order to be executed. Each step in the sequence must logically progress to the following step without invoking any undesirable activities.
  • Selection: Selection is included in the process of structural programming. This allows for the selection of any one of a number of statements to execute, based on the current status of the program. Generally, the selection statements contain keywords that help to identify the order as a logical executable, such as “if,” “then,” “endif,” or “switch.”
  • Repetition: In repetition a selected statement remains active until the program reaches a point where there is a need for some other action to take place. Repetition normally includes keywords such as ”repeat,” “for,” or “do…until.” Essentially, repetition instructs the program how long to continue the function before requesting further instructions.

The exact nature of block structured programming will varies depending on the purpose and function of the program.

Advantages

  • The major advantage of block structured language is restricted visibility to local identifiers.
  • Block structure permits efficient use of storage.
  • The learning and programming of the language is both quick and relatively easy.
  • Since the number of de-bugging runs is usually small, turn around time is likely to be quite rapid.

Limitations

  • Focus is on the algorithm and importance is given to the procedure (logic) and not to the data on which these procedures operate.
  • The data need to be global but global data makes the code complex.
  • As the code size grows, maintaining code becomes difficult and sometimes impossible.
  • Structured programming language is not ‘modular’. In other words, the code is not split up into reusable sections. The code is written and executed sequentially; therefore leading to redundant code. It may take longer to develop programs using structured language.

What is Object-Oriented Programming?

Object-Oriented programming is a programming paradigm using "objects"- the data structures which consists of data fields and methods together with their interactions to design the applications and computer programs. The programming techniques include features like data abstraction, encapsulation, messaging, polymorphism and inheritance. It is a type of programming language where the programmers define not only the data type of a structure but also the type of functions that can be applied to the data structure.

An example of the code from JAVA language is as follows:

Class HelloWorld
{
  public static void main(String args[])
  {
    System.out.println("Hello world!"):
    System.out.println("Arguments you have entered is:");
    if (args.length>0)
    {
      for(int i=0;i<args.length;i++)
      {
        System.out.print(args[i]+" ");
      }
      System.out.println();
    }
   else
      System.out.println("<<No Arguments>>");
   } 
}

Features

  • Inheritance: Inheritance defines relationships among classes in an object-oriented language.It helps objects work together and makes it easy to extend existing structures to produce new structures with slightly different behavior. Example: In Java programming language, all classes descend from java.lang.Object and implement its methods.
  • Encapsulation: Encapsulation is defined as the process of binding or wrapping the data and the code that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. Encapsulation acts as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.
  • Data Abstraction: Data abstration is the development of classes, objects, types in terms of their interfaces and functionality, instead of their implementation details. Abstraction can denote a model, a view, or some other focused representation for an actual item. It is the development of a software object to represent an object we can find in the real world. Abstraction is used to manage complexity.
  • Polymorphism: Polymorphism enables one entity to be used as as general category for different types of actions.The concept of polymorphism can be explained as "one interface, multiple methods". Polymorphism results from the fact that every class lives in its own name space. The names assigned within a class definition will not conflict with names assigned anywhere outside it. This is true both of the instance variables in an object's data structure and of the object's methods. There are two basic types of polymorphism. Overridding, also called run-time polymorphism and Overloading, which is referred to as compile-time polymorphism.

Advantages

  • Ability to simulate real-world event much more effectively
  • Code is reusable thus less code may have to be written
  • Data becomes active
  • Better ablility to create GUI (graphical user interface) applications
  • Programmers are able to reach their goals faster
  • Programmers are able to produce faster, more accurate and better-written applications

Limitations

  • Object-oriented Development is best suited for dynamic, interactive environments, as evidenced by its widespread acceptance in CAD/CAM and engineering design systems but wide-scale object-oriented corporate systems are still unproved, and this procedure may become complicated for systems that don't require so much complexity.
  • Object-oriented Development is not yet completely accepted by major vendors. There are major reservations as to whether Object-oriented development will become a major force, or fade into history.
  • Although it is often claimed that objects in OOP are similar to real-world objects, the analogy is at best limited. If objects in OOP were indeed similar to real-world objects, programmers would transfer their every-day experiences to programming and learn OOP technology with little effort but this iis often not the scenario.
  • OOP provides only one representation (object-oriented). Although this representation may be advantageous in many ways, humans use multiple representations (Davis et al., 1995). For example, expert OOP programmers may focus on the functional properties of the code instead of on objects.
  • Although OOP technology allows the creation of reusable components, programmers may not know what reusable components already exist, how to access them, how to understand them, and how to combine, adapt, and modify them to meet current needs. Current OOP technology provides only limited tools for locating reusable classes and methods.

Transition from Block Structured to Object Oriented Programming

Before the emergence of block strcuctured programming,programmers used to write their code as a continuous stretch of code that extended line after line without any organized structure. This approach was tedious in terms of understanding,debugging and maintaining. This lead to the developement of structural programming.

The principle idea behind structured programming was the idea of divide and conquer. A computer program was considered as a set of tasks and any task that is too complex to be described simply would be broken down into a set of smaller component tasks, until the tasks were sufficiently small and self-contained enough that they were easily understood. This was termed as the top-down methodand was found to be adequate for coding moderately extensive programs. Some examples of structured programming languages are: FORTRAN, Pascal, ALGOL,C etc.

Block-Structured programming remained an enormously successful approach for dealing with complex problems until the late 1980s, when the size of programs increased and some of the deficiencies of structured programming had become evident.Some of these problems included:

  • In this type of methodology as the emphasis is on functions, as the actions became more involved or abstract, the functions grew to be more complex.
  • Hierarchical structuring for data and procedures produced cumbersome code with large amounts of "tramp data"
  • Usage of global subprogram names and variables was recognized dangerous.
  • Also, a small change, such as requesting a user-chosen new option (text font-color) could cause a massive ripple-effect with changing multiple subprograms to propagate the new data into the program's hierarchy.

Object-oriented programming was developed to respond to these needs, providing techniques for managing enormous complexity, achieving reuse of software components, and coupling data with the tasks that manipulate that data.Object Oriented programming paradigm used "objects" which were considered to be data structures consisting of data fields and methods together with their interactions. It also took the concept of subroutines into a completely different zone. Programming techniques included features such as data abstraction, encapsulation, messaging, modularity, polymorphism, and inheritance which changed the face of programming for many people.Some of the most commonly used object-oriented programming languages are: C++, Java etc..


Compare and Contrast

Block-Structured languages vs. Object-oriented languages

Properties Block-Structured languages Object-oriented languages
Data Protection These languages do not prohibit a code that directly accesses global data, and local variables are not very useful for representing data that must be accessed by many functions. Object-Oreiented languages enforce "hiding" of data declared private in a class which can be accessed only through the public member functions of the class.
Creation of new data types It is tedious at the language level if the language does not support OOP. It is easily accomplished at this language level by defining a new class for the data type.
Program design It is complex since the procedure orientation forces the designer to map the real-world problems to features of the programming language. It is simpler because the object orientation allows the designer to work with a much higher level conceptual model of the real-world problem.
Comprehension of the program Rigorous due to poor equivalence between the program variables and physical entities More apparent due to the close match between program classes and real-world classes.
Modularity Not as good since data structures and functions are not linked. Better since related data and functions are placed in the same module.
Maintainance and extension of the program Requires high degree of knowledge of the entire program. Convenient due to enhanced modularity of the program.
Reusability of code Difficult to reuse since submodules are generally coded to satisfy specific needs of a higher level module Easy to use the existing classes without modification by deriving sub classes with additional items and functions required for specific application
Primary focus The focus is around the data structures and subroutines. The subroutines are where actually where stuff happens and data structures are just the containers of the data required by the subroutines. The primary focus of object-oriented programming is on the data itself. Here the types are designed first and then come up with the operations needed to work with them.
Examples C, ALGOL 60, PASCAL are examples of block-structured languages. C++ and JAVA are examples of object-oriented languages.