1-14 GENERAL PROGRAMMING TECHNIQUES
***********************************
(Thanks to Craig Burley for the excellent comments)
We will discuss the following techniques:
1) Framing
2) Escaping
3) Caching
4) Buffering
5) Logical layering (Physical/logical/virtual)
6) Centralized resource management
7) Object Oriented Programming (OOP)
8) Lookup tables
Framing methods
---------------
A common method of combining a sequence of bytes to form one unit,
usually several such units are stringed one after the other.
Examples of framing:
1) Variable-size records used in FORTRAN unformatted files
2) Packets used in TCP/IP and other communication protocols
3)
A possible structure of one such unit may be:
+-----------+-------+------------+-----------------+
| Signature | Count | Data bytes | Integrity check |
+-----------+-------+------------+-----------------+
SIGNATURE some byte combination that identifies the unit type
(by some convention agreed on between the users).
COUNT an integer (whose size is agreed on by convention),
the value of COUNT is the number of data bytes to follow.
With the COUNT field units can have variable length,
that flexibility is important in many applications.
DATA BYTES the 'payload' ...
CHECK Some error checking/correcting code associated with the
data when the data is written, the code can be rechecked
whenever the unit is accessed to validate data integrity.
Escaping methods
----------------
A common problem in data transfer is that you have only one communication
path and you have to pass two different types of information along
(e.g. data and commands), or your data coding method must be expanded
to include more codes.
Examples for such methods:
1) Using Shift/Ctrl/Alt/Meta key modifiers on a keyboard
2) Escaping special and control characters in C source code
and in UNIX shells
3) Switching to the command mode of a modem by the '+++'
escape sequence (subject also to timing constraints)
4) Some binary encoding schemes used to transfer binary
data through electronic mail (Quoted Printable)
5) ANSI terminal escape sequences
6) Escaping CPU instructions in the Intel PC CPU to the
mathematical co-processor
7) Controls prefixing in the Kermit file transfer protocol (?)
8) Hyper-Text Markup Language (HTML) start tag sign '<' ?
9) PC keyboards transmit special combinations as a zero byte
followed by the scan-code.
Escaping is done by sending a special sequence that can't happen in
practice, characters following the special sequence are interpreted
in a different way from characters outside its 'scope'.
In other words the special 'escape' sequence 'switches on' a special
interpretation mode (that has to be 'switched off' later).
How you can ensure that the escape sequence will not occur in the
'normal' data stream? Either you choose a very improbable combination,
or you restrict what goes on through the data stream.
Switching off the special mode is an easier problem.
Caching
-------
Whenever we have available two data storage methods, it usually
happens that one of them will be slower and cheaper so we can buy
more storage, and the other faster and more costly:
1) Local files vs. files accessed through the web
2) Local filesystem vs. network filesystem (NFS)
3) Local files/partitions vs. main memory (RAM)
4) Dynamic memory vs. static memory
The idea of caching is having ...................................
Caching is an effective technique ...............................
(e.g. keeping temporary data in arrays vs. a file) ..............
Buffering
---------
Not ready yet !
Logical Layering
----------------
Well, the best example would be Fortran itself.
People are working on a lot of different machines, of course 'real
programmers' code directly in machine language, but we poor 'virtual
programmers' need a simpler and portable interface to our computer.
The solution is to define a 'high-level language' e.g. Fortran/C/...
and have on each machine a program (compiler) that translates programs
written in Fortran/C/... to machine language. At the price of some
CPU time we get a portable and easy to use interface to every computer
(if someone wrote a compiler for it).
This reminds one of the Esperanto language, that invented to enhance
communication between people of different nations. The inventor of
Esperanto thought that people should learn one artificial language
(designed to be simple as possible) instead of learning all common
languages.
Examples for 'logical layering' are innumerable, they can be found
in the following interfaces:
1) Interactive user / Operating system / Different kinds of terminals
2) User program / Operating system / Different disks and tape drives
3) File names / File system / Data blocks on disks and tape drives
4) Graphic package / Different graphic devices
5) Word-processor / Different printers
6) In a PC: DOS functions / ROM-BIOS interrupts
7) In TCP/IP: DNS names/IP addresses/Ethernet hardware addresses
The 'logical interface' is implemented by a 'translator' that can
translate between the two languages spoken on the two sides of the
interface.
The physical/logical/virtual distinction .........................
Centralized resource management
-------------------------------
When resources such as the pool of logical unit numbers, access to
some data file, etc are shared by several routines or processes, the
natural way to eliminate access collisions is to have a centralized
management.
The resource management can be ....................................
Object-Oriented Programming
---------------------------
Like other good things (Ethernet, windowing systems), OOP was invented
in Xerox a long time ago.
The idea behind OOP is (approximately) that sometimes it is more natural
to partition a program to LOGICAL ENTITIES communicating by passing
messages instead of FUNCTIONAL UNITS as in the modular paradigm.
A good example is a modern visualization program, in which you choose
the required data processing modules from a menu, connect them
together with some mouse clicks, and then tell the input module the
name of your data file, and the whole thing starts producing pictures.
The only suitable conceptual model for this kind of magic is to view
the program as a set of logical entities: the input reader module reads
your 3-dimensional data file, it passes the data to a module that takes
a 2D section, then a graphic module translates the floating-points
to a color picture where the colors correspond to magnitude.
The logical entities used in OOP are a sweeping generalization of the
data structure called 'record' in Pascal and 'structure' in C: an ordered
collection of simpler data structures, not necessarily of the same type,
that can be accessed individually by a name that consists of two parts,
the structure name and the name of the 'field'.
Allowing such 'records' to include also procedures that perform functions
specific to the record type creates a data structure that can implement
the abstract concept of object.
Object-oriented programming can be directly expressed with suitable
programming languages (e.g. Objective C, C++) that support 'objects'.
Objects are data structures with associated functions, maintenance of
the data structure values is performed only with the associated functions.
Fortran 90 supports a few OOP concepts, and the rest can be done
"by hand" if the programmer is disciplined enough.
Lookup tables
-------------
Lookup tables are a simple programming trick to speed a special kind
of calculations.
If you have to compute again and again some function or expression
and the possible values the argument(s) can take are relatively few
you can do the computing once and put the results in an array.
To retrieve results you just reference the array.
Return to contents page