Upload
dominic-ruiz
View
219
Download
2
Tags:
Embed Size (px)
Citation preview
MT311 (Oct 2007)Java Application Development
Concepts of Programming Languages, Language Evaluation
Tutorial 5
Tutor Information
Edmund Chiu (Group 2) Email: [email protected] Please begin your email subject with [MT311] Webpage: http://www.geocities.com/gianted
Why We Study Programming Language Concepts
We can simulate a useful facility which is not supported in the language you are using
– E.g., Simulate C++ virtual methods in C Able to choose a suitable language for a project
– Based on different evaluation criteria, we can know which language is appropriate for a specific feature
Learn and/or design a new language more easily– Most languages share a common fundamental principles, e.g.,
after learning the object-oriented paradigm, you will find C++ and Java more easier to study
Write more efficient code and/or fix a bug more easily– E.g., after studying the parameter passing mechanism, you will
avoid passing large array into a function.
Language Evaluation
Initially, programmer only concerned what a language can do for them, but not how easy the tasks can be done.
However, the resulting programs were difficult to read and write and harmed the reliability of the program
It is important to consider the following criteria for language evaluation
– readability– writability– reliability– cost
Readability
Measures how easily a program language can be understood
The five important characteristics of programming languages that can affect the readability are:– Overall simplicity of the language– Number of exceptional rules (orthogonality)– Control Statements– Data types and structures of the language– Syntax of the language
Simplicity of a Language
The three basic factors that determine the overall simplicity are– Number of basic components
If there are too many components, it will be very difficult to remember all of their functions
– Alternative means of accomplishing the same operationMethod that is used by the author may not be the one with which the reader is also familiar
Example: an array element in C can be accessed through:A[2] or *(A+2)
– The meanings of operators are re-definableIf the operator is given a meaning that is not of common sense, it will be very difficult to read
Example: if A and B are records and A+B means the sum of one of the field, it will be very difficult to know the meaning without studying the whole code.
Number of Exceptional Rules
If the number of rules is small and number of exceptions to the rules is few, the language is easier to learn and to read– Example: all data in Smalltalk is object, but some
data in Java are primitive data and some are objectsIt will be difficult to know if it is valid when a method call is send to a data.
Control Statements
It is a well-known problem by using unconditional jump statement goto– Readability severely reduced by indistinguishing use
of goto statement
Nowadays, most language do not need to use goto statement because of the available of other control statements
Data Types and Structures
For example, availability of boolean type makes meaning more clear
– check=true compare to check=1
The availability of user-defined data types and data structures can improve the program readability significantly
– Example, if using an array of structure, we can relate the name and salary of a staff memberstaff[3].name and staff[3].salary. If the data is stored into two separate array, we do not know name[3] and salary[3] is related
Syntax Consideration
Syntax affects readability in three ways– Identifier forms
If a language allows longer name, the program would be easier to understand. inventory_level is more easy to understand than invlvl
– Special wordsE.g., C uses { } to enclose a block of statement and it is sometimes difficult to decide which { } form a pair. Some other language uses end keyword helps to determine the block
– Form and meaningSome languages use identical language constructs for different meanings – this greatly reduces the readability.
Writability
Measures how easily a program can be written using a language
Program writability is affected by the following factors– Simplicity and Fewer Exceptional Rules
smaller number of primitive constructs and fewer exceptional rules make programmers less to remember
– Support for abstraction abstraction allows programmer to hide the implementation details
and simplifies the use of the details of the classesExample: the programmers who use stack do not need to know the detail implementation of stack
– Expressivity Provides convenient ways for computation (usually reduces
readbility)
Reliability
A program's reliability is very important. The features that affects program reliability– Type checking– Exception handling– Aliasing– Readability and Writability
Type Checking
Type checking is a testing for type errors in a program– Run time type checking is expensive. Thus, compile
time checking is more desirable
In original C and FORTRAN do not do type checking for parameters of function call
Other Reliability Related Features
Exception Handling– This is a mechanism that intercept runtime errors,
take corrective procedures and then continues the program execution
Alias-ing– The situation of having two or more different
referencing methods for the same memory location– Alias-ing severely reduces program reliability– Aliasing example: pointer and union in C
Readability, Writability and Reliability
Higher readability usually improves reliability (easy to spot the errors)
High writability may improve or reduce reliability in different cases– A program that is easy to write may improve
reliability– However, expressivity of a program may reduce
reliability also
The Cost
Training Cost – if fewer exceptional rules, cost is lower Cost of Writing Programs – Higher writability reduces
the cost Cost of Compiling Programs – some compiler may
need to read the program twice Cost of Running Programs – interpreted languages
like Basic have higher running cost Cost of the Language Implementation System –
some program need special hardware platform Cost of Reliability Cost of Maintaining Programs – Higher readability
reduces the maintenance cost
Computer Architecture
Most current computer have the so-called von Neumann architecture.
The two major components in von Neumann computers are
– Memory, which stores program instructions and data– Central Processing Unit (CPU), which executes program
instructions The two components are separate.
– Instructions and data are transmitted first from memory to CPU– CPU can then execute the instructions– After execution of the instructions, results in CP will move back
to memory
Computer Architecture (cont'd)
The design of constructs and features in some imperative programming language model comes from the von Neumann architecture
Programming languages (such as functional language) that do not model the computation processes of von Neumann architecture will be less efficient than imperative languages
Programming Methodologies
Different system methodologies are developed to formalize the system development process, reduce development cost and improve system reliability. Some examples are:– Top-Down design and stepwise refinement– Object-Oriented Design– Process-Oriented Design
Top-Down Design and Stepwise Refinement
The main task is subdivided into smaller tasks The task are divided until they are small
enough to be implemented as a module Example: ATM System is divided into subtasks
like password validation, balance checking, money delivery, account updating and etc.
This stepwise refinement methodology promotes the use of subprogram and modules
Object-Oriented Design
Real world objects are modeled by encapsulating their attributes and operations
The whole system is modeled by the interactions between these objects
Example: One object in ATM system may be account. The attributes may be owner name, account ID, balance and etc. and the operation may be withdrawal.
The object-oriented methodology promotes the use of object-oriented programming languages
Process-Oriented Design
Different processes are modeled separately The whole system is modeled by the interactions
between these processes Example: In a nuclear power plant, a process may
regulate the water temperature and another process may wait for command from the control panel. When the latter receives a command, you may send signal and notify the other
The methodology is mainly used in real time concurrent systems and promotes languages like Ada
Language Categories
Imperative Languages– The procedures on how to perform the computation are stated
explicitly, e.g., C, Pascal– Visual languages (once called fourth generation language, 4GL) is a
subcategory of the imperative languages. E.g. Visual Basic Object-Oriented Languages
– The behavior of objects in program are explicitly stated. – E.g., Java, SmallTalk
Functional Languages– The result of applying a function to some parameter are stated
explicitly. The program can be done without variables, assignment statement and iteration. E.g., LISP
Logic Programming Languages– The rules with no particular order that have to be observed are stated
explicitly. E.g. Prolog.
Language Design Tradoffs
The factors to evaluate a language may conflict with each other
– FORTRAN does not require declaration of variable before using: Writability Reliability
Generally:– Higher readability means a program is easier to understand –
bug fixing is easier and program should be more reliable– More checking needs to be done for a more reliable language.
Cost and reliability is in conflict.– The high expressivity may make a language highly writable,
but it may greatly reduce the readability of the program
FORTRAN
Fortran is the first compiled high-level language, initially designed specially for IBM 704
– IBM 704 is the first computer with the capability to handle indexing and floating point instruction in hardware
The environment in which Fortran was developed– Computers were small, slow and relatively unreliable– Computers were mainly for scientific computation– No existing efficient ways to program computer– High cost of computers compared to the cost of programmers
– this made the speed of code rather readability and writability becomes the main goal of designing the program language.
FORTRAN (cont'd)
Fortran constructs include– input and output formatting statement– separately compliable user-defined subroutines
initially, Fortran I subroutines did not have the independent compilation capability, made it hard to create a program with length more than 300. Fortran II added this feature
– arithmetic IF statement Syntax: IF (arithmetic expression) N1, N2, N3
– if negative N1, if zero N2, if positive N3 Developed because there is a three branch instruction in IBM 704
– Logical IF/ELSE Statement Logical IF was added in Fortran IV ELSE statement was added in Fortran 77
FORTRAN (cont'd)
– DO loop statement a posttest loop developed in Fortran I using IBM 704 instruction
– Logical loop control statement developed in Fortran IV
– Explicit type of declarations of variables Initially in Fortran I, there is no explicit data-typing. Variables whose
name began with I, J, K, L, M and N were implicitly integer. Fortran IV's allow explicit typing
– Character string processing Fortran IV
– Case statement, Array processing, dynamic array, recursive subroutines and etc.
Fortran 90
Pascal
Pascal is an imperative language designed for teaching programming
There are some insecurities in Pascal, but it is still relatively safer than C and Fortran
Its simplicity and expressivity is well known also.
C
C is popular for a number of reasons– C is simple and small
COBOL has ~300 reserved words, C has only 32 C provides input and output facility through library
– C is portable C program can run on several platform with no modification Portability eliminates the cost of redevelopment
– C is available in all UNIX machine All versions of UNIX contain a C version as their native language
– C has the advantages of high-level and low-level language allows programmer to control the hardware and OS
SmallTalk
Smalltalk is the first Object-Oriented programming language
Smalltalk has four basic components of OO– classes– objects– inheritance– polymorphism
C++
C++ is an enhancement of C that combines features of OO and imperative languages
C++ has the following characteristics– C++ supports function parameter type checking and conversion– It supports the definition of classes, subclasses, and friendly
classes– There are public, private and protected access control modifiers– Classes in C++ have constructor and destructor functions– It provides single and multiple inheritance– Operator and functions can be overloaded– Programmers can define virtual functions to achieve
polymorphism– It has exception handling facility
LISP
LISP (stands for LISt Processing) is a functional programming language used mostly in Artificial Intelligence applications.
Pure LISP has only two kinds of data structures– Atoms: either symbols or numeric literals– Lists: specified by delimiting their elements with parentheses
E.g., (A B C D) is a simple list whose elements are atoms E.g., (A (B C) D ((E) (F G))) is a nested list whose elements can
be atoms or lists Internal representations will use single-linked lists
In LISP syntax, the first element in a list is usually taken as the function and the rest elements are the parameter applies to the function
Prolog
Prolog is a logic programming language that uses a formal logic notation as a basis
– Used mainly in developing AI applications, or as a kind of intelligent database
Prolog is non-procedural – the programs specify what kind of results are desirable, but not how the result is computed
The database of a Prolog program consists of facts and rules only.
– Example of facts: mother(joanne, jack). father(john, jack).– Example of rule: grandparent(X,Z) :- parent(X,Y), parent(Y,Z).
A query starts a Prolog program by searching through its database for the stated result
– Example of query ?- father(john, jack).– The prolog will search for the goal and give "true"/"false" answer