Re: OOP considered snake oil
Xah Lee <xah@xahlee.org> writes about:
snake oil
The original Chinese snake oil indeed seems to have
inflammation-reducing properties and thus might be a
?plausible remedy for joint pain? (Wikipedia).
Then, even object-oriented programming might have something
good? But, wait, what does ?object-oriented programming?
actually mean?
These are the questions I recently asked myself.
My answers are following. Other answers are possible, too.
Maybe someone wants to provide them.
~~
What is object-oriented programming?
Object-oriented programming uses objects (storage entities)
annotated with run-time types: Every object carries
information about its type, which implies information about
the layout and encoding of its storage.
What is an object-oriented programming language?
An object-oriented programming language supports
object-oriented programming, but it usually hides the type
information of objects and automatically assigns operations
specified for a type to each object. Thus, in an object
oriented programming language, the programmer does not read
the type information himself nor does he use it to select an
appropriate action. Instead he just states the action by a
"verb" with a type and the programing language will then
make sure, that the correct implementation is called for any
given object at run-time. This is called ?run-time
polymorphism? or ?late binding?. Individual object-oriented
programming languages might include additional support for
object-oriented programing, but this binding is the core
feature of every object-oriented programming language.
What is the main advantage of an object-oriented programming
language?
The main advantage is that verbs can be extended without the
need to modify existing code. For example, in Java, a new
type can extend the verb ?toString?. This extension can then
be used immediatly by the existing ?println? verb, without
the need that the author of the code for ?println? was aware
of the new type, so it is not necessary to modify the given
Java SE library, which contains other definitions of the
verb ?toString?. One only needs to /add/ new code. Thus, the
?open/closed principle? is fulfilled.
Can you give a small example programm that shows something
that can be done in an object-oriented programming language,
but is not possible in a purely procedural programming
language?
Yes, a Java program illustrating the answer to the previous
question is:
class Position
{ int x; int y; public java.lang.String toString()
{ return java.lang.String.valueOf( x ) +
", " + java.lang.String.valufOf( y ); }}
public class Main
{ public static void main( final java.lang.String[] args )
{ java.lang.System.out.println( new Position() ); }}
The output of this program is as follows.
0, 0
What is the reason for run-time information?
It is needed when ever data is to be transfered between to
parties, whose encoding, layout or language might change or
is not yet known, to indicate this encoding, layout or
language.