CSC/ECE 517 Fall 2011/ch1 1e dm: Difference between revisions
Line 20: | Line 20: | ||
<code> | <code> | ||
define MAXSIZE 200 | define MAXSIZE 200 | ||
int storage[MAXSIZE]; | int storage[MAXSIZE]; | ||
int top; //index pointing to the top of stack | int top; //index pointing to the top of stack | ||
void main() | void main() | ||
{ | { | ||
void push(int); | void push(int); | ||
int pop(); | int pop(); | ||
Line 48: | Line 48: | ||
scanf("%d" , &will); | scanf("%d" , &will); | ||
} //end of outer while | } //end of outer while | ||
} //end of main | } //end of main | ||
</code> | </code> | ||
Revision as of 21:40, 6 September 2011
CSC/ECE 517 Fall 2011/ch1 1e dm
Introduction
This wiki article describes the block structured and object oriented languages by highlighting their advantages and limitations along with providing a comparison between them. It also discusses the application of block structured concept in the object oriented programming.
Block Structured Languages
Block structured languages are a class of high-level languages in which a program is made up of blocks. A block is a section of code which can be grouped together. Blocks consist of one or more declarations and statements. A block structured programming language uses and permits the creation of blocks and may also include nested blocks as components. The major function of using blocks is to enable the groups of statements within the block to be treated as if they were a single statement.
Block structured programming is related to structured programming, which is a technique for organizing and coding computer programs in which a hierarchy of modules is used, each having a single entry and a single exit point, and in which control is passed downward through the structure without unconditional branches to higher levels of the structure. A structured program is decomposed into a hierarchy of processes. A process in this context is a body of code, typically a function or subroutine, that takes some input and manipulates it to produce an output. A process may be composed of other, more specialized processes, i.e., it may be a function that calls other functions. Structured Programming uses three types of control flow: sequential, test, and iteration.
Examples of block structured programming languages are ALGOL, FORTAN, Pascal, C, Python, Lisp, Ada, PL/SQL.
Characteristics
- Takes on the top-to-bottom approach. A top-down approach is the breaking down of a system into compositional sub-systems, where each sub-system is then defined in further detail. This could be in the form of greater detail, additional subsystem levels, or until the entire system is reduced to base elements.
- Based around data structures and subroutines.
- Uses single entry and exit block.
- 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.
- Splits the tasks into modular forms.
define MAXSIZE 200
int storage[MAXSIZE];
int top; //index pointing to the top of stack
void main()
{
void push(int);
int pop();
int will=1,i,num;
clrscr();
while(will ==1) {
printf("MAIN MENU: 1.Add element to stack 2.Delete element from the stack\n");
scanf("%d",&will);
switch(will) {
case 1:
printf("Enter the data... ");
scanf("%d",&num);
push(num);
break;
case 2:
i=pop();
printf("Value returned from pop function is %d ",i);
break;
default:
printf("Invalid Choice . ");
}
printf("Do you want to do more operations on Stack ( 1 for yes, any other key to exit) ");
scanf("%d" , &will);
} //end of outer while
} //end of main
Advantages
- Straightforward coding: Writing of classes and complex functions is not necessary to write a simple piece of code, which executes a specific set of commands.
- Structuring: Splitting of tasks into blocks/modules makes the program simpler and easier to read when there are less lines.
- Easy Debugging: The blocks/modules are easier to debug and refactor.
- Visibility: It provides restricted visibility for local identifiers [6].
- Storage: Provides efficient use of storage and hence can save an order of magnitude in memory.
- Block structured programming provides the protection of data that is exclusive to one set of modules within a program from compromise by other modules [7].
- Can save an order of magnitude in run time.
- Being block-structured allows Fortran to be used for array operations.
- Overhead involved in coarse-fine interactions can be amortized over larger array calculations.
Limitations
- Limited Use: Block structured programming uses a modular design which is not suitable for all sorts of problem solutions and hence its usage is pretty limited.
- Redundancy: Block structured programming allows a lot of repetition of code since the code is written and executed sequentially, which leads to redundancy of code.
- Data Corruption: Block structured programming does not support data abstraction or information hiding. Hence it could lead to data corruption.
- Readability: Block structured programming is harder to understand and harder to modify when the code becomes considerable large.
- Separate Compilation: Block structured programming does not preclude separate compilation. The procedures depend on the environment in which they are nested, and hence only the outermost procedures could be suitable for separate compilation [6].
- Implementation: Block structured programming though simple to implement, poses difficulties when addressing non-local variables.
- Communication patterns: Block structured programming may offer complex communication patters between different levels.
- Complexity: Block structured programming may have a substantial algorithm complexity.
Object Oriented Languages
Object oriented programming is an approach to programming in which each data item with the operations used on it is designated as an object; the routines used to operate on the data item are called methods; and objects are grouped in a hierarchy of classes, with each class inheriting characteristics from the class above it.
The four fundamentals of object oriented programming languages are Polymorphism, Inheritance, Encapsulation and Abstraction.
Examples of object oriented programming languages are C++, Java, Smalltalk, Ruby.
Concepts
- Objects: Object is the basic unit of object oriented programming. Data and function that operate on data can be bundles as a unit which is called as object.
- Classes: Classes are the data types on which objects are created. This concept is similar to that of structures in C. Memory is allocated to the class only when an object is created.
- Inheritance: Inheritance is the way to reuse and compartmentalize the code by creating collections of objects that can be based on previously created objects with slightly different or different properties and behaviour. A superclass, base class, or parent class is a class from which other classes are derived. The classes that are derived from a superclass are known as child classes, derived classes, or subclasses.
- Data Abstraction: Data abstraction makes it possible to represent the needed information in program without presenting the details. It is also possible to create user defined data types and thus increase the power of programming language.
- Data Encapsulation: Data Encapsulation is the process of combining data and functions into a single unit called class. Using this method, one cannot access the data directly, and is accessible only through functions present in a class. It thus gives rise to the concept of data hiding. It thus means that the internal representation of an object is generally hidden from view outside of the object's definition.
- Polymorphism: Polymorphism is the ability to use an operator or function in different ways in other words giving different meaning or functions to the operators or functions. It thus uses a single function or an operator which functions in many different ways depending upon the specific usage. The purpose of polymorphism is to implement a style of programming called message-passing, in which objects of various types define a common interface of operations for users. The primary usage of polymorphism is the ability of objects belonging to different types to respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behaviour. Polymorphism is not the same as method overloading or method overriding [5].
- Overloading: Overloading is a specific branch of polymorphism, where the existing operator or function is made to have different implementations depending on their arguments. The purpose of overloading is to allow developers to define/redefine the operators as required.
Thus the object oriented programming features helps the program ad there by users of the application to achieve increased performance, it saves time of developing the application, give optimized code for the application, helps in gaining secured applications and there by helps in easier maintenance.
Advantages
- Simplicity: Object oriented programming models real world objects, thus reducing the complexity of the code. It also helps in giving a clear picture of the program structure.
- Modularity: Object oriented programming uses each object to form a separate entity whose internal workings are decoupled from other parts of the system. Hence it helps to reduce large problems to smaller, more manageable problems.
- Modifiability: Object oriented programming helps in making minor changes to the view or the business logic without affecting any other part of the program.
- Extensibility: Object oriented programming helps in adding new features or modify the existing features without affecting the entire program since the objects and classes are isolated from each other.
- Maintainability: Object oriented programming uses objects which can be easily located and destroyed when the object is no longer in use.
- Re-usability: Object oriented programming uses a modular approach where objects and classes can be reused in different programs. Thus less code needs to be written and reduces code redundancy.
- Framework: Object oriented programming provides a good framework for code libraries where supplied software components can be easily adapted and modified by the programmer. This is particularly useful for developing graphical user interfaces.
Limitations
- Complexity: The writing of classes and functions becomes complex when one has to write a straightforward piece of code.
- Dynamic: Object oriented approach is well suited in case of a dynamic environment and not for a simple environment.
- Over generalization: One tends to overgeneralise the amount of development time and often less provision is provided for design processes which may result in incorrect designs.
- Artificial class relations
Compare and Contrast
The table provides a summary of the various features of block structured and object oriented programming as discussed above.
Point of Comparison | Block Structured | Object Oriented |
---|---|---|
1. Definition |
A programming language in which sections of source code contained within pairs of matching delimiters such as "{" and "}" (e.g. in C) or "begin" and "end" (e.g. Algol) are executed as a single unit [8]. | A programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs [9]. |
2. Focus |
Task-centric | Data-centric |
3. Approach |
Top-down approach | Bottom-up approach |
4. Complexity |
Simpler to understand for smaller programs, gets complex to understand when the code becomes considerable large | Can get complex if not used properly, by creating unnecessary abstraction |
5. Modularity |
Sequential approach, not very modular | Each object forms a separate entity whose internal workings are decoupled from other parts of the system. Hence helps to reduce large problems to smaller, more manageable problems. |
6. Data Protection/Hiding |
Do not provide for data protection or hiding. Local variables may not be accessible outside a particular block, but, global variables are accessible everywhere and can be modified anywhere. There is no particular way to specify that an needs to be protected | Provide for data protection/hiding by abstraction and encapsulation.
Encapsulation hides the internal representation of an object from view outside of the object's definition. Abstraction is the process of hiding away the implementation details. |
7. Code Reuse |
It is difficult to reuse work done for other projects. By starting with a particular problem and subdividing it into convenient pieces, top-down program design tends to produce a design that is unique to that problem. | Rather than starting from scratch with each new application, a programmer can consult libraries of existing components to see if any are appropriate as starting points for the design of a new application. |
8. Declaring new data types |
Not supported very extensively, can create structures but not as efficient, since it is task-centric. | Surrounded around the concept of creating new data types that are more natural and focusing on operations on these new data types. |
9. Efficiency |
More efficient for solving straightforward, simple problems | More efficient for solving complex problems |
10. Maintenance |
Though block structured breaks up code into simpler blocks, it is difficult to maintain once there are many such blocks. | Easier to maintain due to modular design provided by classes. |
11. Extensibility |
Less extensible since the all blocks affected would need to be reorganized to incorporate the new feature which may lead to data inconsistency | More extensible since new features can be easily added due to its isolation of objects and classes |
12. Flexibility |
Is less flexible, since to make a single change it may require to change the code in many places since it inherently does not support a lot of code reuse | More due to modular design and code reuse. |
13. Redundancy |
More redundancy due to lack of reuse of code | Lesser redundancy, similar code is usually put together and resued. |
14. Examples |
ALGOL, C, Pascal, Fortan | C++, Java, Ruby, Smalltalk |
Why is Object Oriented better than Block Structured Programming?
Block Structure in Object Oriented Programming
Conclusion
See Also
- Structured Programming
- Object Oriented Programming
- Comparison of Programming Languages
- Object Oriented Design
- Object Oriented analysis and design
- Block
References
- Advantages of block structured languages
- Advantages of object oriented languages
- Advantages of object oriented languages
- Block Structured Languages
- ^Sierra, Kathy; Bert Bates (2005). Head First Java, 2nd Ed.. O'Reilly Media, Inc.. ISBN 0596009208.
- David R. Hanson, "Is Block Structure Necessary?", 1980.
- Pascal Programming Language
- Definition of Block Structured Programming
- Definition of Object Oriented Programming